core
AbstractBytesWorker.cs 字节工作器(基类),用于用于同一不同功能的字节工作器
BinaryHand.cs 2进制处理器. ThDispose.cs 处理回收相关
crc entity
ThPersonInfo.cs
manager
ThSocketManager.cs ThSocketManagerBusiness.cs
所有的业务
request
RequestCode.cs 请求码
ThProtocolReq.cs 请求逻辑
ThReqBytesWorker.cs 请求相关的字节工作器
response
respLogic
ThProtocolResp.cs 处理服务器响应的数据.
ThProtocolRespDelegates.cs 所有的代理.用于通知客户的事件.
ThProtocolRespEvents.cs 所有的事件.用于调用客户的.
ThProtocolRespListeners.cs 所有的监听器,用于控制事件如何订阅
ThProtocolRespLogic.cs 处理服务器的数据
ThRespBytesWorker.cs 响应字节处理器
BinaryMessageHandler.cs 处理数据包粘结,包一次数据不足等情况.
ResponseCode.cs 响应码
socket
TAsyncTcpClient.cs tcpClient类,read异步.
testcase
===============================================================
部分类代码: BinaryMessageHandler
- #pragma warning disable 0219
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
-
- class BinaryMessageHandler : ThDispose
- {
- List<byte> bytesList = new List<byte>();
-
- private TAsyncTcpClient tcpClient;
-
- public BinaryMessageHandler(TAsyncTcpClient tcpClient)
- {
- this.tcpClient = tcpClient;
- }
- public BinaryMessageHandler()
- {
-
- }
-
- override public void SelfDispose()
- {
- tcpClient = null;
- bytesList = null;
- }
-
-
-
-
-
-
- public void Write(byte[] buf)
- {
- if (buf.Length > 0)
- {
-
- bytesList.AddRange(buf);
- byte[] bytes = bytesList.ToArray<byte>();
- MemoryStream ms = new MemoryStream(bytes);
- BinaryReader reader = new BinaryReader(ms);
-
- int header = reader.ReadUInt16();
- if (header == ThSocketManager.TH_HEADER)
- {
- int len = reader.ReadUInt16();
- int remainLen = len - 4;
- if ((ms.Length - ms.Position) >= remainLen)
- {
-
- ms.Position = 0;
- byte[] pack = reader.ReadBytes(len);
-
- ReadPackage(pack);
-
- bytesList.RemoveRange(0, len);
- }
- }
- reader.Close();
- ms.Close();
- }
-
- }
-
-
-
-
-
-
- public void ReadPackage(byte[] bytes)
- {
-
- MemoryStream ms = new MemoryStream(bytes);
- ms.Position = 0;
- BinaryReader reader = new BinaryReader(ms, Encoding.UTF8);
- ushort header = reader.ReadUInt16();
- ushort totalLen = reader.ReadUInt16();
- ushort respCode = reader.ReadUInt16();
- short signature = reader.ReadInt16();
- int dataLen = totalLen - ThSocketManager.PREFIX_LENGTH;
- byte[] dataBytes = reader.ReadBytes(dataLen);
- reader.Close();
- ms.Close();
-
-
- tcpClient.thProtocolResp.ResponseHandler(respCode, dataBytes);
- }
- }
BinaryHand
- #pragma warning disable 0219
- using System.Text;
- using System.IO;
-
- class BinaryHand
- {
-
-
-
-
-
-
- public static byte[] ToBytes(ushort requestCode, uint clientId, byte[] dataBytes)
- {
- MemoryStream ms = new MemoryStream();
- BinaryWriter writer = new BinaryWriter(ms);
-
- writer.Write(ThSocketManager.TH_HEADER);
-
- ushort packageLen = ThSocketManager.PREFIX_LENGTH;
- if (dataBytes != null)
- {
- packageLen += (ushort)dataBytes.Length;
- }
- writer.Write(packageLen);
-
- writer.Write(requestCode);
-
- writer.Write((short)0);
-
-
-
- if (dataBytes != null)
- writer.Write(dataBytes);
-
- byte[] tmpBytes = ms.ToArray();
- short signature = CRC16.Compute(tmpBytes);
- long oldPos = ms.Position;
- ms.Position = 6;
- writer.Write(signature);
- ms.Position = oldPos;
-
- byte[] bytes = ms.ToArray();
-
- writer.Close();
- ms.Close();
- return bytes;
- }
-
- public static byte[] ToBytes(RequestCode requestCode, uint clientId, byte[] dataBytes)
- {
- return ToBytes((ushort)requestCode, clientId, dataBytes);
- }
-
- public byte[] ToBytes(uint clientId, string data)
- {
- byte[] dataBytes = Encoding.UTF8.GetBytes(data);
- return ToBytes(RequestCode.None, clientId, dataBytes);
- }
-
-
-
-
-
-
- public byte[] FromBytes(byte[] bytes)
- {
- MemoryStream ms = new MemoryStream(bytes);
- ms.Position = 0;
- BinaryReader reader = new BinaryReader(ms, Encoding.UTF8);
- ushort header = reader.ReadUInt16();
- ushort totalLen = reader.ReadUInt16();
- ushort protocolId = reader.ReadUInt16();
- short signature = reader.ReadInt16();
- uint clientId = reader.ReadUInt32();
- int dataLen = totalLen - ThSocketManager.PREFIX_LENGTH;
- byte[] dataBytes = reader.ReadBytes(dataLen);
-
- reader.Close();
- ms.Close();
- return dataBytes;
- }
- }
|
请发表评论