private void CloseSocket() { if (Interlocked.CompareExchange( ref this.closedSwitch, 1, 0) != 0) { return; } try { this.remoteParty.Shutdown(SocketShutdown.Both); } catch (SocketException ex) { this.TriggerCommunicationError(ex.ErrorCode); } try { this.remoteParty.Close(EnvironmentSettings.DefaultSocketCloseTimeout); } catch (SocketException ex) { this.TriggerCommunicationError(ex.ErrorCode); } // ReSharper disable once MethodSupportsCancellation - We don't really want this cancelled Fire.AndForget( (thisL1) => thisL1.Disconnected?.Invoke(thisL1, EventArgs.Empty), this); }
/// <summary> /// Triggers the message received event. /// </summary> /// <param name="message">The message to trigger with.</param> protected private void TriggerMessageReceived(TMessage message) => Fire.AndForget( ( thisL1, messageL1) => thisL1.MessageReceived?.Invoke( thisL1, new ContextObjectEventArgs <TMessage>(messageL1)), this, message);
protected private HighPerformanceSimpleMessageSocket([NotNull] Socket remoteParty, CancellationToken cancellationToken) { Contract.RequiresNotNull( ref this.remoteParty, remoteParty, nameof(remoteParty)); this.cancellationToken = cancellationToken; this.dcs = new DataContractSerializer(typeof(TMessage)); Fire.AndForget( this.ReceiveThread, cancellationToken); }
/// <summary> /// Triggers the communication error event. /// </summary> /// <param name="errorCode">The error code.</param> protected private void TriggerCommunicationError(int errorCode) { if (errorCode == Interlocked.CompareExchange( ref this.lastError, errorCode, errorCode)) { return; } // ReSharper disable once MethodSupportsCancellation - We do not want this to cancel Fire.AndForget( ( thisL1, messageL1) => thisL1.CommunicationError?.Invoke( thisL1, new ContextObjectEventArgs <int>(messageL1)), this, errorCode); }