Beispiel #1
0
        /*
         *
         * /// <summary>
         * /// Send socket 只发送包头
         * /// 只有 SendHeader() 会只发送包头
         * /// SendBytes() 至少会发一个空包
         * /// </summary>
         * /// <param name="socket"></param>
         * /// <param name="header"></param>
         * private static void SendHeader(Socket socket, HB32Header header)
         * {
         *  SendHeader(socket, header, new byte[0]);
         * }
         *
         *
         * /// <summary>
         * /// Send socket 只发送包头
         * /// </summary>
         * /// <param name="socket"></param>
         * /// <param name="flag"></param>
         * /// <param name="i1"></param>
         * /// <param name="i2"></param>
         * /// <param name="i3"></param>
         * public static void SendHeader(Socket socket, SocketPacketFlag flag, int i1 = 0, int i2 = 0, int i3 = 0)
         * {
         *  SendHeader(socket, new HB32Header
         *  {
         *      Flag = flag,
         *      I1 = i1,
         *      I2 = i2,
         *      I3 = i3
         *  });
         * }
         */



        /// <summary>
        /// 发送 Socket 数据包, 过长的 byte流 会被拆成多个包发送
        /// 包头的 PacketCount , ByteLength 等参数会视 bytes 长度被修改
        /// 拆成多包发送时, 只有第一个包头会带 ProxyHeader 供 SocketProxy 或 SocketServer 解析
        /// 后面的包均为 HB32Header + HB32Data
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="header"></param>
        /// <param name="bytes"></param>
        /// <param name="addition_bytes"></param>
        public static void SendBytes(Socket socket, HB32Header header, byte[] bytes, byte[] addition_bytes)
        {
            header.PacketCount     = Math.Max((bytes.Length - 1) / (HB32Encoding.DataSize) + 1, 1);
            header.TotalByteLength = bytes.Length;
            header.PacketIndex     = 0;
            for (int offset = 0; offset < bytes.Length || offset == 0; offset += HB32Encoding.DataSize)
            {
                header.ValidByteLength = Math.Min(bytes.Length - offset, HB32Encoding.DataSize);
                byte[] header_bytes;
                if (offset == 0)
                {
                    header_bytes = header.GetBytes(addition_bytes);
                }
                else
                {
                    header_bytes = header.GetBytes();
                }
                byte[] _toSend = new byte[header_bytes.Length + HB32Encoding.DataSize];
                Array.Copy(header_bytes, 0, _toSend, 0, header_bytes.Length);
                Array.Copy(bytes, offset, _toSend, header_bytes.Length, header.ValidByteLength);
                socket.Send(_toSend);
                /// 若当前数据包之后还有数据包, 在等待对方 发送2个byte 后发送
                if (offset + HB32Encoding.DataSize < bytes.Length)
                {
                    ReceiveBuffer(socket, new byte[2]);
                    //ReceiveHeader(socket, out _);
                }
                header.PacketIndex++;
            }
        }
Beispiel #2
0
        private SocketSender ReversedConnectionProxy(ref SocketResponder responder, HB32Header route_header, byte[] route_bytes)
        {
            ConnectionRoute route = ConnectionRoute.FromBytes(route_bytes);

            if (route.NextNode.Address.Equals(this.HostAddress))
            {
                if (route.IsNextNodeProxy)
                {
                    // todo: 反向代理
                }
                else
                {
                }


                return(null);
            }
            else
            {
                SocketSender    sender = ConnectionRelay(responder, route_header, route);
                SocketResponder r      = sender.ConvertToResponder();
                SocketSender    s      = responder.ConvertToSender(route_header.I1 > 0);
                responder = r;
                return(s);
            }
        }
Beispiel #3
0
        /// <summary>
        /// 创建完整代理隧道, 向下级发送建立连接是否成功信号, 返回与上级通信的 SocketSender
        /// [上级可能是 Proxy, Server, Reversed Server, Reversed Server's Proxy, etc....]
        /// </summary>
        /// <param name="responder"></param>
        /// <param name="route_bytes"></param>
        /// <returns></returns>
        private SocketSender ForwardConnectionProxy(SocketResponder responder, HB32Header route_header, byte[] route_bytes)
        {
            ConnectionRoute route = ConnectionRoute.FromBytes(route_bytes);

            if (route.NextNode.Address.Equals(this.HostAddress))
            {
                /// 为连接到此的反向代理
                // todo
                // 可以试试 ReadWriteLock
                if (ReversedProxyServers.ContainsKey(route.NextNode.Name))
                {
                    ReversedServerInfo server = ReversedProxyServers[route.NextNode.Name];
                    /// Set EventWaitHandle

                    /// Wait for new connection built from server
                }
                else
                {
                    //todo 没有对应挂载的 Server
                }



                return(null);
            }
            else
            {
                return(ConnectionRelay(responder, route_header, route));
            }
        }
Beispiel #4
0
 public void ReceiveBytes(out HB32Header header, out byte[] bytes)
 {
     if (IsWithProxy)
     {
         client.Send(new byte[2] {
             0xA3, (byte)ProxyHeader.ReceiveBytes
         });
     }
     ReceiveBytes(client, out header, out bytes);
 }
Beispiel #5
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);
 }
Beispiel #6
0
 public void ReceiveBytesWithHeaderFlag(SocketPacketFlag flag, out HB32Header header, out byte[] bytes)
 {
     ReceiveBytes(out header, out bytes);
     if (header.Flag != flag)
     {
         string err_msg = "";
         try
         {
             err_msg = Encoding.UTF8.GetString(bytes);
         }
         catch (Exception) {; }
         throw new ArgumentException(string.Format("[Received not valid header: {0}, required : {1}] -- {2}", header.Flag.ToString(), flag.ToString(), err_msg));
     }
 }
Beispiel #7
0
 public void SendBytes(HB32Header header, byte[] bytes)
 {
     if (IsRequireProxyHeader)
     {
         SocketIO.SendBytes(client, header, bytes, new byte[2] {
             SocketProxy.ProxyHeaderByte, (byte)ProxyHeader.SendBytes
         });
     }
     else
     {
         SocketIO.SendBytes(client, header, bytes, new byte[2] {
             0, 0
         });
     }
 }
Beispiel #8
0
 private byte[] GetHeaderBytesWithProxy(HB32Header header)
 {
     if (IsWithProxy)
     {
         byte[] bytes = new byte[34];
         bytes[0] = 0xA3;
         bytes[1] = (byte)NextProxyHeader;
         Array.Copy(header.GetBytes(), 0, bytes, 2, 32);
         return(bytes);
     }
     else
     {
         return(header.GetBytes());
     }
 }
Beispiel #9
0
        /// <summary>
        /// 调用此方法可以在发送包头前添加 addition_bytes (发送代理包头)
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="header"></param>
        /// <param name="addition_bytes"></param>
        public static void SendHeader(Socket socket, HB32Header header, byte[] addition_bytes)
        {
            header.PacketCount     = 0;
            header.TotalByteLength = 0;
            header.PacketIndex     = 0;
            header.ValidByteLength = 0;
            byte[] bytes = header.GetBytes(addition_bytes);
            socket.Send(bytes);

            /*
             * if (addition_bytes.Length == 0)
             * {
             *  socket.Send(header.GetBytes());
             * }
             * else
             * {
             *  socket.Send(BytesConverter.Concatenate(addition_bytes, header.GetBytes()));
             * }
             */
        }
Beispiel #10
0
        /// <summary>
        /// 接收 Socket 数据包, 在接收不定长byte流时使用, 过长byte流会分开接收并拼接成byte数组
        /// 收到的数据只有包头时, 返回空byte数组
        /// 只能处理去掉 ProxyHeader 后的字节流, 所以 socket 接收数据应先经过 SocketEndPoint 类处理 ProxyHeader
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="header"></param>
        /// <param name="bytes"></param>
        public static void ReceiveBytes(Socket socket, out HB32Header header, out byte[] bytes)
        {
            /// 通过包头判断byte流长度, 确定byte数组大小 包数量 等基本信息
            ReceiveHeader(socket, out header);
            /// 此时 socket 只接收了HB32Header包头长度的字节
            /// 对于 SendHeader() 只发送包头的数据
            /// 函数会直接返回空byte数组
            bytes = new byte[header.TotalByteLength];
            int offset = 0;     // bytes 数组写入起点偏移量

            for (int i = 0; i < header.PacketCount; ++i)
            {
                if (i == header.PacketCount - 1)
                {
                    /// 读取缓冲区中有效数据
                    if (header.ValidByteLength > 0)
                    {
                        ReceiveBuffer(socket, bytes, header.ValidByteLength, offset);
                    }
                    /// 读取缓冲区中剩余的无效数据
                    ReceiveBuffer(socket, new byte[HB32Encoding.DataSize - header.ValidByteLength]);
                }
                else
                {
                    /// 读取缓冲区数据
                    ReceiveBuffer(socket, bytes, header.ValidByteLength, offset);
                    offset += header.ValidByteLength;
                    // /// 发送 StreamRequset header
                    // SendHeader(socket, new HB32Header { Flag = SocketPacketFlag.StreamRequest }, new byte[2]);
                    /// 发送 2 个byte, 获取下一个数据包
                    socket.Send(new byte[2]);
                    /// 读取下一个包头
                    ReceiveHeader(socket, out header);
                }
            }
        }
Beispiel #11
0
 public static byte[] GetBytes(HB32Header header)
 {
     return(header.GetBytes());
 }
Beispiel #12
0
        /*
         * /// <summary>
         * /// 发送 Socket 数据包, 过长的 byte流 会被拆成多个包发送
         * /// 包头的 PacketCount , ByteLength 等参数会视 bytes 长度被修改
         * /// </summary>
         * /// <param name="socket"></param>
         * /// <param name="header"></param>
         * /// <param name="bytes"></param>
         * public static void SendBytes(Socket socket, HB32Header header, byte[] bytes)
         * {
         *  SendBytes(socket, header, bytes, new byte[0]);
         * }
         *
         *
         *
         * /// <summary>
         * /// 发送 Socket 数据包, 字符串以 UTF-8 编码后发送
         * /// </summary>
         * /// <param name="socket"></param>
         * /// <param name="header"></param>
         * /// <param name="str"></param>
         * public static void SendBytes(Socket socket, HB32Header header, string str)
         * {
         *  SendBytes(socket, header, Encoding.UTF8.GetBytes(str));
         * }
         *
         *
         *
         * /// <summary>
         * /// 发送 Socket 数据包
         * /// </summary>
         * /// <param name="socket"></param>
         * /// <param name="flag"></param>
         * /// <param name="bytes"></param>
         * /// <param name="i1"></param>
         * /// <param name="i2"></param>
         * /// <param name="i3"></param>
         * public static void SendBytes(Socket socket, SocketPacketFlag flag, byte[] bytes, int i1 = 0, int i2 = 0, int i3 = 0)
         * {
         *  SendBytes(socket, new HB32Header
         *  {
         *      Flag = flag,
         *      I1 = i1,
         *      I2 = i2,
         *      I3 = i3
         *  }, bytes);
         * }
         *
         * /// <summary>
         * /// 发送 Socket 数据包, 字符串以 UTF-8 编码后发送
         * /// </summary>
         * /// <param name="socket"></param>
         * /// <param name="flag"></param>
         * /// <param name="str"></param>
         * /// <param name="i1"></param>
         * /// <param name="i2"></param>
         * /// <param name="i3"></param>
         * public static void SendBytes(Socket socket, SocketPacketFlag flag, string str, int i1 = 0, int i2 = 0, int i3 = 0)
         * {
         *  SendBytes(socket, flag, Encoding.UTF8.GetBytes(str), i1, i2, i3);
         * }
         */



        /// <summary>
        /// Receive socket 只接收包头, 但不可以处理 ProxyHeader (处理 ProxyHeader 方法应位于 SocketEndPoint 类中)
        /// 此方法只能用于对于字节流的Header部份截取处理
        /// 若要完整接收只含 Header 的字节流, 应使用 ReceiveBytes() 方法
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="header"></param>
        public static void ReceiveHeader(Socket socket, out HB32Header header)
        {
            byte[] bytes_header = new byte[HB32Encoding.HeaderSize];
            ReceiveBuffer(socket, bytes_header);
            header = HB32Header.ReadFromBytes(bytes_header);
        }
Beispiel #13
0
 /// <summary>
 /// 尽量不要用, 可以用 ReceiveBytes() 代替
 /// Receive Socket 数据包, 在确定接收数据包只有一个时使用, 输出 包头 和 byte数组格式内容
 /// 擅用后果自负
 /// </summary>
 /// <param name="socket"></param>
 /// <param name="header">输出包头</param>
 /// <param name="bytes_data"></param>
 public static void ReceivePacket(Socket socket, out HB32Header header, out byte[] bytes_data)
 {
     ReceiveHeader(socket, out header);
     bytes_data = new byte[HB32Encoding.DataSize];
     ReceiveBuffer(socket, bytes_data);
 }
Beispiel #14
0
 /// <summary>
 /// 当上一级代理不是挂载在当前代理上的节点上时, 利用此方法完成同方向代理的中继, 并返回上级代理结果
 /// </summary>
 /// <param name="responder"></param>
 /// <param name="route"></param>
 /// <returns></returns>
 private SocketSender ConnectionRelay(SocketResponder responder, HB32Header header, ConnectionRoute route)
 {
     if (route.IsNextNodeProxy)
     {
         /// 继续正向代理
         SocketSender sender  = new SocketSender(true);
         string       err_msg = "";
         try
         {
             sender.ConnectWithTimeout(route.NextNode.Address, Config.BuildConnectionTimeout);
             sender.SetTimeout(Config.SocketSendTimeout, Config.SocketReceiveTimeout);
         }
         catch (Exception ex)
         {
             /// 当前代理建立连接失败
             err_msg = ex.Message;
         }
         if (string.IsNullOrEmpty(err_msg))
         {
             HB32Header next_header = header.Copy();
             next_header.I1++;
             sender.SendBytes(next_header, route.GetBytes(node_start_index: 1));
             sender.ReceiveBytes(out HB32Header respond_header, out byte[] respond_bytes);
             if ((respond_header.Flag | SocketPacketFlag.ExceptionFlag) == 0)
             {
                 responder.SendHeader(respond_header);
             }
             else
             {
                 /// 上级或更上级代理建立连接失败, header 中包含抛出异常的代理位置
                 responder.SendBytes(respond_header, respond_bytes);
             }
         }
         else
         {
             HB32Header err_header = header.Copy();
             err_header.Flag = (SocketPacketFlag)(((int)err_header.Flag & 0xFFFF00) | 0x90);
             responder.SendBytes(err_header, err_msg);
         }
         return(sender);
     }
     else
     {
         /// 直连 server
         SocketSender sender  = new SocketSender(false);
         string       err_msg = "";
         try
         {
             sender.ConnectWithTimeout(route.ServerAddress.Address, Config.BuildConnectionTimeout);
             sender.SetTimeout(Config.SocketSendTimeout, Config.SocketReceiveTimeout);
         }
         catch (Exception ex)
         {
             err_msg = ex.Message;
         }
         /// response
         if (string.IsNullOrEmpty(err_msg))
         {
             HB32Header resp_header = header.Copy();
             resp_header.Flag = (SocketPacketFlag)(((int)resp_header.Flag & 0xFFFF00) | 0x10);
             responder.SendHeader(resp_header);
         }
         else
         {
             HB32Header err_header = header.Copy();
             err_header.Flag = (SocketPacketFlag)(((int)err_header.Flag & 0xFFFF00) | 0x90);
             responder.SendBytes(err_header, err_msg);
         }
         return(sender);
     }
 }
Beispiel #15
0
 public void SendBytes(HB32Header header, byte[] bytes)
 {
     this.NextProxyHeader = ProxyHeader.SendBytes;
     SendBytes(client, header, bytes);
 }
Beispiel #16
0
 private void LongConnectionRespond(SocketResponder responder, HB32Header route_header, byte[] route_bytes)
 {
     /// LongConnection 中继, 或 将Reversed Server 挂载在此
 }
Beispiel #17
0
 public void ReceiveBytesWithHeaderFlag(SocketPacketFlag flag, out HB32Header header)
 {
     ReceiveBytesWithHeaderFlag(flag, out header, out _);
 }
Beispiel #18
0
 public void SendHeader(HB32Header header)
 {
     this.NextProxyHeader = ProxyHeader.SendHeader;
     SendHeader(client, header);
 }
Beispiel #19
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);
 }
Beispiel #20
0
 public void SendBytes(HB32Header header, string str)
 {
     SendBytes(header, Encoding.UTF8.GetBytes(str));
 }