/// <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); } } }
// singleton public static JCS_PacketProcessor GetProcessor(JCS_ClientMode mode) { if (JCS_ClientMode.LOGIN_SERVER == mode) { if (LOGIN_INSTANCE == null) { LOGIN_INSTANCE = new JCS_DefaultPacketProcessor(mode); } return(LOGIN_INSTANCE); } else if (JCS_ClientMode.CHANNEL_SERVER == mode) { if (CHANNEL_INSTANCE == null) { CHANNEL_INSTANCE = new JCS_DefaultPacketProcessor(mode); } return(CHANNEL_INSTANCE); } return(null); }