private void AcceptClient(object obj) { try { var listenSocket = (Socket)obj; listenSocket.Listen(int.MaxValue); while (!this._isDisposed) { try { var client = listenSocket.Accept(); lock (this._isDisposedLock) { if (this._isDisposed) { this.DisposeSocket(client); break; } lock (this._waitAckTcpTransferChannelListLock) { var tcpTransferChannel = new TcpTransferChannel(client, this.ClientDisconnectNotify, this._rev, this.IdAck); this._waitAckTcpTransferChannelList.Add(tcpTransferChannel); tcpTransferChannel.Start(); } } } catch (ObjectDisposedException) { break; } catch (ThreadAbortException) { break; } catch (SocketException se) { if (se.ErrorCode == 10004) { break; } } catch (Exception ex) { Loger.Error(ex, "listenSocket.Accept异常"); } } } catch (ObjectDisposedException) { } catch (ThreadAbortException) { } catch (Exception ex) { Loger.Error(ex); } }
private void ClientDisconnectNotify(TcpTransferChannel tcpTransferChannel) { lock (_htTcpTransferChannel.SyncRoot) { var id = this.GetId(tcpTransferChannel.IpEndPoint); if (_htTcpTransferChannel.ContainsKey(id)) { ((TcpTransferChannel)_htTcpTransferChannel[id]).Dispose(); _htTcpTransferChannel.Remove(id); } } }
private void IdAck(TcpTransferChannel tcpTransferChannel) { lock (this._waitAckTcpTransferChannelListLock) { this._waitAckTcpTransferChannelList.Remove(tcpTransferChannel); } lock (_htTcpTransferChannel.SyncRoot) { var id = this.GetId(tcpTransferChannel.IpEndPoint); if (_htTcpTransferChannel.ContainsKey(id)) { ((TcpTransferChannel)_htTcpTransferChannel[id]).Dispose(); } _htTcpTransferChannel[id] = tcpTransferChannel; } }
private TcpTransferChannel ConnetDst(IPEndPoint remoteEP, string id) { TcpTransferChannel tcpTransferChannel; Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { client.Connect(remoteEP); tcpTransferChannel = new TcpTransferChannel(client, remoteEP, this.ClientDisconnectNotify, this._rev); tcpTransferChannel.Start(); var buffer = new byte[8]; var listenEP = this._config.ListenEP; byte[] ipBytes = listenEP.Address.GetAddressBytes(); using (var ms = new MemoryStream(buffer)) { var bw = new BinaryWriter(ms); bw.Write(ipBytes[0]); bw.Write(ipBytes[1]); bw.Write(ipBytes[2]); bw.Write(ipBytes[3]); bw.Write(listenEP.Port); bw.Flush(); } tcpTransferChannel.SendData(buffer); } catch (Exception) { client.Close(); client.Dispose(); throw; } return(tcpTransferChannel); }