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; }
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)); } }
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); } }
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); } }
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); } }
protected virtual void OnDispose() { try { object data = DequeueSendMessage(); while (data != null) { if (data is IBuffer buffer) { BeetleX.Buffers.Buffer.Free(buffer); //((IBuffer)data).Free(); } data = DequeueSendMessage(); } if (SendEventArgs != null) { SendEventArgs.Dispose(); } if (ReceiveEventArgs != null) { ReceiveEventArgs.Dispose(); } mReceiveArgs.Server = null; mReceiveArgs.Session = null; mBaseNetStream.Dispose(); if (mSslStream != null) { mSslStream.Dispose(); } Server.CloseSession(this); Server = null; ReceiveDispatcher = null; if (Packet != null) { Packet.Dispose(); } mProperties.Clear(); if (SendBufferPool is PrivateBufferPool sendPool) { sendPool.Dispose(); } if (ReceiveBufferPool is PrivateBufferPool receivePool) { receivePool.Dispose(); } } catch { } }
protected virtual void OnDispose() { try { object data = DequeueSendMessage(); while (data != null) { if (data is IBuffer buffer) { BeetleX.Buffers.Buffer.Free(buffer); } data = DequeueSendMessage(); } SendEventArgs?.Dispose(); SendEventArgs?.Clear(); ReceiveEventArgs?.Dispose(); ReceiveEventArgs?.Clear(); mReceiveArgs.Server = null; mReceiveArgs.Session = null; mBaseNetStream.Dispose(); if (mSslStream != null) { mSslStream.Dispose(); } Server.CloseSession(this); ListenHandler = null; ReceiveDispatcher = null; if (Packet != null) { Packet.Dispose(); } ((IDisposable)Tag)?.Dispose(); OnDisposeProperties(); ((IDisposable)mToken)?.Dispose(); } catch { } finally { Tag = null; mToken = null; } }
protected virtual void OnDispose() { try { object data = DequeueSendMessage(); while (data != null) { if (data is IBuffer) { ((IBuffer)data).Free(); } data = DequeueSendMessage(); } if (SendEventArgs != null) { SendEventArgs.Dispose(); } if (ReceiveEventArgs != null) { ReceiveEventArgs.Dispose(); } mReceiveArgs.Server = null; mReceiveArgs.Session = null; mBaseNetStream.Dispose(); if (mSslStream != null) { mSslStream.Dispose(); } Server.CloseSession(this); Server = null; ReceiveDispatcher = null; if (Packet != null) { Packet.Dispose(); } mProperties.Clear(); } catch { } }
protected virtual void OnDispose() { try { object data = DequeueSendMessage(); while (data != null) { if (data is IBuffer buffer) { BeetleX.Buffers.Buffer.Free(buffer); //((IBuffer)data).Free(); } data = DequeueSendMessage(); } SendEventArgs?.Dispose(); SendEventArgs?.Clear(); ReceiveEventArgs?.Dispose(); ReceiveEventArgs?.Clear(); mReceiveArgs.Server = null; mReceiveArgs.Session = null; mBaseNetStream.Dispose(); if (mSslStream != null) { mSslStream.Dispose(); } Server.CloseSession(this); //Server = null; ListenHandler = null; ReceiveDispatcher = null; if (Packet != null) { Packet.Dispose(); } mProperties.Clear(); } catch { } }