/// <summary> /// Process through the server request. /// </summary> private void ProcessServerRequest() { for (int index = 0; index < mServerRequest.Count; ++index) { if (index >= mServerRequest.Count || index >= mClient.Count || index >= mBinaryReader.Count) { continue; } JCS_BinaryReader br = mBinaryReader[index]; JCS_Client client = mClient[index]; try { // handle packet. mServerRequest[index].Invoke(br, client); } catch (System.Exception e) { JCS_Debug.LogError("Packet Handle Error : " + e.ToString()); } } // done all request, wait for next frame's request. mServerRequest.Clear(); mClient.Clear(); mBinaryReader.Clear(); }
/// <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> /// Is packet outdated by the packet number. /// </summary> /// <param name="jcsbr">binary read to read the packet number.</param> /// <param name="client">client to check if the packet is out-dated.</param> /// <param name="packetId">packet id use to get the packet number.</param> /// <returns>true, is out-dated. false, is newest packet.</returns> private bool IsPacketOutdated(JCS_BinaryReader jcsbr, JCS_Client client, short packetId) { // Check if the server need to check the packet No type of server. // UDP is one of the protocol type that does need to check // the packet by packet number. (order issue) if (client.IsOrderCheckServer()) { long packetNumber = jcsbr.ReadLong(); if (client.GetPacketNumber(packetId) > packetNumber) { // No need to do any process, because the packet has been // to late or already update by another packet. return(true); } else { // update packet number client.SetPacketNumber(packetId, packetNumber); } } return(false); }
/// <summary> /// Handle packet. /// </summary> /// <param name="buffer"></param> /// <param name="client"></param> public override void handlePacket(JCS_BinaryReader br, JCS_Client client) { // empty.. }
/// <summary> /// Handle packet. /// </summary> /// <param name="buffer"></param> /// <param name="client"></param> public abstract void handlePacket(JCS_BinaryReader br, JCS_Client client);
/// <summary> /// Deregister the request from server. /// </summary> public void DeresgisterRequest(ServerRequest request, JCS_BinaryReader br, JCS_Client client) { mServerRequest.Remove(request); mBinaryReader.Remove(br); mClient.Remove(client); }
/// <summary> /// Register request from server. /// </summary> public void RegisterRequest(ServerRequest request, JCS_BinaryReader br, JCS_Client client) { mServerRequest.Add(request); mBinaryReader.Add(br); mClient.Add(client); }
/// <summary> /// Handle packet. /// </summary> /// <param name="buffer"></param> /// <param name="client"></param> public override void handlePacket(JCS_BinaryReader br, JCS_Client client) { // handle packet logic.. }