public static void WelcomeReceived(int _fromClient, ServerPacket _packet) { int _clientIdCheck = _packet.ReadInt(); string _username = _packet.ReadString(); int selectedCharacter = _packet.ReadInt(); Console.WriteLine(selectedCharacter); Console.WriteLine($"{Server.clients[_fromClient].tcp.socket.Client.RemoteEndPoint} connected successfully and is now player {_fromClient}."); if (_fromClient != _clientIdCheck) { Console.WriteLine($"Player \"{_username}\" (ID: {_fromClient}) has assumed the wrong client ID ({_clientIdCheck})!"); } Server.clients[_fromClient].SendIntoGame(_username, selectedCharacter); }
private bool HandleData(byte[] _data) { int _packetLength = 0; receivedData.SetBytes(_data); if (receivedData.UnreadLength() >= 4) { _packetLength = receivedData.ReadInt(); if (_packetLength <= 0) { return(true); } } while (_packetLength > 0 && _packetLength <= receivedData.UnreadLength()) { byte[] _packetBytes = receivedData.ReadBytes(_packetLength); ThreadManager.ExecuteOnMainThread(() => { using (ServerPacket _packet = new ServerPacket(_packetBytes)) { int _packetId = _packet.ReadInt(); Server.packetHandlers[_packetId](id, _packet); } }); _packetLength = 0; if (receivedData.UnreadLength() >= 4) { _packetLength = receivedData.ReadInt(); if (_packetLength <= 0) { return(true); } } } if (_packetLength <= 1) { return(true); } return(false); }
public static void PlayerMovement(int _fromClient, ServerPacket _packet) { bool[] _inputs = new bool[_packet.ReadInt()]; for (int i = 0; i < _inputs.Length; i++) { _inputs[i] = _packet.ReadBool(); } Quaternion _rotation = _packet.ReadQuaternion(); Console.WriteLine(_rotation); Server.clients[_fromClient].player.SetInput(_inputs, _rotation); }
public void HandleData(ServerPacket _packetData) { int _packetLength = _packetData.ReadInt(); byte[] _packetBytes = _packetData.ReadBytes(_packetLength); ThreadManager.ExecuteOnMainThread(() => { using (ServerPacket _packet = new ServerPacket(_packetBytes)) { int _packetId = _packet.ReadInt(); Server.packetHandlers[_packetId](id, _packet); } }); }
private static void UDPReceiveCallback(IAsyncResult _result) { try { IPEndPoint _clientEndPoint = new IPEndPoint(IPAddress.Any, 0); byte[] _data = udpListener.EndReceive(_result, ref _clientEndPoint); udpListener.BeginReceive(UDPReceiveCallback, null); if (_data.Length < 4) { return; } using (ServerPacket _packet = new ServerPacket(_data)) { int _clientId = _packet.ReadInt(); if (_clientId == 0) { return; } if (clients[_clientId].udp.endPoint == null) { clients[_clientId].udp.Connect(_clientEndPoint); return; } if (clients[_clientId].udp.endPoint.ToString() == _clientEndPoint.ToString()) { clients[_clientId].udp.HandleData(_packet); } } } catch (Exception _ex) { Console.WriteLine($"Error receiving UDP data: {_ex}"); } }