Exemplo n.º 1
0
 private void OnConnect()
 {
     mSocket = new Socket(AddressFamily.InterNetwork, 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;
     if (mBaseNetworkStream != null)
     {
         mBaseNetworkStream.Dispose();
         mBaseNetworkStream = null;
     }
     if (mSslStream != null)
     {
         mSslStream.Dispose();
         mSslStream = null;
     }
     mBaseNetworkStream = new PipeStream(BufferPool, this.LittleEndian, this.Encoding)
     {
         Socket         = mSocket,
         Encoding       = this.Encoding,
         LittleEndian   = this.LittleEndian,
         FlashCompleted = OnWriterFlash
     };
     if (this.Packet != null)
     {
         this.Packet           = this.Packet.Clone();
         this.Packet.Completed = this.OnPacketCompleted;
     }
     if (SSL)
     {
         mBaseNetworkStream.SSL = true;
         mSslStream             = new SslStreamX(BufferPool, this.Encoding, this.LittleEndian, mBaseNetworkStream, new RemoteCertificateValidationCallback(ValidateServerCertificate));
         var task = mSslStream.AuthenticateAsClientAsync(SslServiceName);
         task.Wait();
         mBaseNetworkStream.SSLConfirmed = true;
         mSslStream.SyncData();
     }
     mConnected = true;
     if (mConnected)
     {
         this.Connected?.Invoke(this);
     }
 }
Exemplo n.º 2
0
        private async Task OnConnect()
        {
            var task = Task.Run(() => CreateSocket());

            if (!task.Wait(ConnectTimeOut))
            {
                mSocket?.Dispose();
                throw new TimeoutException($"connect {mIPAddress}@{mPort} timeout! task status:{task.Status}");
            }
            //if (LocalEndPoint == null)
            //    LocalEndPoint = mSocket.LocalEndPoint;
            mSocket.ReceiveTimeout = TimeOut;
            mSocket.SendTimeout    = TimeOut;
            if (mBaseNetworkStream != null)
            {
                mBaseNetworkStream.Dispose();
                mBaseNetworkStream = null;
            }
            if (mSslStream != null)
            {
                mSslStream.Dispose();
                mSslStream = null;
            }
            mBaseNetworkStream                = new Buffers.PipeStream(BufferPool, this.LittleEndian, this.Encoding);
            mBaseNetworkStream.Socket         = mSocket;
            mBaseNetworkStream.Encoding       = this.Encoding;
            mBaseNetworkStream.LittleEndian   = this.LittleEndian;
            mBaseNetworkStream.FlashCompleted = OnWriterFlash;
            if (this.Packet != null)
            {
                this.Packet           = this.Packet.Clone();
                this.Packet.Completed = this.OnPacketCompleted;
            }
            if (SSL)
            {
                mBaseNetworkStream.SSL = true;
                mSslStream             = new SslStreamX(BufferPool, this.Encoding, this.LittleEndian, mBaseNetworkStream, new RemoteCertificateValidationCallback(ValidateServerCertificate));
                //var task = mSslStream.AuthenticateAsClientAsync(SslServiceName);
                //task.Wait();
                await OnSslAuthenticate(mSslStream);

                mBaseNetworkStream.SSLConfirmed = true;
                mSslStream.SyncData(null);
            }
            mConnected = true;
            if (mConnected)
            {
                this.Connected?.Invoke(this);
            }
        }
Exemplo n.º 3
0
 public bool Connect()
 {
     if (IsConnected)
     {
         return(true);
     }
     try
     {
         lock (this)
         {
             if (!IsConnected)
             {
                 mLastError = null;
                 if (mBaseNetworkStream != null)
                 {
                     mBaseNetworkStream.Dispose();
                     mBaseNetworkStream = null;
                 }
                 if (mSslStream != null)
                 {
                     mSslStream.Dispose();
                     mSslStream = null;
                 }
                 mSocket = new Socket(AddressFamily.InterNetwork, 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();
                 if (mReceiveEventArgs != null)
                 {
                     mReceiveEventArgs.Dispose();
                 }
                 mReceiveEventArgs            = new SocketAsyncEventArgsX();
                 mReceiveEventArgs.Completed += IO_Completed;
                 if (mSendEventArgs != null)
                 {
                     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();
                     mBaseNetworkStream.SSLConfirmed = true;
                     mSslStream.SyncData();
                 }
                 BeginReceive();
                 if (IsConnected)
                 {
                     OnConnected();
                 }
             }
         }
     }
     catch (Exception e_)
     {
         mConnected = false;
         ProcessError(e_, "client connect to server error!");
     }
     return(mConnected);
 }
Exemplo n.º 4
0
 public void SyncSSLData()
 {
     mBaseNetStream.SSLConfirmed = true;
     mSslStream.SyncData(SslReceive);
 }