/// <summary> /// 监听Remote服务端口 /// </summary> public void ListenRemoteServerPort() { EndPoint endPoint = RemoteServerTcp.Client.RemoteEndPoint; Logger.Info.WriteLine("[RemoteServer] 新联入Tcp:{0}", endPoint); NetworkStream readStream = RemoteServerTcp.GetStream(); byte[] buffer = new byte[1024]; int length = 0; while (readStream.CanRead) { length = 0; try { length = readStream.Read(buffer, 0, buffer.Length); } catch (Exception ex) { Logger.Info.WriteLine("[Port]->[RemoteServer] 接收数据错误:\r\n{0}", ex); } if (length > 0) { Logger.Trace.WriteLine("[Port]->[RemoteServer] 接收到数据,长度:{0}", length); TransferRemoteServerDataToServer(buffer, length, RemoteServerTcp); } else { Logger.Trace.WriteLine("[Port]->[RemoteServer] 接收到数据,长度:{0},断开连接!", length); break; } } DoTcpException(TcpErrorType.RemoteServer, string.Format("[RemoteServer] 已断开Tcp:{0}", endPoint)); }
/// <summary> /// 断开本地Client服务Tcp /// </summary> public void BreakRemoteServerTcp() { if (RemoteServerTcp == null) { return; } try { RemoteServerTcp.Close(); } catch { } RemoteServerTcp = null; }
public void HandleRemotePackage(byte type, byte[] data) { switch (type) { case P2PSocketType.Remote.Break.Code: { BreakRemoteServerTcp(); } break; case P2PSocketType.Remote.Error.Code: { BreakRemoteServerTcp(); } break; case P2PSocketType.Remote.Secure.Code: { } break; case P2PSocketType.Remote.ServerName.Code: { } break; case P2PSocketType.Remote.Transfer.Code: { try { RemoteServerTcp.WriteAsync(data); } catch (Exception ex) { DoTcpException(TcpErrorType.RemoteServer, string.Format("[RemoteServer]->[Port] 数据转发错误,长度:{0}\r\n{1}", data.Length, ex)); } } break; } }