Exemple #1
0
        /*
         *
         * private bool DoForwardProxy(Socket client, SocketEndPoint socket_ep)
         * {
         *  socket_ep = AuthenticationProxy(client);
         *  int error_count = 0;
         *  while (error_count < 5)
         *  {
         *      try
         *      {
         *          byte[] proxy_header = ReceiveProxyHeader(client);
         *          HB32Header recv_header;
         *          byte[] recv_bytes;
         *
         *          switch ((ProxyHeader)proxy_header[1])
         *          {
         *              case ProxyHeader.SendHeader:
         *                  SocketIO.ReceiveBytes(client, out recv_header, out recv_bytes);
         *                  socket_ep.SendHeader(recv_header);
         *                  if (recv_header.Flag == SocketPacketFlag.DisconnectRequest)
         *                  {
         *                      DisposeClient(client, socket_ep);
         *                      return true;
         *                  }
         *                  break;
         *              case ProxyHeader.SendBytes:
         *                  SocketIO.ReceiveBytes(client, out recv_header, out recv_bytes);
         *                  socket_ep.SendBytes(recv_header, recv_bytes);
         *                  break;
         *              case ProxyHeader.ReceiveBytes:
         *                  socket_ep.ReceiveBytes(out recv_header, out recv_bytes);
         *                  SocketIO.SendBytes(client, recv_header, recv_bytes);
         *                  break;
         *          }
         *
         *          error_count = 0;
         *      }
         *      catch (SocketException ex)
         *      {
         *          error_count++;
         *          switch (ex.ErrorCode)
         *          {
         *              // 远程 client 主机关闭连接
         *              case 10054:
         *                  DisposeClient(client, socket_ep);
         *                  Log("Connection closed (client closed). " + ex.Message, LogLevel.Info);
         *                  return false;
         *              // Socket 超时
         *              case 10060:
         *                  Thread.Sleep(200);
         *                  Log("Socket timeout. " + ex.Message, LogLevel.Trace);
         *                  continue;
         *              default:
         *                  Log("Server receive data :" + ex.Message, LogLevel.Warn);
         *                  continue;
         *          }
         *      }
         *      catch (Exception ex)
         *      {
         *          error_count++;
         *          if (ex.Message.Contains("Buffer receive error: cannot receive package"))
         *          {
         *              DisposeClient(client, socket_ep);
         *              Log(ex.Message, LogLevel.Trace);
         *              return false;
         *          }
         *          if (ex.Message.Contains("Invalid socket header"))
         *          {
         *              DisposeClient(client, socket_ep);
         *              Log("Connection closed : " + ex.Message, LogLevel.Warn);
         *              return false;
         *          }
         *          Log("Server exception :" + ex.Message, LogLevel.Warn);
         *          Thread.Sleep(200);
         *          continue;
         *      }
         *  }
         *  return false;
         * }
         */



        /// <summary>
        /// 完成创建 Socket 过程身份认证的代理过程
        /// </summary>
        /// <param name="client"></param>
        /// <returns>已与Server或下级代理连接成功的 SocketEndPoint 对象</returns>
        private SocketSender AuthenticationProxy(Socket client)
        {
            // todo 重写 21.05.28
            byte[] proxy_header;
            SocketIO.ReceiveBytes(client, out HB32Header route_header, out byte[] route_bytes);
            Debug.Assert(route_bytes[0] == 1);
            int             pt    = 0;
            ConnectionRoute route = ConnectionRoute.FromBytes(route_bytes, ref pt);

            byte[] key_bytes = new byte[route_bytes.Length - pt];
            Array.Copy(route_bytes, pt, key_bytes, 0, key_bytes.Length);
            SocketSender proxy_client = new SocketSender(null, route.IsNextNodeProxy);

            byte[] bytes_to_send = new byte[0]; // todo
            // todo 异常处理
            if (route.NextNode.Address.Equals(this.HostAddress))
            {
                /// 下级为挂载在此的反向代理
                /// 利用与反向代理的长连接新建 socket 并通信
            }
            else
            {
                /// 下级需正向代理
                proxy_client.Connect(route.NextNode.Address, Config.SocketSendTimeout, Config.SocketReceiveTimeout);
            }
            proxy_client.SendBytes(route_header, bytes_to_send);
            if (proxy_client.IsRequireProxyHeader)
            {
                ReceiveProxyHeader(client);
            }
            proxy_client.ReceiveBytes(out HB32Header auth_header, out byte[] auth_bytes);
            //SocketIO.SendBytes(client, auth_header, auth_bytes);
            return(proxy_client);



            /*
             *
             *
             * GetAimInfo(route_bytes, out TCPAddress AimAddress, out bool IsAimProxy, out byte[] AimBytes);
             * SocketClient socket_client = new SocketClient(AimAddress);
             * socket_client.IsWithProxy = IsAimProxy;
             * try
             * {
             *  socket_client.Connect(Config.SocketSendTimeOut, Config.SocketReceiveTimeOut);
             *  socket_client.SendBytes(route_header, AimBytes);
             * }
             * catch(Exception ex)
             * {
             *  proxy_header = ReceiveProxyHeader(client);
             *  this.SendBytes(client, SocketPacketFlag.AuthenticationException, "Proxy connect to server failed.");
             *  throw new Exception("Cannot connect to server : " + ex.Message);
             * }
             * proxy_header = ReceiveProxyHeader(client);
             * socket_client.ReceiveBytes(out HB32Header auth_header, out byte[] auth_bytes);
             * this.SendBytes(client, auth_header, auth_bytes);
             * return socket_client;
             */
        }
Exemple #2
0
 public void ReceiveBytes(out HB32Header header, out byte[] bytes)
 {
     if (IsRequireProxyHeader)
     {
         client.Send(new byte[2] {
             0xA3, (byte)ProxyHeader.ReceiveBytes
         });
     }
     /// Receive 的数据仍有一个空的ProxyHeader, 应处理后再接收数据
     ReceiveProxyHeader();
     SocketIO.ReceiveBytes(client, out header, out bytes);
 }
Exemple #3
0
 /// <summary>
 /// 在 SocketProxy 中调用, 对于socket 在 ReceiveProxyHeader 后调用
 /// </summary>
 /// <param name="header"></param>
 /// <param name="bytes"></param>
 public void ReceiveBytesWithoutProxyHeader(out HB32Header header, out byte[] bytes)
 {
     SocketIO.ReceiveBytes(client, out header, out bytes);
 }