You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
176 lines
4.5 KiB
176 lines
4.5 KiB
/*
|
|
Sog 游戏基础库
|
|
2016 by zouwei
|
|
*/
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Sog.Gate
|
|
{
|
|
|
|
|
|
public enum GateMsgOpCode
|
|
{
|
|
None = 0,
|
|
|
|
// trans msg to one session in connected stat
|
|
Trans = 1,
|
|
|
|
// trans msg to some session
|
|
TransMulti = 2,
|
|
|
|
// client connected
|
|
ConnectedReq = 3,
|
|
ConnectedRes = 4,
|
|
|
|
DisconnectedReq = 5,
|
|
DisconnectedRes = 6,
|
|
|
|
//route packet to other logic server
|
|
Route = 7,
|
|
|
|
//notify gate client count to logic server
|
|
ClientCount = 8,
|
|
|
|
}
|
|
|
|
public static class GateMsgDefine
|
|
{
|
|
public const int TransMultiSessionCountMax = 1000;
|
|
}
|
|
|
|
public struct GateMsgHeader
|
|
{
|
|
public byte OpCode;
|
|
public long SessionID;
|
|
}
|
|
|
|
public struct GateMsgMultiSessionHeader
|
|
{
|
|
public int Count;
|
|
unsafe public fixed long SessionIDs[GateMsgDefine.TransMultiSessionCountMax];
|
|
|
|
public int GetLength()
|
|
{
|
|
return 8*Count + 4;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public static class GateMsgHeaderPacker
|
|
{
|
|
public static int GetLength()
|
|
{
|
|
return 9;
|
|
}
|
|
|
|
public static byte[] Pack(ref GateMsgHeader header)
|
|
{
|
|
byte[] packetBytes = new byte[GetLength()];
|
|
|
|
packetBytes[0] = header.OpCode;
|
|
Span<byte> pbuffer = new Span<byte>(packetBytes,1,8);
|
|
BitConverter.TryWriteBytes(pbuffer,header.SessionID);
|
|
|
|
return packetBytes;
|
|
}
|
|
|
|
public static void Pack(ref GateMsgHeader header, byte[] packetBytes)
|
|
{
|
|
packetBytes[0] = header.OpCode;
|
|
Span<byte> pbuffer = new Span<byte>(packetBytes, 1, 8);
|
|
BitConverter.TryWriteBytes(pbuffer, header.SessionID);
|
|
|
|
return;
|
|
}
|
|
|
|
public static byte[] Pack(ref GateMsgHeader header, ref GateMsgMultiSessionHeader multiSession)
|
|
{
|
|
byte[] packetBytes = new byte[GetLength() + multiSession.GetLength()];
|
|
|
|
packetBytes[0] = header.OpCode;
|
|
Span<byte> pbuffer = new Span<byte>(packetBytes, 1, 8);
|
|
BitConverter.TryWriteBytes(pbuffer, header.SessionID);
|
|
|
|
pbuffer = new Span<byte>(packetBytes, 9, 4);
|
|
BitConverter.TryWriteBytes(pbuffer, multiSession.Count);
|
|
int pos = 13;
|
|
|
|
unsafe{
|
|
fixed (long* psession = multiSession.SessionIDs) {
|
|
for (int i = 0; i < multiSession.Count; i++) {
|
|
pbuffer = new Span<byte>(packetBytes, pos, 8);
|
|
pos += 8;
|
|
BitConverter.TryWriteBytes(pbuffer, psession[i]);
|
|
}
|
|
}
|
|
}
|
|
return packetBytes;
|
|
}
|
|
|
|
public static void UnPack(byte[] data, ref GateMsgHeader header)
|
|
{
|
|
header.OpCode = data[0];
|
|
header.SessionID = BitConverter.ToInt64(data,1);
|
|
|
|
return;
|
|
}
|
|
|
|
public static void RawSetGateMsgHeadOpCode(byte[] data, GateMsgOpCode opCode)
|
|
{
|
|
data[0] = (byte)opCode;
|
|
}
|
|
|
|
public static void UnPackMultiSessionHeader(byte[] data, ref GateMsgMultiSessionHeader header)
|
|
{
|
|
int pos = GetLength();
|
|
header.Count = BitConverter.ToInt32(data,pos);
|
|
pos += 4;
|
|
unsafe
|
|
{
|
|
fixed (long* psession = header.SessionIDs)
|
|
{
|
|
for (int i = 0; i < header.Count; i++)
|
|
{
|
|
psession[i] = BitConverter.ToInt64(data, pos);
|
|
pos += 8;
|
|
}
|
|
}
|
|
}
|
|
|
|
return ;
|
|
}
|
|
}
|
|
|
|
public class GateClientConnectedReq
|
|
{
|
|
public string IPAddress;
|
|
|
|
public byte[] Pack()
|
|
{
|
|
byte[] ipBytes = System.Text.Encoding.UTF8.GetBytes(IPAddress);
|
|
byte[] packetBytes = new byte[ipBytes.Length + 1];
|
|
|
|
packetBytes[0] = (byte)ipBytes.Length;
|
|
Buffer.BlockCopy(ipBytes, 0, packetBytes, 1, ipBytes.Length);
|
|
|
|
return packetBytes;
|
|
}
|
|
|
|
public static GateClientConnectedReq ParseFrom(byte[] data, int offset)
|
|
{
|
|
byte iplength = data[offset];
|
|
|
|
string ipstr = System.Text.Encoding.UTF8.GetString(data, offset + 1, iplength);
|
|
|
|
GateClientConnectedReq req = new GateClientConnectedReq();
|
|
req.IPAddress = ipstr;
|
|
|
|
return req;
|
|
}
|
|
}
|
|
}
|
|
|