public void OnReceiveMsgFromNet(byte[] bytes) { // Debug.Log("NetMsgHandler:OnReceiveMsgFromNet : "+bytes.Length.ToString()); int content_size = bytes.Length - 5; if (content_size <= 0) { return; } char flag = BitConverter.ToChar(bytes, content_size); int cur_session = BitConverter.ToInt32(bytes, content_size + 1); cur_session = Util.SwapInt32(cur_session); // Debug.Log("NetMsgHandler:OnReceiveMsgFromNet flag:"+flag.ToString()+" session:"+cur_session.ToString()); RpcRspHandler rpcRspHandler = NetMsgDispatcher.GetHandler(cur_session); if (rpcRspHandler != null) { SprotoTypeBase receive_info = null; ProtocolFunctionDictionary.typeFunc GenResponse; sessionDict.TryGetValue(cur_session, out GenResponse); // if (flag == 1) // { receive_info = GenResponse(bytes, 0, content_size); // } // else // { // receive_info = GenResponse(null, 0, 0); // } rpcRspHandler(receive_info); } }
public void SendMessage <T>(SprotoTypeBase rpcReq, RpcRspHandler rpcRspHandler = null) { session += 1; if (session >= maxSession) { session = 0; } if (rpcRspHandler != null) { AddHandler(session, rpcRspHandler); } byte[] message = rpcReq.encode(); MemoryStream ms = null; using (ms = new MemoryStream()) { ms.Position = 0; BinaryWriter writer = new BinaryWriter(ms); UInt16 msglen = Util.SwapUInt16((UInt16)(message.Length + 8)); writer.Write(msglen); int tag = protocol[typeof(T)]; sessionDict.Add((long)session, protocol[tag].Response.Value); tag = Util.SwapInt32(tag); writer.Write(tag); writer.Write(message); int swap_session = Util.SwapInt32(session); writer.Write(swap_session); writer.Flush(); byte[] payload = ms.ToArray(); NetworkManager.GetInstance().SendBytesWithoutSize(payload); } }
public void ListenMessage <T>(SprotoTypeBase rpcReq, RpcRspHandler rpcRspHandler = null) { RpcRspHandler on_ack = null; on_ack = (SprotoTypeBase result) => { rpcRspHandler(result); this.SendMessage <T>(rpcReq, on_ack); }; this.SendMessage <T>(rpcReq, on_ack); }
public static void Dispatch() { package pkg = new package(); lock (queueLock) { if (recvQueue.Count == 0) { return; } Queue <byte[]> tmpQueue = recvQueue; recvQueue = dispatchQueue; dispatchQueue = tmpQueue; } if (dispatchQueue.Count > 20) { Debug.Log("dispatchQueue.Count: " + dispatchQueue.Count); } while (dispatchQueue.Count > 0) { byte[] data = recvPack.unpack(dispatchQueue.Dequeue()); int offset = pkg.init(data); int tag = (int)pkg.type; long session = (long)pkg.session; if (pkg.HasType) { RpcReqHandler rpcReqHandler = NetReceiver.GetHandler(tag); if (rpcReqHandler != null) { SprotoTypeBase rpcRsp = rpcReqHandler(protocol.GenRequest(tag, data, offset)); if (pkg.HasSession) { Send(rpcRsp, session, tag); } } } else { RpcRspHandler rpcRspHandler = NetSender.GetHandler(session); if (rpcRspHandler != null) { ProtocolFunctionDictionary.typeFunc GenResponse; sessionDict.TryGetValue(session, out GenResponse); rpcRspHandler(GenResponse(data, offset)); } } } }
public static void Send <T>(SprotoTypeBase rpcReq = null, RpcRspHandler rpcRspHandler = null) { if (rpcRspHandler != null) { session++; AddHandler(session, rpcRspHandler); NetCore.Send <T>(rpcReq, session); } else { NetCore.Send <T>(rpcReq); } }
private static void AddHandler(long session, RpcRspHandler rpcRspHandler) { rpcRspHandlerDict.Add(session, rpcRspHandler); }
public static void Dispatch() { //Debug.Log("dis"); try { package pkg = new package(); if (recvQueue.Count > 20) { Debug.Log("recvQueue.Count: " + recvQueue.Count); } while (recvQueue.Count > 0) { //Debug.Log("dis"); byte[] data = recvPack.unpack(recvQueue.Dequeue()); int offset = pkg.init(data); int tag = (int)pkg.type; long session = (long)pkg.session; if (pkg.HasType) { //Debug.Log("type"); try { RpcReqHandler rpcReqHandler = NetReceiver.GetHandler(tag); if (rpcReqHandler != null) { SprotoTypeBase rpcRsp = rpcReqHandler(protocol.GenRequest(tag, data, offset)); if (pkg.HasSession) { Send(rpcRsp, session, tag); } } } catch (Exception e) { Debug.Log("NetReceiver exception: " + e.Message + tag + session + offset); throw; } } else { //Debug.Log("no type"); try { RpcRspHandler rpcRspHandler = NetSender.GetHandler(session); if (rpcRspHandler != null) { ProtocolFunctionDictionary.typeFunc GenResponse; sessionDict.TryGetValue(session, out GenResponse); rpcRspHandler(GenResponse(data, offset)); } } catch (Exception e) { Debug.Log("NetReceiver exception: " + e.Message + tag + session + offset); throw; } } } } catch (Exception e) { Debug.Log("Dispatch exception: " + e.Message); throw; } }
//SprotoType.Handshake.request req = new SprotoType.Handshake.request(); //req.uid = uid; //req.token = token; //NetSender.Send<Protocol.Handshake>(req, (_) => //{ // SprotoType.Handshake.response rsp = _ as SprotoType.Handshake.response; // if (rsp.result == 0) // { // } //}); //SprotoTypeBase HeartbeatRsp(SprotoTypeBase _) //{ // SprotoType.Heartbeat.request req = _ as SprotoType.Heartbeat.request; // return null; // can return a response //} //NetReceiver.AddHandler<Protocol.Heartbeat>(HeartbeatRsp); public void Send <T>(SprotoTypeBase rpcReq = null, RpcRspHandler rpcRspHandler = null) { NetSender.Send <T>(rpcReq, rpcRspHandler); }