public bool Send(FSPFrameData frame) { if (!IsActived()) { Debuger.LogWarning("Session已经不活跃了!"); return(false); } tempSendData.frame = frame; int len = ProtoBuffUtility.Serialize(tempSendData, sendBufferTemp); return(kcp.Send(sendBufferTemp, len) == 0); }
public bool SendFSP(int clientFrameID, int cmd, int[] args) { if (isRunning) { FSPMessage msg = tempSendData.msg; msg.cmd = cmd; msg.clientFrameID = clientFrameID; msg.args = args; int length = ProtoBuffUtility.Serialize(tempSendData, sendBufferTemp); kcp.Send(sendBufferTemp, length); return(length > 0); } return(false); }
private void OnReceive(byte[] bytes, int len) { NetMessage msg = new NetMessage(); msg.Deserialize(bytes, len); if (msg.Head.cmd == 0) { RPCMessage rpcmsg = ProtoBuffUtility.Deserialize <RPCMessage>(msg.content); HandleRPCMessage(rpcmsg); } else { HandlePrptoMessage(msg); } }
private void HandleRPCMessage(ISession session, RPCMessage rpcMessage) { RPCMethodHelper helper = rpcManager.GetMethodHelper(rpcMessage.name); if (helper != null) { object[] args = new object[rpcMessage.rawargs.Count + 1]; var rawargs = rpcMessage.rawargs; var paramInfo = helper.method.GetParameters(); args[0] = session; if (args.Length == paramInfo.Length) { for (int i = 0; i < rawargs.Count; i++) { if (rawargs[i].type == enRPCArgType.PBObject) { args[i + 1] = ProtoBuffUtility.Deserialize(paramInfo[i + 1].ParameterType, rawargs[i].rawValue); } else { args[i + 1] = rawargs[i].value; } } currInvokingName = rpcMessage.name; currInvokingSession = session; try { helper.method.Invoke(helper.listener, BindingFlags.NonPublic, null, args, null); } catch (Exception e) { Debuger.LogError("RPC调用出错:{0} : {1}\n{2}", rpcMessage.name, e.Message, e.StackTrace); } currInvokingName = null; currInvokingSession = null; } else { Debuger.LogWarning("参数数量不一致!{0}", rpcMessage.name); } } else { Debuger.LogWarning("RPC不存在!{0}", rpcMessage.name); } }
public void Invoke(int dstID, string name, params object[] args) { RPCMessage rpcmsg = new RPCMessage { name = name, args = args }; IPCMessage msg = new IPCMessage { srcID = ipcInfo.ID, rpcMessage = rpcmsg }; byte[] temp = ProtoBuffUtility.Serialize(msg); SendMessage(dstID, temp, temp.Length); }
public void ReturnError(string errinfo, int errcode = 1) { var name = "On" + currInvokingName + "Error"; RPCMessage rpcmsg = new RPCMessage { name = name, args = new object[] {errinfo, errcode} }; IPCMessage msg = new IPCMessage { srcID = ipcInfo.ID, rpcMessage = rpcmsg }; byte[] temp = ProtoBuffUtility.Serialize(msg); SendMessage(currInvokingSrc, temp, temp.Length); }
private void HandleMessage(IPCMessage msg) { RPCMessage rpcmsg = msg.rpcMessage; var helper = rpcManager.GetMethodHelper(rpcmsg.name); if (helper != null) { object[] args = new object[rpcmsg.args.Length +1]; List<RPCArg> rawargs = rpcmsg.rawargs; ParameterInfo[] paramInfo = helper.method.GetParameters(); if (args.Length == paramInfo.Length) { for (int i = 0; i < rawargs.Count; i++) { if (rawargs[i].type == enRPCArgType.PBObject) { args[i + 1] = ProtoBuffUtility.Deserialize(paramInfo[i + 1].ParameterType, rawargs[i].rawValue); } else { args[i + 1] = rawargs[i].value; } } args[0] = msg.srcID; currInvokingName = rpcmsg.name; currInvokingSrc = msg.srcID; try { helper.method.Invoke(helper.listener, BindingFlags.NonPublic, null, args, null); } catch (Exception e) { Debuger.LogError("RPC调用出错:{0}\n{1}", e.Message, e.StackTrace); } currInvokingName = ""; currInvokingSrc = 0; } else { Debuger.LogWarning("参数数量不一致!"); } } else { Debuger.LogWarning("RPC不存在!"); } }
public void Return(params object[] args) { var name = "On" + currInvokingName; RPCMessage rpcmsg = new RPCMessage { name = name, args = args }; IPCMessage msg = new IPCMessage { srcID = ipcInfo.ID, rpcMessage = rpcmsg }; byte[] temp = ProtoBuffUtility.Serialize(msg); SendMessage(currInvokingSrc, temp, temp.Length); }
public void Send <TReq>(uint cmd, TReq req) { NetMessage meaasge = new NetMessage { Head = { index = 0, cmd = cmd, uid = uid }, content = ProtoBuffUtility.Serialize(req) }; meaasge.Head.dataSize = (ushort)meaasge.content.Length; byte[] temp; int len = meaasge.Serialize(out temp); connection.Send(temp, len); }
private void HandleRPCMessage(RPCMessage rpcmsg) { Debuger.Log("Connection[{0}]-> {1}({2})", connection.ID, rpcmsg.name, rpcmsg.args); var helper = rpcManager.GetMethodHelper(rpcmsg.name); if (helper != null) { object[] args = rpcmsg.args; var rawargs = rpcmsg.rawargs; var paramInfo = helper.method.GetParameters(); if (rawargs.Count == paramInfo.Length) { for (int i = 0; i < rawargs.Count; i++) { if (rawargs[i].type == enRPCArgType.PBObject) { var type = paramInfo[i].ParameterType; object arg = ProtoBuffUtility.Deserialize(type, rawargs[i].rawValue); args[i] = arg; } } currInvokingName = rpcmsg.name; try { helper.method.Invoke(helper.listener, BindingFlags.NonPublic, null, args, null); } catch (Exception e) { Debuger.LogError("RPC调用出错:{0}: {1}\n{2}", rpcmsg.name, e.Message, e.StackTrace); } currInvokingName = null; } else { Debuger.LogWarning("参数数量不一致!{0}", rpcmsg.name); } } else { Debuger.LogWarning("RPC不存在!{0} ", rpcmsg.name); } }
public void Send <MsgType>(ISession session, uint index, uint cmd, MsgType msg) { NetMessage msgobj = new NetMessage { Head = { index = index, cmd = cmd, uid = session.uid }, content = ProtoBuffUtility.Serialize(msg) }; msgobj.Head.dataSize = (ushort)msgobj.content.Length; byte[] tmp; int len = msgobj.Serialize(out tmp); session.Send(tmp, len); }
//一问一答方式 public void Send <TReq, TRsp>(uint cmd, TReq req, Action <TRsp> successHandle, float timeOut = 30, Action <enNetErrorCode> errorHandle = null) { NetMessage message = new NetMessage { Head = { index = MessageIndexGenerator.GetNextMessageIndex(), cmd = cmd, uid = uid }, content = ProtoBuffUtility.Serialize(req) }; message.Head.dataSize = (ushort)message.content.Length; byte[] temp; int len = message.Serialize(out temp); connection.Send(temp, len); AddListener(cmd, successHandle, message.Head.index, timeOut, errorHandle); }
//---------------------------------------------------------------------------- //---------------------------------------------------------------------------- private void HandlePrptoMessage(ISession session, NetMessage msg) { if (listenerHelpers.ContainsKey(msg.Head.cmd)) { var helper = listenerHelpers[msg.Head.cmd]; if (helper != null) { object obj = ProtoBuffUtility.Deserialize(helper.typeMsg, msg.content); if (obj != null) { helper.handleMsg.DynamicInvoke(session, msg.Head.index, obj); } } else { Debuger.LogWarning("未找到对应的监听者! cmd:{0}", msg.Head.cmd); } } else { Debuger.LogWarning("未找到对应的监听者! cmd:{0}", msg.Head.cmd); } }
public void Invoke(ISession session, string name, params object[] args) { RPCMessage rpcmsg = new RPCMessage { name = name, args = args }; byte[] buffer = ProtoBuffUtility.Serialize(rpcmsg); NetMessage msg = new NetMessage { Head = new ProtocolHead { dataSize = (ushort)buffer.Length }, content = buffer }; byte[] tmp = null; int len = msg.Serialize(out tmp); session.Send(tmp, len); }
public void ReturnError(string errinfo, int errcode = 1) { var name = "On" + currInvokingName + "Error"; RPCMessage rpcmsg = new RPCMessage { name = name, args = new object[] { errinfo, errcode } }; byte[] buffer = ProtoBuffUtility.Serialize(rpcmsg); NetMessage msg = new NetMessage { Head = new ProtocolHead { dataSize = (ushort)buffer.Length }, content = buffer }; byte[] tmp = null; int len = msg.Serialize(out tmp); connection.Send(tmp, len); }
public void Return(params object[] args) { var name = "On" + currInvokingName; RPCMessage rpcmsg = new RPCMessage { name = name, args = args }; byte[] buffer = ProtoBuffUtility.Serialize(rpcmsg); NetMessage msg = new NetMessage { Head = new ProtocolHead { dataSize = (ushort)buffer.Length }, content = buffer }; byte[] tmpBytes = null; int len = msg.Serialize(out tmpBytes); currInvokingSession.Send(tmpBytes, len); }