protected internal override void Receive(IClientMetadata state, int offset = 0) { try { state.SslStream = _sslStream; if (offset > 0) { state.UnhandledBytes = state.Buffer; } if (state.Buffer.Length < state.BufferSize) { state.ChangeBuffer(new byte[state.BufferSize]); if (offset > 0) { Array.Copy(state.UnhandledBytes, 0, state.Buffer, 0, state.UnhandledBytes.Length); } } _mreRead.WaitOne(); _mreRead.Reset(); state.SslStream.BeginRead(state.Buffer, offset, state.BufferSize - offset, ReceiveCallback, state); } catch (Exception ex) { throw new Exception(ex.Message, ex); } }
protected internal override void Receive(IClientMetadata state, int offset = 0) { try { var firstRead = true; while (!Token.IsCancellationRequested) { state.MreReceiving.WaitOne(); state.MreTimeout.Reset(); state.MreReceiving.Reset(); if (!firstRead) { offset = state.Buffer.Length; } if (offset > 0) { state.UnhandledBytes = state.Buffer; } if (state.Buffer.Length < state.BufferSize) { state.ChangeBuffer(new byte[state.BufferSize]); if (offset > 0) { Array.Copy(state.UnhandledBytes, 0, state.Buffer, 0, state.UnhandledBytes.Length); } } firstRead = false; state.Listener.BeginReceive(state.Buffer, offset, state.BufferSize - offset, SocketFlags.None, ReceiveCallback, state); if (Timeout.TotalMilliseconds > 0 && !state.MreTimeout.WaitOne(Timeout, false)) { throw new SocketException((int)SocketError.TimedOut); } } } catch (SocketException se) { if (se.SocketErrorCode == SocketError.TimedOut) { Log("Client with id: " + state.Id + " and guid: " + state.Guid + " has timed out."); RaiseClientDisconnected(state, DisconnectReason.Timeout); } else { RaiseErrorThrown(se); } Close(state.Id); } catch (Exception ex) { RaiseErrorThrown(ex); Close(state.Id); } }
protected internal override void Receive(IClientMetadata state, int offset = 0) { try { var firstRead = true; while (!Token.IsCancellationRequested) { state.MreReceiving.WaitOne(); state.MreTimeout.Reset(); state.MreReceiving.Reset(); if (!firstRead && state.Buffer.Length != state.BufferSize) { offset = state.Buffer.Length; } if (offset > 0) { state.UnhandledBytes = state.Buffer; } if (state.Buffer.Length < state.BufferSize) { state.ChangeBuffer(new byte[state.BufferSize]); if (offset > 0) { Array.Copy(state.UnhandledBytes, 0, state.Buffer, 0, state.UnhandledBytes.Length); } } firstRead = false; var sslStream = state.SslStream; state.MreRead.WaitOne(); state.MreRead.Reset(); sslStream.BeginRead(state.Buffer, offset, state.BufferSize - offset, ReceiveCallback, state); if (Timeout.TotalMilliseconds > 0 && !state.MreTimeout.WaitOne(Timeout, false)) { throw new SocketException((int)SocketError.TimedOut); } } } catch (Exception ex) { Log(ex); Log("Error trying to receive from client " + state.Id); throw new Exception(ex.Message, ex); } }
protected internal override void Receive(IClientMetadata state, int offset = 0) { bool firstRead = true; while (!Token.IsCancellationRequested) { _mreReceiving.WaitOne(); if (_sslStream == null) { if (IsRunning == true) { RaiseDisconnected(); } Close(); throw new SocketException((int)SocketError.NotConnected); } _mreReceiving.Reset(); state.SslStream = _sslStream; if (!firstRead && state.Buffer.Length != state.BufferSize) { offset = state.Buffer.Length; } if (offset > 0) { state.UnhandledBytes = state.Buffer; } if (state.Buffer.Length < state.BufferSize) { state.ChangeBuffer(new byte[state.BufferSize]); if (offset > 0) { Array.Copy(state.UnhandledBytes, 0, state.Buffer, 0, state.UnhandledBytes.Length); } } firstRead = false; _mreRead.WaitOne(); _mreRead.Reset(); state.SslStream.BeginRead(state.Buffer, offset, state.BufferSize - offset, ReceiveCallback, state); } }
protected internal override void Receive(IClientMetadata state, int offset = 0) { try { bool firstRead = true; while (!Token.IsCancellationRequested) { _mreReceiving.WaitOne(); _mreReceiving.Reset(); state.SslStream = _sslStream; if (!firstRead && state.Buffer.Length != state.BufferSize) { offset = state.Buffer.Length; } if (offset > 0) { state.UnhandledBytes = state.Buffer; } if (state.Buffer.Length < state.BufferSize) { state.ChangeBuffer(new byte[state.BufferSize]); if (offset > 0) { Array.Copy(state.UnhandledBytes, 0, state.Buffer, 0, state.UnhandledBytes.Length); } } firstRead = false; _mreRead.WaitOne(); _mreRead.Reset(); state.SslStream.BeginRead(state.Buffer, offset, state.BufferSize - offset, ReceiveCallback, state); } } catch (SocketException se) { Log("Socket error thrown: " + se.Message); } catch (Exception ex) { RaiseErrorThrown(ex); } }
protected internal override void Receive(IClientMetadata state, int offset = 0) { if (offset > 0) { state.UnhandledBytes = state.Buffer; } if (state.Buffer.Length < state.BufferSize) { state.ChangeBuffer(new byte[state.BufferSize]); if (offset > 0) { Array.Copy(state.UnhandledBytes, 0, state.Buffer, 0, state.UnhandledBytes.Length); } } state.Listener.BeginReceive(state.Buffer, offset, state.BufferSize - offset, SocketFlags.None, ReceiveCallback, state); }
protected internal override void Receive(IClientMetadata state, int offset = 0) { bool firstRead = true; while (!Token.IsCancellationRequested) { _mreReceiving.WaitOne(); if (state.Listener == null || Listener == null) { throw new SocketException((int)SocketError.NotConnected); } _mreReceiving.Reset(); if (!firstRead) { offset = state.Buffer.Length; } if (offset > 0) { state.UnhandledBytes = state.Buffer; } if (state.Buffer.Length < state.BufferSize) { state.ChangeBuffer(new byte[state.BufferSize]); if (offset > 0) { Array.Copy(state.UnhandledBytes, 0, state.Buffer, 0, state.UnhandledBytes.Length); } } firstRead = false; state.Listener.BeginReceive(state.Buffer, offset, state.BufferSize - offset, SocketFlags.None, this.ReceiveCallback, state); } }
protected internal override void Receive(IClientMetadata state, int offset = 0) { try { var firstRead = true; while (!Token.IsCancellationRequested) { state.MreTimeout.Reset(); state.MreReceiving.WaitOne(); state.MreReceiving.Reset(); if (!firstRead && state.Buffer.Length != state.BufferSize) { offset = state.Buffer.Length; } if (offset > 0) { state.UnhandledBytes = state.Buffer; } if (state.Buffer.Length < state.BufferSize) { state.ChangeBuffer(new byte[state.BufferSize]); if (offset > 0) { Array.Copy(state.UnhandledBytes, 0, state.Buffer, 0, state.UnhandledBytes.Length); } } firstRead = false; var sslStream = state.SslStream; state.MreRead.WaitOne(); state.MreRead.Reset(); sslStream.BeginRead(state.Buffer, offset, state.BufferSize - offset, ReceiveCallback, state); if (Timeout.TotalMilliseconds > 0 && !state.MreTimeout.WaitOne((int)Timeout.TotalMilliseconds, false)) { throw new SocketException((int)SocketError.TimedOut); } } } catch (SocketException se) { if (se.SocketErrorCode == SocketError.TimedOut) { Log("Socket has timed out."); RaiseClientTimedOut(state); } else { RaiseErrorThrown(se); } } catch (Exception ex) { Log("Error trying to receive from client with id:" + state.Id + " and Guid: " + state.Guid); RaiseErrorThrown(ex); } finally { Log("Closing socket from client with id:" + state.Id + " and Guid: " + state.Guid); Close(state.Id); Log("Socket from client with id:" + state.Id + " and Guid: " + state.Guid + " has been closed."); } }