示例#1
0
 public void ClearAndClose()
 {
     Stream.Close();
     Stream = null;
     Client.Close();
     Client        = null;
     ReceiveBuffer = null;
 }
示例#2
0
        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.
            }
        }
示例#3
0
        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();
        }
示例#4
0
        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);
        }
示例#5
0
 public TcpClientsEventArgs(TcpReceiveBuffer receiveBuffer)
 {
     this.ReceiveBuffer = receiveBuffer;
 }