/// <summary> /// Callback when message received. /// </summary> /// <param name="buffer"> buffer we received. </param> public void MessageReceived(byte[] buffer) { // convert byte array to stream Stream stream = new MemoryStream(buffer); // using byte reader for the stream. BinaryReader br = new BinaryReader(stream); JCS_BinaryReader jcsbr = new JCS_BinaryReader(br); short packetId = jcsbr.ReadShort(); /** * NOTE(jenchieh): packet responded does not need a handler * to handle to response. I think is good if we have it inside * check if the packet get handler, but I think is easier if * just want to check if the packet responded or not. So I * decide to have this function here instead inside the * packet handler check under. */ // packet responded! JCS_PacketLostPreventer.instance.AddRespondPacketId(packetId); JCS_Client client = JCS_ClientManager.LOCAL_CLIENT; if (IsPacketOutdated(jcsbr, client, packetId)) { return; } // handler depends on the client/server mode. JCS_PacketHandler packetHandler = JCS_DefaultPacketProcessor.GetProcessor( JCS_NetworkSettings.instance.CLIENT_MODE ).GetHandler(packetId); if (packetHandler != null && packetHandler.validateState(client)) { // set the client and packet data buffer sequence. packetHandler.Client = client; packetHandler.PacketData = jcsbr; // register request. JCS_ServerRequestProcessor.instance.RegisterRequest(packetHandler.handlePacket, jcsbr, client); } else { if (packetHandler == null) { JCS_Debug.Log("Exception during processing packet: null"); } else { JCS_Debug.Log("Exception during processing packet: " + packetHandler); } } }
/// <summary> /// Get the packet by the packet id. /// </summary> /// <param name="packetId"> packet id. </param> /// <returns> packet handller that handles the packet by the /// packet id. </returns> public virtual JCS_PacketHandler GetHandler(short packetId) { if (packetId > mHandlers.Length) { return(null); } JCS_PacketHandler handler = mHandlers[packetId]; if (handler != null) { return(handler); } return(null); }
/// <summary> /// Reset all the packet handler list by mode. /// </summary> /// <param name="mode"></param> /// <returns></returns> public override void Reset(JCS_ClientMode mode) { mHandlers = new JCS_PacketHandler[mHandlers.Length]; // General RegisterHandler(JCS_RecvPacketType.HELLO, new JCS_HelloHandler()); if (mode == JCS_ClientMode.LOGIN_SERVER) { // Design here... } else if (mode == JCS_ClientMode.CHANNEL_SERVER) { // Design here... } }
/// <summary> /// Register the handler to the array list. /// </summary> /// <param name="code"> code byte/packet id </param> /// <param name="handler"> handler want to register. </param> public virtual void RegisterHandler(JCS_RecvPacketType code, JCS_PacketHandler handler) { mHandlers[(int)code] = handler; }