public void Disconnect() { if (Interlocked.CompareExchange(ref connected, 0, 1) == 1) { if (cancelReconnect()) { // clear the unacknowledged messages clearRequests(CompletionListenerConstants.PUBLISH_FAILED, "Disconnected"); // a pending reconnect was cancelled, // tell the user we've disconnected listener.OnDisconnect(this, ConnectionListenerConstants.NORMAL, null); } else { // invoked in a task to prevent potential // deadlocks with message callback new Task(() => { // synchronize to prevent a disconnect // from occurring between the message // callback and message acknowledgment lock (processLock) { Queue(DISCONNECT); } }).Start(); } } }
private void ReceiveTask() { var senderAddress = new IPEndPoint(IPAddress.Any, 0); try { while (true) { var data = _client.Receive(ref senderAddress); var message = MessageSerializer.GetMessage(data); if (message != null) { _listener.OnMessageReceived(message, senderAddress); } senderAddress = new IPEndPoint(IPAddress.Any, 0); } } catch (SocketException e) { switch (e.SocketErrorCode) { case SocketError.ConnectionReset: _listener.OnDisconnect(senderAddress); break; default: throw e; } } }
public void Disconnect() { if (Interlocked.CompareExchange(ref connected, 0, 1) == 1) { if (cancelReconnect()) { // clear the unacknowledged messages publishClear(CompletionListenerConstants.PUBLISH_FAILED, "Disconnected"); // a pending reconnect was cancelled, // tell the user we've disconnected listener.OnDisconnect(this, ConnectionListenerConstants.NORMAL, null); } else { Queue(DISCONNECT); } } }