Exemplo n.º 1
0
        public void CreateSSL(AsyncCallback asyncCallback, ListenHandler listen, IServer server)
        {
            try
            {
                if (server.EnableLog(EventArgs.LogType.Info))
                {
                    server.Log(EventArgs.LogType.Info, null, $"{RemoteEndPoint} create ssl stream");
                }
                mBaseNetStream.SSL = true;
                mSslStream         = new SslStreamX(this.SendBufferPool, server.Options.Encoding,
                                                    server.Options.LittleEndian, mBaseNetStream, false);
#if (NETSTANDARD2_0)
                mSslStream.BeginAuthenticateAsServer(Server.Certificate, new AsyncCallback(asyncCallback),
                                                     new Tuple <TcpSession, SslStream>(this, this.mSslStream));
#else
                mSslStream.BeginAuthenticateAsServer(listen.Certificate, false, true, new AsyncCallback(asyncCallback),
                                                     new Tuple <TcpSession, SslStream>(this, this.mSslStream));
#endif
            }
            catch (Exception e_)
            {
                if (server.EnableLog(EventArgs.LogType.Warring))
                {
                    server.Error(e_, this, "{1} create session ssl error {0}", e_.Message, this.RemoteEndPoint);
                }
                this.Dispose();
            }
        }
Exemplo n.º 2
0
 public void DisConnect()
 {
     mConnected = false;
     try
     {
         Token = null;
         if (mSocket != null)
         {
             CloseSocket(mSocket);
             mSocket = null;
         }
         if (mSslStream != null)
         {
             mSslStream.Dispose();
             mSslStream = null;
         }
         if (mBaseNetworkStream != null)
         {
             mBaseNetworkStream.Dispose();
             mBaseNetworkStream = null;
         }
     }
     catch
     {
     }
 }
Exemplo n.º 3
0
 private void OnConnect()
 {
     mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     mSocket.Connect(mIPAddress, mPort);
     mSocket.ReceiveTimeout = TimeOut;
     mSocket.SendTimeout    = TimeOut;
     if (mBaseNetworkStream != null)
     {
         mBaseNetworkStream.Dispose();
     }
     if (mSslStream != null)
     {
         mSslStream.Dispose();
     }
     mBaseNetworkStream                = new Buffers.PipeStream(ClientBufferPool.Pool, 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)
     {
         mSslStream = new SslStreamX(ClientBufferPool.Pool, this.Encoding, this.LittleEndian, mBaseNetworkStream, new RemoteCertificateValidationCallback(ValidateServerCertificate));
         var task = mSslStream.AuthenticateAsClientAsync(SslServiceName);
         task.Wait();
     }
     mConnected = true;
 }
Exemplo n.º 4
0
 public void DisConnect()
 {
     lock (this)
     {
         mConnected = false;
         try
         {
             OnDisconnected();
             Token = null;
             if (mSocket != null)
             {
                 TcpClient.CloseSocket(mSocket);
                 mSocket = null;
             }
             mReceiveEventArgs.Dispose();
             mReceiveEventArgs = null;
             mSendEventArgs.Dispose();
             mSendEventArgs = null;
             mProperties.Clear();
             if (mBaseNetworkStream != null)
             {
                 mBaseNetworkStream.Dispose();
                 mBaseNetworkStream = null;
             }
             if (mSslStream != null)
             {
                 mSslStream.Dispose();
                 mSslStream = null;
             }
             object item = DequeueSendMessage();
             while (item != null)
             {
                 if (item is IBuffer)
                 {
                     Buffers.Buffer.Free(((IBuffer)item));
                 }
                 else if (item is IBuffer[])
                 {
                     foreach (IBuffer b in (IBuffer[])item)
                     {
                         Buffers.Buffer.Free(b);
                     }
                 }
                 item = DequeueSendMessage();
             }
         }
         catch
         {
         }
     }
     if (awaitPipeStream.Pending)
     {
         awaitPipeStream.Error(new SocketException((int)SocketError.ConnectionAborted));
     }
     if (mReadMessageAwait.Pending)
     {
         mReadMessageAwait.Error(new SocketException((int)SocketError.ConnectionAborted));
     }
 }
Exemplo n.º 5
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);
            }
        }
Exemplo n.º 6
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.º 7
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.º 8
0
        public static PipeStream ToPipeStream(this Stream stream)
        {
            PipeStream result = stream as PipeStream;

            if (result != null)
            {
                return(result);
            }
            else
            {
                SslStreamX sslStramX = stream as SslStreamX;
                if (sslStramX != null)
                {
                    return(sslStramX.GetPipeStream());
                }
                throw new BXException("invalid cast to PipeStream!");
            }
        }
Exemplo n.º 9
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);
        }