Exemplo n.º 1
0
 private static void TcpServer_SessionClosed(TcpAppSession session, CSuperSocket.SocketBase.CloseReason value)
 {
     try
     {
         CloseLocalClient(session);
         HandleLog.WriteLine($"客户端【{session.PackJson.UserId},{session.RemoteEndPoint}】已下线:{value}");
     }
     catch (Exception ex)
     {
         HandleLog.WriteLine($"关闭连接【{session.LocalEndPoint}】发生异常:{ex}");
     }
 }
Exemplo n.º 2
0
        private static void TcpServer_NewSessionConnected(TcpAppSession session)
        {
            try
            {
                //转发请求
                var natSession = NATServer.GetSessions(c => c.MapList?.Any(m => m.remote_port == session.LocalEndPoint.Port) ?? false).FirstOrDefault();
                if (natSession == null)
                {
                    session?.Close();
                    HandleLog.WriteLine($"请求:{session.LocalEndPoint}失败,Nat客户端连接不在线!");
                    return;
                }
                var map = natSession.MapList?.Find(c => c.remote_port == session.LocalEndPoint.Port);
                if (map == null)
                {
                    session?.Close();
                    HandleLog.WriteLine($"请求:{session.LocalEndPoint}失败,映射{session.LocalEndPoint}不存在!");
                    return;
                }
                session.Map = map;
                var pack = new PackJson()
                {
                    Host   = map?.remote_endpoint,
                    Local  = map?.local_endpoint,
                    UserId = session.UserId,
                    Method = "TCP"
                };
                session.PackJson = pack;
                var json      = JsonHelper.Instance.Serialize(pack);
                var jsonBytes = Encoding.UTF8.GetBytes(json);
                //03 01 数据长度(4) 正文数据(n)   ---tcp连接注册包
                var sendBytes = new List <byte>()
                {
                    0x3, 0x1
                };
                sendBytes.AddRange(BitConverter.GetBytes(jsonBytes.Length).Reverse());
                sendBytes.AddRange(jsonBytes);
                natSession.Send(sendBytes.ToArray(), 0, sendBytes.Count);

                session.NatSession = natSession;
                HandleLog.WriteLine($"客户端【{session.PackJson.UserId},{session.RemoteEndPoint}】已连接【{session.LocalEndPoint}】");
            }
            catch (Exception ex)
            {
                HandleLog.WriteLine($"连接【{session.PackJson.UserId},{session.LocalEndPoint}】发生异常:{ex}");
            }
        }
Exemplo n.º 3
0
        public static void CloseLocalClient(TcpAppSession session)
        {
            var pack = new PackJson()
            {
                Host   = session.PackJson.Host,
                UserId = session.PackJson.UserId
            };
            var json      = JsonHelper.Instance.Serialize(pack);
            var jsonBytes = Encoding.UTF8.GetBytes(json);
            //03 03 数据长度(4) 正文数据(n)   ---tcp连接关闭包
            var sendBytes = new List <byte>()
            {
                0x3, 0x3
            };

            sendBytes.AddRange(BitConverter.GetBytes(jsonBytes.Length).Reverse());
            sendBytes.AddRange(jsonBytes);
            //转发给客户端
            session.NatSession?.Send(sendBytes.ToArray());
        }
Exemplo n.º 4
0
 private static void TcpServer_NewRequestReceived(TcpAppSession session, TcpRequestInfo requestInfo)
 {
     Task.Run(() =>
     {
         try
         {
             while (session.NatSession == null)
             {
                 Thread.Sleep(50);
             }
             //先gzip压缩  再转为16进制字符串
             var body = DataHelper.Compress(requestInfo.Data);
             var pack = new PackJson()
             {
                 Host    = session.Map?.remote_endpoint,
                 Local   = session.Map?.local_endpoint,
                 UserId  = session.UserId,
                 Method  = "TCP",
                 Content = body
             };
             var json      = JsonHelper.Instance.Serialize(pack);
             var jsonBytes = Encoding.UTF8.GetBytes(json);
             //03 02 数据长度(4) 正文数据(n)   ---tcp响应包
             var sendBytes = new List <byte>()
             {
                 0x3, 0x2
             };
             sendBytes.AddRange(BitConverter.GetBytes(jsonBytes.Length).Reverse());
             sendBytes.AddRange(jsonBytes);
             session.NatSession.Send(sendBytes.ToArray(), 0, sendBytes.Count);
             HandleLog.WriteLine($"<---- {session.PackJson.UserId} 收到报文{requestInfo.Data.Length}字节");
         }
         catch (Exception ex)
         {
             HandleLog.WriteLine($"【{session.LocalEndPoint}】请求参数:{Encoding.UTF8.GetString(requestInfo.Data)},处理发生异常:{ex}");
         }
     });
 }