예제 #1
0
        protected void SendRequestSync(object o)
        {
            ClientRequest lastRequest = o as ClientRequest;

            try
            {
                if (((lastRequest != null) && (lastRequest.BinaryRequest != null)) && (lastRequest.BinaryRequest.Length > 0))
                {
                    ProtocolCommon.WriteToStream(this._Writer, lastRequest.BinaryRequest);
                }
                if ((lastRequest == null) || lastRequest.AbortConnection)
                {
                    this.CloseConnection();
                }
                else
                {
                    lastRequest.BinaryResponse = ProtocolCommon.ReadFromStream(this._Reader);
                    ClientRequest nextRequest = new ClientRequest();
                    if (this.ResponseReceived != null)
                    {
                        this.ResponseReceived(lastRequest, nextRequest);
                    }
                    else
                    {
                        nextRequest.AbortConnection = true;
                    }
                    this.SendRequestAsync(nextRequest);
                }
            }
            catch (Exception exception)
            {
                this.Log(exception);
            }
        }
예제 #2
0
        private void ConnectedAsyncCallback(IAsyncResult ar)
        {
            TcpClient asyncState = null;

            try
            {
                asyncState = (TcpClient)ar.AsyncState;
                asyncState.EndConnect(ar);
            }
            catch (Exception exception)
            {
                if (this.ServerConnected != null)
                {
                    this.ServerConnected(ServerConnectedResult.Failed, exception.ToString(), null);
                }
                this.Log(exception);
            }
            try
            {
                this._Reader = new BinaryReader(asyncState.GetStream());
                this._Writer = new BinaryWriter(asyncState.GetStream());
                string        welcomeMessage = Encoding.Unicode.GetString(ProtocolCommon.ReadFromStream(this._Reader));
                ClientRequest firstRequest   = new ClientRequest();
                if (this.ServerConnected != null)
                {
                    this.ServerConnected(ServerConnectedResult.Connected, welcomeMessage, firstRequest);
                }
                else
                {
                    firstRequest.AbortConnection = true;
                }
                this.SendRequestAsync(firstRequest);
            }
            catch (Exception exception2)
            {
                this.Log(exception2);
            }
        }
예제 #3
0
        private void SendResponseSync(object o)
        {
            object[]      objArray = (object[])o;
            TcpClient     client   = objArray[0] as TcpClient;
            ClientRequest request  = objArray[1] as ClientRequest;

            try
            {
                if (((request != null) && (request.BinaryResponse != null)) && (request.BinaryResponse.Length > 0))
                {
                    ProtocolCommon.WriteToStream(new BinaryWriter(client.GetStream()), request.BinaryResponse);
                }
                if ((request == null) || request.AbortConnection)
                {
                    this.CloseConnection(client);
                }
                else
                {
                    ClientRequest request2 = new ClientRequest {
                        BinaryRequest = ProtocolCommon.ReadFromStream(new BinaryReader(client.GetStream()))
                    };
                    if (this.RequestReceived != null)
                    {
                        this.RequestReceived(client, request2);
                    }
                    else
                    {
                        request2.AbortConnection = true;
                    }
                    this.SendResponseAsync(client, request2);
                }
            }
            catch (Exception exception)
            {
                this.Log(exception);
            }
        }