public void OnUDPReceive(IAsyncResult ia) { try { byte[] data = _UDPSocket.EndReceive(ia, ref _IEPRef); //======================================================================\\ _UDPSocket.BeginReceive(OnUDPReceive, null); //======================================================================\\ if (data.Length > 0) { lock (lockerUDPEndPoints) { if (!udpEndPoints.Contains(_IEPRef)) { udpEndPoints.Add(_IEPRef); } } //=========================================================================\\ byte[] decompressedBuffer = data.Decompress(COMPRESSION_MODE, data.Length); //==========================================================================\\ using (NeutronReader mReader = new NeutronReader(decompressedBuffer)) { Packet mCommand = mReader.ReadPacket <Packet>(); if (GetPlayer(_IEPRef, out Player Sender)) { switch (mCommand) { case Packet.RPC: HandleRPC(Sender, mCommand, mReader.ReadPacket <Broadcast>(), mReader.ReadInt32(), mReader.ReadPacket <SendTo>(), mReader.ReadPacket <ValidationPacket>(), mReader.ReadBoolean(), mReader.ReadBytes(4096), Sender.tcpClient.RemoteEndPoint()); break; case Packet.OnCustomPacket: break; case Packet.VoiceChat: HandleVoiceChat(Sender, mCommand, mReader.ReadInt32(), mReader.ReadBytes(4096), Sender.tcpClient.RemoteEndPoint()); break; } } } } } catch (SocketException ex) { LoggerError(ex.ErrorCode); //=========================== RenitializeWhenException(_IEPRef); } }
public static void OnUDPReceive(IAsyncResult ia) { try { byte[] data = _UDPSocket.EndReceive(ia, ref _IEPRef); //==============================================================================================\\ _UDPSocket.BeginReceive(OnUDPReceive, null); //==============================================================================================\\ if (data.Length > 0) { byte[] decompressedBuffer = data.Decompress(COMPRESSION_MODE, data.Length); //==================================================================================\\ using (NeutronReader mReader = new NeutronReader(decompressedBuffer)) { Packet mCommand = mReader.ReadPacket <Packet>(); switch (mCommand) { case Packet.SendInput: SerializableInput nInput = mReader.ReadBytes(2048).DeserializeObject <SerializableInput>(); //================================================================================================================================ SerializableVector3 nVelocity = nInput.Vector; //================================================================================================================================ Vector3 velocity = new Vector3(nVelocity.x, nVelocity.y, nVelocity.z); //================================================================================================================================ //Neutron.Enqueue(() => playerRB.velocity = velocity); break; case Packet.RPC: HandleRPC(mReader.ReadInt32(), mReader.ReadBytes(4096)); break; case Packet.VoiceChat: HandleVoiceChat(mReader.ReadInt32(), mReader.ReadBytes(4096)); break; } } } else { LoggerError("UDP Error"); } } catch (SocketException ex) { LoggerError(ex.Message + ":" + ex.ErrorCode); } }
void ProcessClientData(TcpClient mSocket, byte[] bufferCopy, int length, IPEndPoint endPoint) { try { using (NeutronReader mReader = new NeutronReader(bufferCopy)) { Packet mCommand = mReader.ReadPacket <Packet>(); if (GetPlayer(mSocket, out Player Sender)) { switch (mCommand) { case Packet.Connected: HandleConfirmation(Sender, mCommand); break; case Packet.Nickname: HandleNickname(Sender, mReader.ReadString()); break; case Packet.SendChat: HandleSendChat(Sender, mCommand, mReader.ReadPacket <Broadcast>(), mReader.ReadString()); break; case Packet.InstantiatePlayer: HandleInstantiatePlayer(Sender, mCommand, mReader.ReadPacket <Broadcast>(), mReader.ReadVector3(), mReader.ReadQuaternion(), mReader.ReadString()); break; case Packet.SendInput: HandleSendInput(Sender, mCommand, mReader.ReadBytes(length)); break; case Packet.RPC: HandleRPC(Sender, mCommand, mReader.ReadPacket <Broadcast>(), mReader.ReadInt32(), mReader.ReadPacket <SendTo>(), mReader.ReadPacket <ValidationPacket>(), mReader.ReadBoolean(), mReader.ReadBytes(length), endPoint); break; case Packet.OnCustomPacket: HandleOnCustomPacket(Sender, mCommand, mReader.ReadPacket <Packet>(), mReader.ReadBytes(length)); break; case Packet.Database: Packet dbPacket = mReader.ReadPacket <Packet>(); switch (dbPacket) { case Packet.Login: if (!isLoggedin(mSocket)) { string username = mReader.ReadString(); string passsword = mReader.ReadString(); Enqueue(() => StartCoroutine(Login(Sender, username, passsword)), ref monoBehaviourActions); } else { SendErrorMessage(Sender, mCommand, "You are already logged in."); } break; } break; case Packet.GetChannels: HandleGetChannels(Sender, mCommand); break; case Packet.JoinChannel: HandleJoinChannel(Sender, mCommand, mReader.ReadInt32()); break; case Packet.GetChached: HandleGetCached(Sender, mReader.ReadPacket <CachedPacket>()); break; case Packet.CreateRoom: HandleCreateRoom(Sender, mCommand, mReader.ReadString(), mReader.ReadInt32(), mReader.ReadString(), mReader.ReadBoolean(), mReader.ReadBoolean(), mReader.ReadBytes(length)); break; case Packet.GetRooms: HandleGetRooms(Sender, mCommand); break; case Packet.JoinRoom: HandleJoinRoom(Sender, mCommand, mReader.ReadInt32()); break; case Packet.DestroyPlayer: HandleDestroyPlayer(Sender, mCommand); break; case Packet.SendMousePosition: HandleNavMeshAgent(Sender, mCommand, mReader.ReadVector3()); break; } } } } catch (Exception ex) { LoggerError($"Failed to Response Client {ex.Message}"); } }