/// <summary> /// 仅生成ProtoHead /// </summary> public Protocol() { protoHead = new ProtoHead(); serverHead = new ServerHead(); protoBody = null; }
public Protocol(ProtoID id, ProtoBody body) { protoHead = new ProtoHead(); protoHead.CmdID = id; serverHead = new ServerHead(); protoBody = body; }
public int InitSession(string ip, int port, ProtocolType socketType = ProtocolType.Tcp, bool isClient = true) { ProtoBody.RegisterSerializeFunc(SerializeProto, DeserializeProto); string[] ips = ip.Split('.'); byte[] ipAddr = { byte.Parse(ips[0]), byte.Parse(ips[1]), byte.Parse(ips[2]), byte.Parse(ips[3]) }; IPAddress addr = new IPAddress(ipAddr); IPEndPoint endpoint = new IPEndPoint(addr, port); Session session; if (isClient) { session = new ClientSession(); sessionCount++; session.SessionId = sessionCount; session.Init(socketType); ((ClientSession)session).Connect(endpoint); } else { session = new ServerSession(); sessionCount++; session.SessionId = sessionCount; session.Init(socketType); ((ServerSession)session).Listen(endpoint); } allSessions.Add(sessionCount, session); return(sessionCount); }
static int SendTcpServerMessage(IntPtr L) { try { int count = LuaDLL.lua_gettop(L); if (count == 2) { LuaFramework.NetworkManager obj = (LuaFramework.NetworkManager)ToLua.CheckObject <LuaFramework.NetworkManager>(L, 1); Protocols.ProtoBody arg0 = (Protocols.ProtoBody)ToLua.CheckObject <Protocols.ProtoBody>(L, 2); obj.SendTcpServerMessage(arg0); return(0); } else if (count == 3) { LuaFramework.NetworkManager obj = (LuaFramework.NetworkManager)ToLua.CheckObject <LuaFramework.NetworkManager>(L, 1); ushort arg0 = (ushort)LuaDLL.luaL_checknumber(L, 2); byte[] arg1 = ToLua.CheckByteBuffer(L, 3); obj.SendTcpServerMessage(arg0, arg1); return(0); } else { return(LuaDLL.luaL_throw(L, "invalid arguments to method: LuaFramework.NetworkManager.SendTcpServerMessage")); } } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
public void Call(System.IO.Stream param0, Protocols.ProtoBody param1) { func.BeginPCall(); func.PushObject(param0); func.PushObject(param1); func.PCall(); func.EndPCall(); }
/// <summary> /// 生成ProtoHead,ServerHead,ProtoID,并使用传入的ProtoBody赋值 /// </summary> /// <param name="bodyIn"></param> public Protocol(ProtoBody bodyIn) { protoHead = new ProtoHead(); serverHead = new ServerHead(); protoBody = bodyIn; protoHead.CmdID = ProtoID.GetProtoIDByBody(bodyIn); }
public Protocols.ProtoBody Call(System.IO.Stream param0, System.Type param1) { func.BeginPCall(); func.PushObject(param0); func.Push(param1); func.PCall(); Protocols.ProtoBody ret = (Protocols.ProtoBody)func.CheckObject(typeof(Protocols.ProtoBody)); func.EndPCall(); return(ret); }
/// <summary> /// 协议体类型转换为协议ID /// </summary> /// <param name="body"></param> /// <returns></returns> public static ProtoID GetProtoIDByBody(ProtoBody body) { Type type = body.GetType(); ProtoID pID; if (typeMap.TryGetValue(type, out pID)) { return(pID); } Debug.LogWarning("没有ID号的ProtoBody : {0}", body.GetType().Name); return(CMD_STATIC_NULL); }
static int SendMessage(IntPtr L) { try { int count = LuaDLL.lua_gettop(L); if (count == 3 && TypeChecker.CheckTypes <Protocols.Protocol>(L, 3)) { Protocols.NetworkController obj = (Protocols.NetworkController)ToLua.CheckObject <Protocols.NetworkController>(L, 1); int arg0 = (int)LuaDLL.luaL_checknumber(L, 2); Protocols.Protocol arg1 = (Protocols.Protocol)ToLua.ToObject(L, 3); obj.SendMessage(arg0, arg1); return(0); } else if (count == 3 && TypeChecker.CheckTypes <Protocols.ProtoBody>(L, 3)) { Protocols.NetworkController obj = (Protocols.NetworkController)ToLua.CheckObject <Protocols.NetworkController>(L, 1); int arg0 = (int)LuaDLL.luaL_checknumber(L, 2); Protocols.ProtoBody arg1 = (Protocols.ProtoBody)ToLua.ToObject(L, 3); obj.SendMessage(arg0, arg1); return(0); } else if (count == 3 && TypeChecker.CheckTypes <System.IO.Stream>(L, 3)) { Protocols.NetworkController obj = (Protocols.NetworkController)ToLua.CheckObject <Protocols.NetworkController>(L, 1); int arg0 = (int)LuaDLL.luaL_checknumber(L, 2); System.IO.Stream arg1 = (System.IO.Stream)ToLua.ToObject(L, 3); obj.SendMessage(arg0, arg1); return(0); } else if (count == 4) { Protocols.NetworkController obj = (Protocols.NetworkController)ToLua.CheckObject <Protocols.NetworkController>(L, 1); int arg0 = (int)LuaDLL.luaL_checknumber(L, 2); ushort arg1 = (ushort)LuaDLL.luaL_checknumber(L, 3); byte[] arg2 = ToLua.CheckByteBuffer(L, 4); obj.SendMessage(arg0, arg1, arg2); return(0); } else { return(LuaDLL.luaL_throw(L, "invalid arguments to method: Protocols.NetworkController.SendMessage")); } } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
static int SetBody(IntPtr L) { try { ToLua.CheckArgsCount(L, 2); Protocols.Protocol obj = (Protocols.Protocol)ToLua.CheckObject <Protocols.Protocol>(L, 1); Protocols.ProtoBody arg0 = (Protocols.ProtoBody)ToLua.CheckObject <Protocols.ProtoBody>(L, 2); obj.SetBody(arg0); return(0); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
static int GetBody(IntPtr L) { try { ToLua.CheckArgsCount(L, 1); Protocols.Protocol obj = (Protocols.Protocol)ToLua.CheckObject <Protocols.Protocol>(L, 1); Protocols.ProtoBody o = obj.GetBody(); ToLua.PushObject(L, o); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
static int GetProtoIDByBody(IntPtr L) { try { ToLua.CheckArgsCount(L, 1); Protocols.ProtoBody arg0 = (Protocols.ProtoBody)ToLua.CheckObject <Protocols.ProtoBody>(L, 1); Protocols.ProtoID o = Protocols.ProtoID.GetProtoIDByBody(arg0); ToLua.PushObject(L, o); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
static int _CreateProtocols_ProtoBody(IntPtr L) { try { int count = LuaDLL.lua_gettop(L); if (count == 0) { Protocols.ProtoBody obj = new Protocols.ProtoBody(); ToLua.PushObject(L, obj); return(1); } else { return(LuaDLL.luaL_throw(L, "invalid arguments to ctor method: Protocols.ProtoBody.New")); } } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
static int _CreateProtocols_Protocol(IntPtr L) { try { int count = LuaDLL.lua_gettop(L); if (count == 0) { Protocols.Protocol obj = new Protocols.Protocol(); ToLua.PushObject(L, obj); return(1); } else if (count == 1) { Protocols.ProtoBody arg0 = (Protocols.ProtoBody)ToLua.CheckObject <Protocols.ProtoBody>(L, 1); Protocols.Protocol obj = new Protocols.Protocol(arg0); ToLua.PushObject(L, obj); return(1); } else if (count == 2) { Protocols.ProtoID arg0 = (Protocols.ProtoID)ToLua.CheckObject <Protocols.ProtoID>(L, 1); Protocols.ProtoBody arg1 = (Protocols.ProtoBody)ToLua.CheckObject <Protocols.ProtoBody>(L, 2); Protocols.Protocol obj = new Protocols.Protocol(arg0, arg1); ToLua.PushObject(L, obj); return(1); } else { return(LuaDLL.luaL_throw(L, "invalid arguments to ctor method: Protocols.Protocol.New")); } } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
//[12/3/2014 史耀力] TODO:此处性能需要优化! public int Deserialize(System.IO.Stream stream) { //LogSys.Warn("[PROTO] Begin Deser len {0}, position {1}", stream.Length, stream.Position); long initPos = stream.Position; if (protoHead.Deserialize(stream) < 0) { stream.Position = initPos; return(-1); } Stream bodyStream = stream; //长度过长,需要分包 if ((stream.Length - stream.Position) > protoHead.BodyLen) { bodyStream = new MemoryStream(); if (0 != protoHead.BodyLen) { long curPos = stream.Position; //stream.CopyTo(bodyStream); int len = (int)stream.Length; byte[] buffer = new byte[len]; stream.Read(buffer, 0, len); bodyStream.Write(buffer, 0, len); //修正位置 stream.Position = curPos + protoHead.BodyLen; bodyStream.SetLength(protoHead.BodyLen); bodyStream.Position = 0; } } //长度不够 else if ((stream.Length - stream.Position) < protoHead.BodyLen) { stream.Position = initPos; return(-1); } if (protoHead.CmdID.BodyType == null) //typeof(LuaProtoBody)) { protoBody = new LuaProtoBody(); byte[] byteArray = new byte[bodyStream.Length - bodyStream.Position]; bodyStream.Read(byteArray, 0, byteArray.Length); //转成bytearray //((LuaProtoBody)protoBody).byteArray = ((MemoryStream)bodyStream).ToArray(); ((LuaProtoBody)protoBody).byteArray = byteArray; } else { protoBody = ProtoBody.Deserialize(bodyStream, protoHead.CmdID.BodyType); } //if (!isClient) //{ // protoBody.BodyRoleID = serverHead.roleID; //} //LogSys.Warn("[PROTO] End Deser len {0}, position {1}", stream.Length, stream.Position); return(0); }
//发送协议 public int SendMessage(ProtoBody body) { Protocol proto = new Protocol(body); return(SendMessage(proto)); }
static int SendTcpClientMessage(IntPtr L) { try { int count = LuaDLL.lua_gettop(L); if (count == 2) { LuaFramework.NetworkManager obj = (LuaFramework.NetworkManager)ToLua.CheckObject <LuaFramework.NetworkManager>(L, 1); Protocols.ProtoBody arg0 = (Protocols.ProtoBody)ToLua.CheckObject <Protocols.ProtoBody>(L, 2); obj.SendTcpClientMessage(arg0); return(0); } else if (count == 3 && TypeChecker.CheckTypes <Protocols.ProtoID, Protocols.ProtoBody>(L, 2)) { LuaFramework.NetworkManager obj = (LuaFramework.NetworkManager)ToLua.CheckObject <LuaFramework.NetworkManager>(L, 1); Protocols.ProtoID arg0 = (Protocols.ProtoID)ToLua.ToObject(L, 2); Protocols.ProtoBody arg1 = (Protocols.ProtoBody)ToLua.ToObject(L, 3); obj.SendTcpClientMessage(arg0, arg1); return(0); } else if (count == 3 && TypeChecker.CheckTypes <ushort, byte[]>(L, 2)) { LuaFramework.NetworkManager obj = (LuaFramework.NetworkManager)ToLua.CheckObject <LuaFramework.NetworkManager>(L, 1); ushort arg0 = (ushort)LuaDLL.lua_tonumber(L, 2); byte[] arg1 = ToLua.CheckByteBuffer(L, 3); obj.SendTcpClientMessage(arg0, arg1); return(0); } else if (count == 3 && TypeChecker.CheckTypes <ushort, LuaFramework.ByteBuffer>(L, 2)) { LuaFramework.NetworkManager obj = (LuaFramework.NetworkManager)ToLua.CheckObject <LuaFramework.NetworkManager>(L, 1); ushort arg0 = (ushort)LuaDLL.lua_tonumber(L, 2); LuaFramework.ByteBuffer arg1 = (LuaFramework.ByteBuffer)ToLua.ToObject(L, 3); obj.SendTcpClientMessage(arg0, arg1); return(0); } else if (count == 4) { LuaFramework.NetworkManager obj = (LuaFramework.NetworkManager)ToLua.CheckObject <LuaFramework.NetworkManager>(L, 1); Protocols.ProtoID arg0 = (Protocols.ProtoID)ToLua.CheckObject <Protocols.ProtoID>(L, 2); Protocols.ProtoBody arg1 = (Protocols.ProtoBody)ToLua.CheckObject <Protocols.ProtoBody>(L, 3); System.Action arg2 = (System.Action)ToLua.CheckDelegate <System.Action>(L, 4); obj.SendTcpClientMessage(arg0, arg1, arg2); return(0); } else if (count == 5) { LuaFramework.NetworkManager obj = (LuaFramework.NetworkManager)ToLua.CheckObject <LuaFramework.NetworkManager>(L, 1); Protocols.ProtoID arg0 = (Protocols.ProtoID)ToLua.CheckObject <Protocols.ProtoID>(L, 2); Protocols.ProtoBody arg1 = (Protocols.ProtoBody)ToLua.CheckObject <Protocols.ProtoBody>(L, 3); System.Action arg2 = (System.Action)ToLua.CheckDelegate <System.Action>(L, 4); bool arg3 = LuaDLL.luaL_checkboolean(L, 5); obj.SendTcpClientMessage(arg0, arg1, arg2, arg3); return(0); } else { return(LuaDLL.luaL_throw(L, "invalid arguments to method: LuaFramework.NetworkManager.SendTcpClientMessage")); } } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
void SerializeProto(Stream stream, ProtoBody body) { ProtoBuf.Serializer.NonGeneric.Serialize(stream, body); }
public void SendMessage(int sessionId, ProtoBody body) { Protocol proto = new Protocol(body); SendMessage(sessionId, proto); }
public void SetBody(ProtoBody body) { protoBody = body; protoHead.CmdID = ProtoID.GetProtoIDByBody(body); }