实例介绍
【实例简介】
【实例截图】
【核心代码】
using cn.org.hentai.tentacle.util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace cn.org.hentai.tentacle.protocol
{
public class Packet
{
// 实际数据长度(头部 数据体)
int size = 0;
// 当前读写指针索引位置
int offset = 0;
// 包数据体最大大小
int maxSize = 0;
// 头部 数据体 可能冗余的空间
public byte[] data;
private Packet()
{
// do nothing here..
}
public static Packet from(byte[] data)
{
Packet packet = new Packet();
packet.data = data;
packet.size = data.Length;
return packet;
}
public static Packet read(Stream stream, int bytesInBuffer)
{
if (bytesInBuffer < 11) return null;
byte[] head = new byte[11];
int len = stream.Read(head, 0, 11);
int dataLength = ByteUtil.getInt(head, 7, 4) & 0x7fffff;
using (MemoryStream ms = new MemoryStream(dataLength 10))
{
byte[] buff = new byte[512];
for (int i = 0; i < dataLength; i = len)
{
len = stream.Read(buff, 0, Math.Min(512, dataLength - i));
if (len == -1) break;
ms.Write(buff, 0, len);
}
Packet p = new Packet();
p.data = new byte[dataLength 6 1 4];
p.size = 0;
p.maxSize = p.size;
p.addBytes(head);
p.addBytes(ms.ToArray());
buff = null;
head = null;
return p;
}
}
/**
* 创建协议数据包
* @param command 指令,参见cn.org.hentai.tentacle.protocol.Command类
* @param length 数据包的长度
* @return
*/
public static Packet create(byte command, int length)
{
Packet p = new Packet();
p.data = new byte[length 6 1 4];
p.data[0] = (byte)'H';
p.data[1] = (byte)'E';
p.data[2] = (byte)'N';
p.data[3] = (byte)'T';
p.data[4] = (byte)'A';
p.data[5] = (byte)'I';
p.data[6] = command;
Array.Copy(ByteUtil.toBytes(length), 0, p.data, 7, 4);
p.size = 11;
p.maxSize = length;
return p;
}
public int getSize()
{
return this.size;
}
public Packet addByte(byte b)
{
this.data[size ] = b;
return this;
}
public Packet addShort(short s)
{
this.data[size ] = (byte)((s >> 8) & 0xff);
this.data[size ] = (byte)(s & 0xff);
return this;
}
public Packet addInt(int i)
{
this.data[size ] = (byte)((i >> 24) & 0xff);
this.data[size ] = (byte)((i >> 16) & 0xff);
this.data[size ] = (byte)((i >> 8) & 0xff);
this.data[size ] = (byte)(i & 0xff);
return this;
}
public Packet addLong(long l)
{
this.data[size ] = (byte)((l >> 56) & 0xff);
this.data[size ] = (byte)((l >> 48) & 0xff);
this.data[size ] = (byte)((l >> 40) & 0xff);
this.data[size ] = (byte)((l >> 32) & 0xff);
this.data[size ] = (byte)((l >> 24) & 0xff);
this.data[size ] = (byte)((l >> 16) & 0xff);
this.data[size ] = (byte)((l >> 8) & 0xff);
this.data[size ] = (byte)(l & 0xff);
return this;
}
public Packet addBytes(byte[] b)
{
Array.Copy(b, 0, this.data, size, b.Length);
size = b.Length;
return this;
}
public Packet rewind()
{
this.offset = 0;
return this;
}
public byte nextByte()
{
return this.data[offset ];
}
public short nextShort()
{
return (short)(((this.data[offset ] & 0xff) << 8) | (this.data[offset ] & 0xff));
}
public int nextInt()
{
return (this.data[offset ] & 0xff) << 24 | (this.data[offset ] & 0xff) << 16 | (this.data[offset ] & 0xff) << 8 | (this.data[offset ] & 0xff);
}
public long nextLong()
{
return ((long)this.data[offset ] & 0xff) << 56
| ((long)this.data[offset ] & 0xff) << 48
| ((long)this.data[offset ] & 0xff) << 40
| ((long)this.data[offset ] & 0xff) << 32
| ((long)this.data[offset ] & 0xff) << 24
| ((long)this.data[offset ] & 0xff) << 16
| ((long)this.data[offset ] & 0xff) << 8
| ((long)this.data[offset ] & 0xff);
}
public byte[] nextBytes(int length)
{
byte[] buf = new byte[length];
Array.Copy(this.data, offset, buf, 0, length);
offset = length;
return buf;
}
public Packet skip(int offset)
{
this.offset = offset;
return this;
}
public Packet seek(int index)
{
this.offset = index;
return this;
}
public byte[] getBytes()
{
if (size == maxSize) return this.data;
else
{
byte[] buff = new byte[size];
Array.Copy(this.data, 0, buff, 0, size);
return buff;
}
}
}
}
好例子网口号:伸出你的我的手 — 分享!
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


支持(0) 盖楼(回复)