/// <summary> /// 发送 Socket 数据包, 过长的 byte流 会被拆成多个包发送 /// 包头的 PacketCount , ByteLength 等参数会视 bytes 长度被修改 /// </summary> /// <param name="socket"></param> /// <param name="header"></param> /// <param name="bytes"></param> protected void SendBytes(Socket socket, HB32Header header, byte[] bytes) { header.PacketCount = ((bytes.Length > 0 ? bytes.Length : 1) - 1) / (HB32Encoding.DataSize) + 1; header.TotalByteLength = bytes.Length; header.PacketIndex = 0; for (int offset = 0; offset < bytes.Length || offset == 0; offset += HB32Encoding.DataSize) { if (bytes.Length - offset < HB32Encoding.DataSize) { header.ValidByteLength = bytes.Length - offset; byte[] _toSend = new byte[HB32Encoding.PacketSize]; Array.Copy(GetHeaderBytesFunc(header), 0, _toSend, 0, HB32Encoding.HeaderSize); Array.Copy(bytes, offset, _toSend, HB32Encoding.HeaderSize, bytes.Length - offset); socket.Send(_toSend); } else { header.ValidByteLength = HB32Encoding.DataSize; byte[] _toSend = new byte[HB32Encoding.PacketSize]; Array.Copy(GetHeaderBytesFunc(header), 0, _toSend, 0, HB32Encoding.HeaderSize); Array.Copy(bytes, offset, _toSend, HB32Encoding.HeaderSize, HB32Encoding.DataSize); socket.Send(_toSend); } /// 若当前数据包之后还有数据包, 在等待对方 发送StreamRequest包头 后发送 if (offset + HB32Encoding.DataSize < bytes.Length) { ReceiveHeader(socket, out _); } header.PacketIndex++; } }
/// <summary> /// Send socket 只发送包头 /// 只有 SendHeader() 会只发送包头 /// SendBytes() 至少会发一个空包 /// </summary> /// <param name="socket"></param> /// <param name="header"></param> protected void SendHeader(Socket socket, HB32Header header) { header.PacketCount = 0; header.TotalByteLength = 0; header.PacketIndex = 0; header.ValidByteLength = 0; socket.Send(GetHeaderBytesFunc(header), HB32Encoding.HeaderSize, SocketFlags.None); }
/// 接收 Socket 数据包, 在接收不定长byte流时使用, 过长byte流会分开接收并拼接成byte数组 /// 收到的数据只有包头时, 返回空byte数组 protected void ReceiveBytes(Socket socket, out HB32Header header, out byte[] bytes) { /// 通过包头判断byte流长度, 确定byte数组大小 包数量 等基本信息 ReceiveHeader(socket, out header); /// 此时 socket 只接收了HB32Header包头长度的字节 /// 对于 SendHeader() 只发送包头的数据 /// 函数会直接返回空byte数组 /* * if (header.Flag == SocketPacketFlag.DownloadPacketRequest || * header.Flag == SocketPacketFlag.UploadPacketResponse) * { * bytes = new byte[0]; * return; * } */ 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, SocketPacketFlag.StreamRequest); /// 读取下一个包头 ReceiveHeader(socket, out header); } } }
public static byte[] GetBytes(HB32Header header) { return(header.GetBytes()); }
/// <summary> /// 尽量不要用, 可以用 ReceiveBytes() 代替 /// Receive Socket 数据包, 在确定接收数据包只有一个时使用, 输出 包头 和 byte数组格式内容 /// </summary> /// <param name="socket"></param> /// <param name="header">输出包头</param> /// <param name="bytes_data"></param> protected 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); }
/// <summary> /// Receive socket 只接收包头 /// </summary> /// <param name="socket"></param> /// <param name="header"></param> protected void ReceiveHeader(Socket socket, out HB32Header header) { byte[] bytes_header = new byte[HB32Encoding.HeaderSize]; ReceiveBuffer(socket, bytes_header); header = HB32Header.ReadFromBytes(bytes_header); }
/// <summary> /// 发送 Socket 数据包, 字符串以 UTF-8 编码后发送 /// </summary> /// <param name="socket"></param> /// <param name="header"></param> /// <param name="str"></param> protected void SendBytes(Socket socket, HB32Header header, string str) { SendBytes(socket, header, Encoding.UTF8.GetBytes(str)); }