Exemplo n.º 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++;
            }
        }
Exemplo n.º 2
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());
     }
 }
Exemplo n.º 3
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()));
             * }
             */
        }
Exemplo n.º 4
0
 public static byte[] GetBytes(HB32Header header)
 {
     return(header.GetBytes());
 }