//消息分发 public void DispatchMsgEvent(NetMsgBase msg) { // Debug.Log("接收协议" + msg.MsgType.ToString()); if (msg.MsgType == eNetMsgType.FRAME) { NetFrameMsg realMSg = (NetFrameMsg)msg; if (realMSg == null) { Debug.Log("协议 error"); return; } GameMain.GetInstance().logicManager.AddNewFrame(realMSg.frame); } else if (msg.MsgType == eNetMsgType.SYS) { Debug.Log("sys init"); NetSysMsg realMSg = (NetSysMsg)msg; GameMain.GetInstance().logicManager.Init(realMSg.localPid); } //string name = protocol.GetName(); //Debug.Log("分发处理消息 " + name); //if (eventDict.ContainsKey(name)) //{ // eventDict[name](protocol); //} //if (onceDict.ContainsKey(name)) //{ // onceDict[name](protocol); // onceDict[name] = null; // onceDict.Remove(name); //} }
//消息处理 private void ProcessData() { if (buffCount < sizeof(Int32)) { return; } Array.Copy(readBuff, lenBytes, sizeof(Int32)); msgLength = BitConverter.ToInt32(lenBytes, 0); if (buffCount < msgLength + sizeof(Int32)) { return; } ByteBuffer buffer = new ByteBuffer(readBuff, sizeof(Int32), msgLength); NetMsgBase msg = ReadAndDecode(buffer); msgDist.msgList.Enqueue(msg); int count = buffCount - msgLength - sizeof(Int32); //使用循环队列避免无意义的复制开销 Array.Copy(readBuff, sizeof(Int32) + msgLength, readBuff, 0, count); buffCount = count; if (buffCount > 0) { ProcessData(); } }
static int _CreateTCPSocketMsg(IntPtr L) { try { int count = LuaDLL.lua_gettop(L); if (count == 0) { TCPSocketMsg obj = new TCPSocketMsg(); ToLua.PushObject(L, obj); return(1); } else if (count == 2 && TypeChecker.CheckTypes <ushort, NetMsgBase>(L, 1)) { ushort arg0 = (ushort)LuaDLL.lua_tonumber(L, 1); NetMsgBase arg1 = (NetMsgBase)ToLua.ToObject(L, 2); TCPSocketMsg obj = new TCPSocketMsg(arg0, arg1); ToLua.PushObject(L, obj); return(1); } else { return(LuaDLL.luaL_throw(L, "invalid arguments to ctor method: TCPSocketMsg.New")); } } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
public void PushSendMsg(NetMsgBase msg) { lock (sendQueue) { sendQueue.Enqueue(msg); } }
public override void ProcessEvent(MsgBase msg) { if (callBack != null) { //网络数据 if (msg.GetState() != 127) { NetMsgBase tmpBase = (NetMsgBase)msg; byte[] proto = tmpBase.GetProtoBuffer(); if (proto != null && proto.Length > 0) { LuaByteBuffer buffer = new LuaByteBuffer(proto); callBack.Call(true, tmpBase.msgId, tmpBase.GetState(), buffer); } else { callBack.Call(true, tmpBase.msgId, tmpBase.GetState()); } } else { // 其它消息 //Debug.Log("122222222222221tmpBase.GetState()==" + msg.GetState()); callBack.Call(false, msg); } } }
public void PutSendMsgToPool(NetMsgBase msg) { lock (sendMsgPool) { sendMsgPool.Enqueue(msg); } }
public void PushRecvMsg(NetMsgBase msg) { lock (recvQueue) { recvQueue.Enqueue(msg); } }
public override void ProcessEvent(MsgBase msg) { if (callback == null) { return; } // 从网络来的msg 和从框架其他模块来的数据不一样 if (msg.GetState() != 127) // 从网络来的 { NetMsgBase netMsg = (NetMsgBase)msg; byte[] proto = netMsg.GetProtoBuffer(); // 转成lua能识别的字符串 LuaByteBuffer buffer = new LuaByteBuffer(proto); // 第一个参数代表是否是网络来的 callback.Call(true, netMsg.msgID, netMsg.GetState(), buffer); } else // 从框架来的 { callback.Call(false, msg); } }
private void TcpLoginMsgButtonClick() { Login login = new Login(); login.UserName = "******"; login.PassWord = "******"; byte[] bodys = login.ToByteArray(); string strs = ""; for (int i = 0; i < bodys.Length; i++) { strs = strs + " " + bodys[i]; } int bodycount = bodys.Length; byte[] bodycountbytes = BitConverter.GetBytes(bodycount); byte[] headbackMsgbytes = BitConverter.GetBytes((ushort)TCPEvent.TcpBackLoginMsg); NetMsgBase ba = new NetMsgBase(FrameTools.CombomBinaryArray(bodycountbytes, FrameTools.CombomBinaryArray(headbackMsgbytes, bodys))); TCPMsg msg = new TCPMsg((ushort)TCPEvent.TcpSendLoginMsg, ba); SendMsg(msg); }
/// <summary> /// 接收消息 /// </summary> public void RecvUpdate() { while (recvMsgPool.Count > 0) { NetMsgBase tmpMsg = recvMsgPool.Dequeue(); AnalyseData(tmpMsg); } }
public void Update() { if (recvMsgPool != null) { while (recvMsgPool.Count > 0) { NetMsgBase tmpBase = recvMsgPool.Dequeue(); AnalyseData(tmpBase); } } }
private void ReceiveCallBack(bool isSuccess, SocketError errorType, string exception, byte[] msgBytes, string str) { if (isSuccess) { NetMsgBase msg = new NetMsgBase(msgBytes); PushRecvMsg(msg); } else {//处理接收消息发生错误 } }
public void Update() { if (recvQueue != null) { if (recvQueue.Count > 0) { NetMsgBase msg = recvQueue.Dequeue(); AnalysisMsg(msg); } } }
public void Update() { if (recvMsgPool != null) { while (recvMsgPool.Count > 0) { NetMsgBase tmp = recvMsgPool.Dequeue(); MsgCenter.instance.SendToMsg(tmp); } } }
void AsysnRecvCallBack(bool sucess, ErrorSocket error, string exception, byte[] byteMessage, string strMsg) { if (sucess) { NetMsgBase tmp = new NetMsgBase(byteMessage); } else { //处理错误信息 } }
public void Update() { if (recvMsgPool != null) { while (recvMsgPool.Count > 0) { NetMsgBase tmp = recvMsgPool.Dequeue(); AnalyseData(tmp); } } //clientSocket.Recive(); }
private void TcpSendMsgButtonClick() { string body = "body content = asdfasdf"; byte[] data = Encoding.Default.GetBytes(body); int bodycount = body.Length; byte[] bodycountbytes = BitConverter.GetBytes(bodycount); byte[] headbackMsgbytes = BitConverter.GetBytes((ushort)TCPEvent.TcpSendMsgBack); NetMsgBase ba = new NetMsgBase(FrameTools.CombomBinaryArray(bodycountbytes, FrameTools.CombomBinaryArray(headbackMsgbytes, data))); TCPMsg msg = new TCPMsg((ushort)TCPEvent.TcpSendMsg, ba); SendMsg(msg); }
/// <summary> /// 不断发送消息给服务端 /// </summary> private void LoodSendMsg() { while (clientSocket != null && clientSocket.IsConnect()) { lock (sendMsgPool) { while (sendMsgPool.Count > 0) { NetMsgBase body = sendMsgPool.Dequeue(); clientSocket.AsyncSend(body.GetNetBytes(), CallBackSend); } } Thread.Sleep(100); } }
void LoopSendMsg() { while (clientSocket != null && clientSocket.isConnected()) { lock (sendMsgPool) { while (sendMsgPool.Count > 0) { NetMsgBase tmpBody = sendMsgPool.Dequeue(); clientSocket.AsynSend(tmpBody.GetNetBytes(), CallBackSend); } } Thread.Sleep(100); } }
void LoopSendMsg() { while (clientSocket.isConnect()) { lock (sendQueue) { while (sendQueue.Count > 0) { NetMsgBase msg = sendQueue.Dequeue(); clientSocket.SendAsyn(msg.GetNetBytes(), SendCallBack); } } Thread.Sleep(100); } }
private void LoopSendMsg() { while (clientSocket != null && clientSocket.IsConnect) { lock (sendMsgPool) { while (sendMsgPool.Count > 0) { NetMsgBase tmpSendMsg = sendMsgPool.Dequeue(); clientSocket.AsyncSend(tmpSendMsg.GetNetBytes(), OnSendCallback); } } Thread.Sleep(100); } }
static int ChangerMsg(IntPtr L) { try { ToLua.CheckArgsCount(L, 3); TCPSocketMsg obj = (TCPSocketMsg)ToLua.CheckObject <TCPSocketMsg>(L, 1); ushort arg0 = (ushort)LuaDLL.luaL_checknumber(L, 2); NetMsgBase arg1 = (NetMsgBase)ToLua.CheckObject <NetMsgBase>(L, 3); obj.ChangerMsg(arg0, arg1); return(0); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
static int get_netMsg(IntPtr L) { object o = null; try { o = ToLua.ToObject(L, 1); TCPSocketMsg obj = (TCPSocketMsg)o; NetMsgBase ret = obj.netMsg; ToLua.PushObject(L, ret); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index netMsg on a nil value" : e.Message)); } }
static int set_netMsg(IntPtr L) { object o = null; try { o = ToLua.ToObject(L, 1); TCPSocketMsg obj = (TCPSocketMsg)o; NetMsgBase arg0 = (NetMsgBase)ToLua.CheckObject <NetMsgBase>(L, 2); obj.netMsg = arg0; return(0); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index netMsg on a nil value" : e.Message)); } }
public override void ProcessEvent(MsgBase tmpMsg) { if (callBack != null) { //从网络来 if (tmpMsg.GetState() != 127) { NetMsgBase tmpBase = (NetMsgBase)tmpMsg; byte[] proto = tmpBase.GetProtoBuffer(); LuaByteBuffer buffer = new LuaByteBuffer(proto); callBack.Call(true, tmpBase.msgId, tmpBase.GetState(), buffer); } else //从框架其他模块来的 { callBack.Call(false, tmpMsg); } } }
public override void ProcessEvent(MsgBase tmpMsg) { switch (tmpMsg.msgId) { case (ushort)TCPEvent.TcpSendMsgBack: Debug.Log("TcpSendMsgBack"); break; case (ushort)TCPEvent.TcpBackLoginMsg: NetMsgBase tCPMsg = (NetMsgBase)tmpMsg; IMessage IMLogin = new Login(); Login login = new Login(); login = (Login)IMLogin.Descriptor.Parser.ParseFrom(tCPMsg.GetBodyBytes()); Debug.Log("login.UserName = "******"login.PassWord = " + login.PassWord); break; } }
public override void ProcessEvent(MsgBase msg) { //从网络来的msg和从框架来的数据不一样 if (luaFunctionCallBack != null) { //从网络来的msg if (msg.GetState() != 127) { NetMsgBase netMsgBase = msg as NetMsgBase; byte[] proto = netMsgBase.GetProtoBuffer(); LuaByteBuffer buffer = new LuaByteBuffer(proto); luaFunctionCallBack.Call(true, netMsgBase.msgId, netMsgBase.GetState(), buffer); } else { luaFunctionCallBack.Call(false, msg); } } }
public TCPMsg(ushort tmpId, NetMsgBase tmpBase) { this.msgId = tmpId; this.netMsg = tmpBase; }
void PutRecvMsgToPool(byte[] recvMsg) { NetMsgBase tmp = new NetMsgBase(recvMsg); recvMsgPool.Enqueue(tmp); }
void AnalysisMsg(NetMsgBase msg) { MsgCenter.Instance.HandleMsg(msg); }