Skip to content

Commit

Permalink
Merge pull request Redth#34 from SimonCropp/master
Browse files Browse the repository at this point in the history
make event calls thread safe
  • Loading branch information
Redth committed Mar 6, 2012
2 parents b294c0c + 26700b2 commit a32c2a4
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 57 deletions.
64 changes: 36 additions & 28 deletions JdSoft.Apple.Apns.Notifications/NotificationConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,29 +333,33 @@ private void start()

void OnChannelDisconnected(object sender)
{
if (Disconnected != null)
Disconnected(this);
var onDisconnected = Disconnected;
if (onDisconnected != null)
onDisconnected(this);
}

void OnChannelConnecting(object sender)
void OnChannelConnecting(object sender)
{
if (Connecting != null)
Connecting(this);
var onConnecting = Connecting;
if (onConnecting != null)
onConnecting(this);
}

void OnChannelConnected(object sender)
{
if (Connected != null)
Connected(this);
}
void OnChannelConnected(object sender)
{
var onConnected = Connected;
if (onConnected != null)
onConnected(this);
}

void OnChannelError(object sender, Exception ex)
void OnChannelError(object sender, Exception ex)
{
if (Error != null)
Error(this, ex);
var onError = Error;
if (onError != null)
onError(this, ex);
}

private void workerMethod()
private void workerMethod()
{
while (!disposing && !closing)
{
Expand All @@ -382,19 +386,20 @@ private void workerMethod()
}
catch (BadDeviceTokenException btex)
{
if (this.BadDeviceToken != null)
this.BadDeviceToken(this, btex);
var onBadDeviceToken = BadDeviceToken;
if (onBadDeviceToken != null)
onBadDeviceToken(this, btex);
}
catch (NotificationLengthException nlex)
{
if (this.NotificationTooLong != null)
this.NotificationTooLong(this, nlex);
var onNotificationTooLong = NotificationTooLong;
if (onNotificationTooLong != null)
onNotificationTooLong(this, nlex);
}

string txtAlert = string.Empty;

if (this.NotificationSuccess != null)
this.NotificationSuccess(this, notification);
var onNotificationSuccess = NotificationSuccess;
if (onNotificationSuccess != null)
onNotificationSuccess(this, notification);

sent = true;
}
Expand All @@ -405,8 +410,9 @@ private void workerMethod()
}
catch (Exception ex)
{
if (this.Error != null)
this.Error(this, ex);
var onError = Error;
if (onError != null)
onError(this, ex);

apnsChannel.ForceReconnect();
}
Expand All @@ -415,14 +421,16 @@ private void workerMethod()
}

//Didn't send in 3 tries
if (!sent && this.NotificationFailed != null)
this.NotificationFailed(this, notification);
var onNotificationFailed = NotificationFailed;
if (!sent && onNotificationFailed != null)
onNotificationFailed(this, notification);
}
}
catch (Exception ex)
{
if (this.Error != null)
this.Error(this, ex);
var onError = Error;
if (onError != null)
onError(this, ex);

apnsChannel.ForceReconnect();
}
Expand Down
56 changes: 32 additions & 24 deletions JdSoft.Apple.Apns.Notifications/NotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,53 +399,61 @@ public void Dispose()
#region Private Methods
void newCon_NotificationSuccess(object sender, Notification notification)
{
if (this.NotificationSuccess != null)
this.NotificationSuccess(sender, notification);
var onNotificationSuccess = NotificationSuccess;
if (onNotificationSuccess != null)
NotificationSuccess(sender, notification);
}

void newCon_NotificationTooLong(object sender, NotificationLengthException ex)
void newCon_NotificationTooLong(object sender, NotificationLengthException ex)
{
if (this.NotificationTooLong != null)
this.NotificationTooLong(sender, ex);
var onNotificationTooLong = NotificationTooLong;
if (onNotificationTooLong != null)
NotificationTooLong(sender, ex);
}

void newCon_BadDeviceToken(object sender, BadDeviceTokenException ex)
void newCon_BadDeviceToken(object sender, BadDeviceTokenException ex)
{
if (this.BadDeviceToken != null)
this.BadDeviceToken(this, ex);
var onBadDeviceToken = BadDeviceToken;
if (onBadDeviceToken != null)
BadDeviceToken(this, ex);
}

void newCon_NotificationFailed(object sender, Notification failed)
void newCon_NotificationFailed(object sender, Notification failed)
{
if (this.NotificationFailed != null)
this.NotificationFailed(sender, failed);
var onNotificationFailed = NotificationFailed;
if (onNotificationFailed != null)
NotificationFailed(sender, failed);
}

void newCon_Error(object sender, Exception ex)
void newCon_Error(object sender, Exception ex)
{
if (this.Error != null)
this.Error(sender, ex);
var onError = Error;
if (onError != null)
Error(sender, ex);
}

void newCon_Disconnected(object sender)
void newCon_Disconnected(object sender)
{
if (this.Disconnected != null)
this.Disconnected(sender);
var onDisconnected = Disconnected;
if (onDisconnected != null)
Disconnected(sender);
}

void newCon_Connected(object sender)
void newCon_Connected(object sender)
{
if (this.Connected != null)
this.Connected(sender);
var onConnected = Connected;
if (onConnected != null)
Connected(sender);
}

void newCon_Connecting(object sender)
void newCon_Connecting(object sender)
{
if (this.Connecting != null)
this.Connecting(sender);
var onConnecting = Connecting;
if (onConnecting != null)
onConnecting(sender);
}

private bool queueSequential(Notification notification)
private bool queueSequential(Notification notification)
{
if (notificationConnections.Count <= sequential && sequential > 0)
sequential = 0;
Expand Down
6 changes: 1 addition & 5 deletions JdSoft.Apple.Apns.Notifications/ThreadSafeQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,10 @@ public ThreadSafeQueue()

public T Dequeue()
{
T result = default(T);

lock (lockObj)
{
result = queue.Dequeue();
return queue.Dequeue();
}

return result;
}

public void Enqueue(T item)
Expand Down

0 comments on commit a32c2a4

Please sign in to comment.