Beispiel #1
0
        public bool Connect()
        {
            lock (this)
            {
                mLastError = null;
                if (!IsConnected)
                {
                    try
                    {
                        if (mBaseNetworkStream != null)
                        {
                            mBaseNetworkStream.Dispose();
                        }
                        if (mSslStream != null)
                        {
                            mSslStream.Dispose();
                        }
                        mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        mSocket.Connect(mIPAddress, mPort);
                        mSocket.ReceiveTimeout            = TimeOut;
                        mSocket.SendTimeout               = TimeOut;
                        mConnected                        = true;
                        mLastError                        = null;
                        mBaseNetworkStream                = new PipeStream(ClientBufferPool.Pool, this.LittleEndian, this.Encoding);
                        mBaseNetworkStream.Socket         = mSocket;
                        mBaseNetworkStream.Encoding       = this.Encoding;
                        mBaseNetworkStream.LittleEndian   = this.LittleEndian;
                        mBaseNetworkStream.FlashCompleted = OnWriterFlash;
                        mSendStatus                       = 0;
                        mReceiveArgs                      = new ClientReceiveArgs();

                        if (this.Packet != null)
                        {
                            this.Packet           = this.Packet.Clone();
                            this.Packet.Completed = this.OnPacketCompleted;
                        }
                        if (SSL)
                        {
                            mSslStream = new SslStreamX(ClientBufferPool.Pool, this.Encoding, this.LittleEndian, mBaseNetworkStream, new RemoteCertificateValidationCallback(ValidateServerCertificate));
                            var task = mSslStream.AuthenticateAsClientAsync(SslServiceName);
                            task.Wait();
                        }
                        BeginReceive();
                    }
                    catch (Exception e_)
                    {
                        mConnected = false;

                        ProcessError(e_, "client connect to server error!");
                    }
                    if (IsConnected)
                    {
                        OnConnected();
                    }
                }
                return(mConnected);
            }
        }
Beispiel #2
0
        //public AwaitObject ReceiveMessage()
        //{
        //    mReadMessageAwait.Reset();
        //    bool isconnect;
        //    Connect(out isconnect);
        //    if (Packet == null)
        //        ProcessError(new BXException("packet is empty be cannot receive messages!"), "packet is empty be cannot receive messages");
        //    if (!AutoReceive)
        //        BeginReceive();
        //    return mReadMessageAwait;
        //}

        //public AwaitStruct<PipeStream> ReceiveFrom(Action<PipeStream> writeHandler)
        //{
        //    var result = Receive();
        //    if (writeHandler != null)
        //    {
        //        PipeStream stream = this.Stream.ToPipeStream();
        //        writeHandler(stream);
        //        if (stream.CacheLength > 0)
        //            this.Stream.Flush();
        //    }
        //    return result;
        //}

        //public AwaitStruct<T> ReceiveMessage<T>()
        //{
        //    return new AwaitStruct<T>(ReceiveMessage());
        //}

        //public AwaitStruct<PipeStream> Receive()
        //{
        //    awaitPipeStream.Reset();
        //    bool isconnect;
        //    Connect(out isconnect);
        //    if (!AutoReceive)
        //        BeginReceive();
        //    return new AwaitStruct<PipeStream>(awaitPipeStream);
        //}

        public bool Connect(out bool newConnection)
        {
            newConnection = false;
            if (IsConnected)
            {
                return(true);
            }
            try
            {
                lock (this)
                {
                    if (!IsConnected)
                    {
                        mLastError = null;
                        mBaseNetworkStream?.Dispose();
                        mBaseNetworkStream = null;
                        mSslStream?.Dispose();
                        mSslStream = null;
                        mSocket    = new Socket(mIPAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                        if (LocalEndPoint != null)
                        {
                            mSocket.Bind(LocalEndPoint);
                        }
                        mSocket.Connect(mIPAddress, mPort);
                        //if (LocalEndPoint == null)
                        //    LocalEndPoint = mSocket.LocalEndPoint;
                        mSocket.ReceiveTimeout            = TimeOut;
                        mSocket.SendTimeout               = TimeOut;
                        mConnected                        = true;
                        mLastError                        = null;
                        mBaseNetworkStream                = new PipeStream(SendBufferPool, this.LittleEndian, this.Encoding);
                        mBaseNetworkStream.Socket         = mSocket;
                        mBaseNetworkStream.Encoding       = this.Encoding;
                        mBaseNetworkStream.LittleEndian   = this.LittleEndian;
                        mBaseNetworkStream.FlashCompleted = OnWriterFlash;
                        mSendStatus                       = 0;
                        mReceiveArgs                      = new ClientReceiveArgs();
                        mReceiveEventArgs?.Dispose();
                        mReceiveEventArgs            = new SocketAsyncEventArgsX();
                        mReceiveEventArgs.Completed += IO_Completed;
                        mSendEventArgs?.Dispose();
                        mSendEventArgs            = new SocketAsyncEventArgsX();
                        mSendEventArgs.Completed += IO_Completed;
                        if (this.Packet != null)
                        {
                            this.Packet           = this.Packet.Clone();
                            this.Packet.Completed = this.OnPacketCompleted;
                        }
                        if (SSL)
                        {
                            mBaseNetworkStream.SSL = true;
                            mSslStream             = new SslStreamX(ReceiveBufferPool, this.Encoding, this.LittleEndian, mBaseNetworkStream, new RemoteCertificateValidationCallback(ValidateServerCertificate));
                            //var task = mSslStream.AuthenticateAsClientAsync(SslServiceName);
                            //task.Wait();
                            OnSslAuthenticate(mSslStream);
                            mBaseNetworkStream.SSLConfirmed = true;
                            mSslStream.SyncData(OnReceive);
                        }
                        if (AutoReceive)
                        {
                            BeginReceive();
                        }
                        if (IsConnected)
                        {
                            OnConnected();
                        }
                        newConnection = true;
                    }
                }
            }
            catch (Exception e_)
            {
                mConnected = false;
                ProcessError(e_, "client connect to server error!");
            }
            return(mConnected);
        }