public void ClearAndClose() { Stream.Close(); Stream = null; Client.Close(); Client = null; ReceiveBuffer = null; }
private static void AcceptClient(IAsyncResult ar) { var state = (AcceptClientState)ar.AsyncState; TcpClient client; try { client = state.Listener.EndAcceptTcpClient(ar); } catch { // Assume the socket has been closed. return; } NetworkStream stream = client.GetStream(); TcpReceiveBuffer buffer = new TcpReceiveBuffer( maxPacketSize: state.MaxPacketSize, packetQueueCapacity: state.PacketQueueCapacity); state.Clients.Add(new TcpClientData { Client = client, Stream = stream, ReceiveBuffer = buffer, }); state.StreamMessageReader.Start(stream, buffer); state.Logger.Info($"Connected. remoteEp={client.Client.RemoteEndPoint}"); // Since we're adding a new client this is a great time to clean up any disconnected clients. state.Clients.CleanupAnyDisconnectedClients(); try { // Begin waiting for the next request. state.Listener.BeginAcceptTcpClient(AcceptCallback, state); } catch { // Assume the socket has closed. } }
public TcpSocketClient(ILogger logger, int maxPacketSize, int packetQueueCapacity) { this._client = new TcpClient(AddressFamily.InterNetworkV6); this._receiveBuffer = new TcpReceiveBuffer(maxPacketSize, packetQueueCapacity); this._tcpReceiver = new TcpStreamMessageReader(logger, maxPacketSize, packetQueueCapacity); this._requestAsyncStates = new RequestAsyncState[packetQueueCapacity]; this._freeRequestAsyncStateIndices = new int[packetQueueCapacity]; this._idToRequestAsyncStateIndex = new Dictionary <int, int>(packetQueueCapacity); this._requestAsyncStateCount = 0; this._freeRequestAsyncStateCount = 0; this._receiveBuffer.OnWriteComplete = this.OnWriteComplete; this.OnCancelResponseTcs = OnSendAsyncCancellation; this._stateLock = new object(); this._streamWriteLock = new object(); }
public void Start(NetworkStream stream, TcpReceiveBuffer receiveBuffer) { var state = new TcpStreamMessageReadingState { Stream = stream, ReceiveBuffer = receiveBuffer, AcceptReadBuffer = new byte[this._maxMessageSize * this._maxMessageQueueSize], AcceptReadBufferSize = 0, MessageSize = 0, ReadCallback = AcceptRead, Logger = this._logger, }; stream.BeginRead( state.AcceptReadBuffer, 0, state.AcceptReadBuffer.Length, state.ReadCallback, state); }
public TcpClientsEventArgs(TcpReceiveBuffer receiveBuffer) { this.ReceiveBuffer = receiveBuffer; }