void OnData(int i, DataStreamReader stream) { NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); stream.ReadBytes(bytes); string recMsg = Encoding.ASCII.GetString(bytes.ToArray()); NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg); switch (header.cmd) { case Commands.LOGIN: LoginMsg lMsg = JsonUtility.FromJson <LoginMsg>(recMsg); Debug.Log("[Server] Client login message received! : " + lMsg.clientId); OnClientLogIn(i, lMsg.clientId); break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); //Debug.Log( "[Server] Player update message received! : " + puMsg.player.ToString() ); GameServerManager.Instance.UpdatePlayerCommands(puMsg.player, puMsg.commands); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("[Server] Server update message received!"); break; default: Debug.Log("[Server] Unrecognized message received!"); break; } }
void OnData(DataStreamReader stream, int i) { NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); // Convert the stream into bytes stream.ReadBytes(bytes); string recMsg = Encoding.ASCII.GetString(bytes.ToArray()); // Convert bytes into string NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg); // Convert the string into a json object switch (header.cmd) { case Commands.HANDSHAKE: HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg); Debug.Log("Handshake message received!"); break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); clients[puMsg.player.id].cubPos = puMsg.player.cubPos; //clients[puMsg.player.id].cubeColor = puMsg.player.cubeColor; Debug.Log("Player update message received!"); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("Server update message received!"); break; default: Debug.Log("SERVER ERROR: Unrecognized message received!"); break; } }
void OnData(DataStreamReader stream, int i) { NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); stream.ReadBytes(bytes); string recMsg = Encoding.ASCII.GetString(bytes.ToArray()); NetworkMessage header = JsonUtility.FromJson <NetworkMessage>(recMsg); switch (header.cmd) { case Commands.HANDSHAKE: HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg); AddPlayer(hsMsg); break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); UpdatePlayer(puMsg); break; case Commands.DISCONNECT: DisconnectMsg dcMsg = JsonUtility.FromJson <DisconnectMsg>(recMsg); RemovePlayer(dcMsg); break; default: Debug.Log("SERVER ERROR: Unrecognized message received!"); break; } }
void OnData(DataStreamReader stream, int i) { NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); stream.ReadBytes(bytes); string recMsg = Encoding.ASCII.GetString(bytes.ToArray()); NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg); switch (header.cmd) { case Commands.HANDSHAKE: HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg); Debug.Log("Handshake message received!"); break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); Debug.Log("Player update message received!"); UpdatePlayerInfo(puMsg.player); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("Server update message received!"); break; default: Debug.Log("SERVER ERROR: Unrecognized message received!"); break; } }
public unsafe void Assert_GotConnectionRequest(NetworkEndPoint from, bool accept = false) { int length; NetworkEndPoint remote; m_LocalDataStream.Clear(); Assert.True( IPCManager.Instance.PeekNext(Address, m_LocalDataStream.GetUnsafePtr(), out length, out remote) >= sizeof(UdpCHeader)); m_LocalDataStream.WriteBytesWithUnsafePointer(length); UdpCHeader header = new UdpCHeader(); var reader = new DataStreamReader(m_LocalDataStream, 0, sizeof(UdpCHeader)); var readerCtx = default(DataStreamReader.Context); Assert.True(reader.IsCreated); reader.ReadBytes(ref readerCtx, header.Data, sizeof(UdpCHeader)); Assert.True(header.Type == (int)UdpCProtocol.ConnectionRequest); Assert.True(remote.Family == NetworkFamily.IPC); //Assert.True(remote.ipc_handle == from.ipc_handle); Assert.True(remote.Port == from.Port); if (accept) { m_LocalDriver.ScheduleUpdate().Complete(); var con = m_LocalDriver.Accept(); ClientConnections.Add(con); Assert.True(con != default(NetworkConnection)); } }
void OnData(DataStreamReader stream) { NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); stream.ReadBytes(bytes); //Get bytes string recMsg = Encoding.ASCII.GetString(bytes.ToArray()); //convert bytes to JSON string NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg); //convert JSON to c# class switch (header.cmd) { case Commands.HANDSHAKE: HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg); Debug.Log("Handshake message received!"); break; case Commands.PLAYER_INTERNALID: PlayerUpdateMsg internalId = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); Debug.Log("Got internalId from server"); playerInfo.player.id = internalId.player.id; playerInternalIdText.text = playerInfo.player.id; break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); //Debug.Log("Player update message received!"); Debug.Log("Got data from server, player Pos: " + puMsg.player.pos); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("Server update message received!"); UpdateClientsInfo(suMsg); break; //To spawn existed players case Commands.SPAWN_EXISTED_PLAYERS: ServerUpdateMsg existedPlayerInfo = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("existed player info received!"); SpawnExistedPlayer(existedPlayerInfo); break; //Spawn new player case Commands.SPAWN_NEW_PLAYER: PlayerUpdateMsg newPlayerInfo = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); Debug.Log("new client info received!"); SpawnNewPlayer(newPlayerInfo); break; //handle disconnected player case Commands.DISCONNECTED_PLAYER: DisconnectedPlayersMsg dpm = JsonUtility.FromJson <DisconnectedPlayersMsg>(recMsg); Debug.Log("Disconnected player info recieved!"); DeleteDisconnectPlayer(dpm); break; default: Debug.Log("Unrecognized message received!"); break; } }
protected override FileTransporter RecieveStart(int hash, DataStreamReader stream, ref DataStreamReader.Context ctx) { if (string.IsNullOrEmpty(SaveDirectory)) { SaveDirectory = Application.persistentDataPath + "/RecieveFile"; System.IO.Directory.CreateDirectory(SaveDirectory); } string fileName = stream.ReadString(ref ctx); int fileSize = stream.ReadInt(ref ctx); ushort dataSize = stream.ReadUShort(ref ctx); string path = SaveDirectory + "/" + hash + "_" + fileName; FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite); var transporter = new FileTransporter(hash, fileName, fs, 8092, 8092); unsafe { fixed(byte *data = transporter.buffer) { stream.ReadBytes(ref ctx, data, dataSize); transporter.bufferPos += dataSize; } } return(transporter); }
void OnData(DataStreamReader stream, int i) { NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); stream.ReadBytes(bytes); string recMsg = Encoding.ASCII.GetString(bytes.ToArray()); NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg); switch (header.cmd) { case Commands.NEW_CLIENT: NewClientMsg ncMsg = JsonUtility.FromJson <NewClientMsg>(recMsg); Debug.Log("Server msg: New Client message received!"); break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); Debug.Log("Server msg: Player update message received!"); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); ServerMessage(suMsg); break; default: Debug.Log("SERVER ERROR: Unrecognized message received!"); break; } }
public unsafe void Assert_GotConnectionRequest(NetworkEndPoint from, bool accept = false) { int length; NetworkInterfaceEndPoint remote; Assert.True(EndPoint.IsLoopback || EndPoint.IsAny); Assert.True(from.IsLoopback || from.IsAny); var localEndPoint = IPCManager.Instance.CreateEndPoint(EndPoint.Port); var fromEndPoint = IPCManager.Instance.CreateEndPoint(from.Port); Assert.True( IPCManager.Instance.PeekNext(localEndPoint, m_LocalData.GetUnsafePtr(), out length, out remote) >= sizeof(UdpCHeader)); UdpCHeader header = new UdpCHeader(); var reader = new DataStreamReader(m_LocalData.GetSubArray(0, sizeof(UdpCHeader))); Assert.True(reader.IsCreated); reader.ReadBytes(header.Data, sizeof(UdpCHeader)); Assert.True(header.Type == (int)UdpCProtocol.ConnectionRequest); Assert.True(remote == fromEndPoint); if (accept) { m_LocalDriver.ScheduleUpdate().Complete(); var con = m_LocalDriver.Accept(); ClientConnections.Add(con); Assert.True(con != default(NetworkConnection)); } }
protected override void Deserialize(ref DataStreamReader reader, byte dirtyMask) { if (IsDirtyAt(dirtyMask, 0)) { int byteCount = reader.ReadInt(); var bytes = new NativeArray <byte>(byteCount, Allocator.Temp); reader.ReadBytes(bytes); _paramBytes = bytes.ToArray(); bytes.Dispose(); DeserializeParameters(_paramBytes); } if (IsDirtyAt(dirtyMask, 1)) { _currentStateHash = reader.ReadInt(); _currentEnterTime = reader.ReadFloat(); if (_currentStateHash == _animator.GetCurrentAnimatorStateInfo(0).fullPathHash) { _animator.PlayInFixedTime(0, 0, Server.Time - _currentEnterTime); } else { _animator.PlayInFixedTime(_currentStateHash, 0, Server.Time - _currentEnterTime); } } }
void OnData(DataStreamReader stream, int i, NetworkConnection client) { NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); stream.ReadBytes(bytes); string recMsg = Encoding.ASCII.GetString(bytes.ToArray()); NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg); switch (header.cmd) { case Commands.HANDSHAKE: HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg); Debug.Log("Handshake message received!"); break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); Debug.Log("Player position " + puMsg.player.pos); UpdatePlayerInfo(puMsg); break; case Commands.SERVER_UPDATE: ServerUpdateMsg ServerUpdateMessage = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("Server Update."); break; default: Debug.Log("ServerError (Default Log)"); break; } }
void OnData(DataStreamReader stream) { //these guys are converting unreadable data to readable and the final version will be called recMsg NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); stream.ReadBytes(bytes); string recMsg = Encoding.ASCII.GetString(bytes.ToArray()); NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg); // switch (header.cmd) { case Commands.ID_UPDATE: IDUpdateMsg idMsg = JsonUtility.FromJson <IDUpdateMsg>(recMsg); myPlayer.id = idMsg.id; Debug.Log("Client: Server returned me my new id: " + myPlayer.id.ToString()); break; case Commands.HANDSHAKE: HandshakeMsg m = JsonUtility.FromJson <HandshakeMsg>(recMsg); Debug.Log("(Server) Hello, player " + myPlayer.id.ToString()); break; default: Debug.Log("Unrecognized message received!"); break; } }
void OnData(DataStreamReader stream) { NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); stream.ReadBytes(bytes); string recMsg = Encoding.ASCII.GetString(bytes.ToArray()); MessageHeader header = JsonUtility.FromJson <MessageHeader>(recMsg); switch (header.type) { case MessageType.HEARTBEAT: Debug.Log("Heartbeat"); break; case MessageType.BATTLE_MSG: var bMsg = JsonUtility.FromJson <BattleMessage>(recMsg); BattleData.SetBattleData(bMsg); Debug.Log("Battle Started"); GameController.Instance.StartMultiplayerBattle(); break; case MessageType.MOVE_MSG: //Move Received var mMsg = JsonUtility.FromJson <MoveMessage>(recMsg); move = mMsg; Debug.Log("Move " + mMsg.MoveName + " Received"); break; default: Debug.Log("Unrecognized message received!"); break; } }
void OnData(DataStreamReader stream) { NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); stream.ReadBytes(bytes); string recMsg = Encoding.ASCII.GetString(bytes.ToArray()); NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg); switch (header.cmd) { case Commands.HANDSHAKE: HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg); Debug.Log("[Client] Handshake message received! " + hsMsg.player.id); GameplayManager.Instance.SpawnPlayer(hsMsg.player, true); break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); Debug.Log("[Client] Player update message received!"); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("[Client] Server update message received!" + suMsg.players.ToArrayString()); GameplayManager.Instance.UpdatePlayers(suMsg.players); break; default: Debug.Log("[Client] Unrecognized message received!"); break; } }
private void OnData(DataStreamReader stream, int connectionIndex) { NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); stream.ReadBytes(bytes); string returnData = Encoding.ASCII.GetString(bytes.ToArray()); NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(returnData); int clientIndex = FindMatchingClient(m_Connections[connectionIndex].InternalId); clients[clientIndex].interval = Time.deltaTime; switch (header.cmd) { case Commands.INPUT: { PlayerInput input = JsonUtility.FromJson <PlayerInput>(returnData); players[clientIndex].transform.position = input.input; clients[clientIndex].position = players[clientIndex].transform.position; } break; default: break; } }
void OnData(DataStreamReader stream, int i, NetworkConnection client) { NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); //Where to store bytes data stream.ReadBytes(bytes); //Get bytes data from stream string recMsg = Encoding.ASCII.GetString(bytes.ToArray()); //convert bytes data to string(JSON) NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg); //convert JSON to c# object switch (header.cmd) { case Commands.HANDSHAKE: HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg); Debug.Log("Handshake message received!"); break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); Debug.Log("Got " + puMsg.player.id + " player Info"); UpdateClientInfo(puMsg); //Debug.Log("Player update message received!"); //Debug.Log("Got data from client, Player Pos: " + puMsg.player.pos); //TestSendBack(client, puMsg); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("Server update message received!"); break; default: Debug.Log("SERVER ERROR: Unrecognized message received!"); break; } }
void OnData(DataStreamReader stream, int i) { NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); stream.ReadBytes(bytes); string recMsg = Encoding.ASCII.GetString(bytes.ToArray()); NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg); switch (header.cmd) { case Commands.HANDSHAKE: HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg); Debug.Log("Server: Hello, player " + hsMsg.player.id.ToString()); break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); m_listOfPlayers[i].body.transform.position = puMsg.player.body.transform.position; ServerUpdateMsg serUpMsg = new ServerUpdateMsg(); serUpMsg.players = m_listOfPlayers; SendToClient(JsonUtility.ToJson(serUpMsg), m_Connections[i]); Debug.Log("Player update message received!"); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("Server update message received!"); break; default: Debug.Log("SERVER ERROR: Unrecognized message received!"); break; } }
// on data received void OnData(DataStreamReader stream) { NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); stream.ReadBytes(bytes); string recMsg = Encoding.ASCII.GetString(bytes.ToArray()); NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg); switch (header.cmd) { // player handshake case Commands.HANDSHAKE: HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg); SpawnPlayer(hsMsg.player); Debug.Log("Handshake message received!"); break; // player update case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); ClientPlayerUpdate(puMsg.players); Debug.Log("Player update message received!"); break; // server update case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("Server update message received!"); break; // player list update case Commands.PLAYER_LIST: ClientListMsg listMsg = JsonUtility.FromJson <ClientListMsg>(recMsg); for (int p = 0; p < listMsg.players.Length; p++) { SpawnPlayer(listMsg.players[p]); } Debug.Log("Player list message received!"); break; // player disconnect case Commands.PLAYER_DISCONNECT: DiscPlayerMsg discMsg = JsonUtility.FromJson <DiscPlayerMsg>(recMsg); RemoveClientPlayer(discMsg.players); Debug.Log("Player disconnect message received!"); break; // player connect case Commands.PLAYER_JOIN: HandshakeMsg nhsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg); ClientPlayerID = nhsMsg.player.id; SpawnPlayer(nhsMsg.player); break; default: Debug.Log("Unrecognized message received!"); break; } }
void OnData(DataStreamReader stream, int i) { NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); stream.ReadBytes(bytes); string recMsg = Encoding.ASCII.GetString(bytes.ToArray()); NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg); switch (header.cmd) { case Commands.HANDSHAKE: HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg); Debug.Log("Handshake message received!"); break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); Debug.Log("Player update message received!"); Debug.Log("Player List: " + serverPlayerList); foreach (NetworkObjects.NetworkPlayer player in serverPlayerList) { if (player.id == puMsg.player.id) { player.cubeColor = puMsg.player.cubeColor; player.cubPos = puMsg.player.cubPos; player.cubRot = puMsg.player.cubRot; } } break; case Commands.PLAYER_BULLET: PlayerBulletMsg pbMsg = JsonUtility.FromJson <PlayerBulletMsg>(recMsg); Debug.Log("Player Bullet Fired!"); FireBulletForPlayers(pbMsg, m_Connections[i]); break; case Commands.PLAYER_JOINED: PlayerJoinMessage pjMsg = JsonUtility.FromJson <PlayerJoinMessage>(recMsg); Debug.Log("Player join message received!"); CreatePlayer(pjMsg, m_Connections[i]); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("Server update message received!"); break; case Commands.PLAYER_LEFT: PlayerLeaveMsg plMsg = JsonUtility.FromJson <PlayerLeaveMsg>(recMsg); Debug.Log("Player leave message recieved!"); OnDisconnect(int.Parse(plMsg.player.id)); break; default: Debug.Log("SERVER ERROR: Unrecognized message received!"); break; } }
private void OnData(DataStreamReader stream) { NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); stream.ReadBytes(bytes); string recMsg = Encoding.ASCII.GetString(bytes.ToArray()); NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg); switch (header.cmd) { case Commands.HANDSHAKE: HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg); Debug.Log("[Notice] Handshake message received!"); break; case Commands.PLAYER_UPDATE: //shouldnt happen Debug.LogError("[Error] Player update message received! Client shouldn't receive messages from clients."); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); //Debug.Log("[Routine] Server update message received!"); UpdateRemotePlayers(suMsg.players); break; case Commands.PONG: if (bVerboseDebug) { //Debug.Log("[Routine] Pong message received!"); } break; case Commands.SERVER_HANDSHAKE: ServerHandshakeMsg shMsg = JsonUtility.FromJson <ServerHandshakeMsg>(recMsg); Debug.Log("[Notice] Handshake from server received!"); m_clientIDDict.Add(shMsg.clientID, localClientCharacterRef); //Add local character to player dictionary m_thisClientID = shMsg.clientID; //keep a reference to local player ID SpawnRemotePlayers(shMsg.players); //Spawn remote players StartCoroutine(UploadClientDataRoutine(m_clientUpdateInterval)); //Start routinely updating server with local cube character data break; case Commands.NEW_PLAYER: PlayerUpdateMsg puMSg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); Debug.Log("[Notice] A new player has connected. (" + puMSg.player.clientID + ")"); SpawnRemotePlayer(puMSg.player); break; case Commands.PLAYER_DISCONNECT: PlayerIDMsg pdMSg = JsonUtility.FromJson <PlayerIDMsg>(recMsg); Debug.Log("[Notice] A player has disconnected. (" + pdMSg.clientID + ")"); RemoveRemotePlayer(pdMSg.clientID); break; default: Debug.LogError("[Error] Unrecognized message received!"); break; } }
private void OnData(DataStreamReader stream) { NativeArray <byte> message = new NativeArray <byte>(stream.Length, Allocator.Temp); stream.ReadBytes(message); string returnData = Encoding.ASCII.GetString(message.ToArray()); NetworkHeader header = new NetworkHeader(); header = JsonUtility.FromJson <NetworkHeader>(returnData); switch (header.cmd) { case Commands.NEW_CLIENT: { Debug.Log("New client"); NewPlayer np = JsonUtility.FromJson <NewPlayer>(returnData); Debug.Log("NEW_CLIENT: " + np.player.ToString()); SpawnPlayers(np.player); break; } case Commands.UPDATE: { UpdatedPlayer up = JsonUtility.FromJson <UpdatedPlayer>(returnData); UpdatePlayers(up.update); break; } case Commands.CLIENT_DROPPED: { DisconnectedPlayer dp = JsonUtility.FromJson <DisconnectedPlayer>(returnData); DestroyPlayers(dp.disconnect); Debug.Log("Client dropped"); break; } case Commands.CLIENT_LIST: { ConnectedPlayer cp = JsonUtility.FromJson <ConnectedPlayer>(returnData); SpawnPlayers(cp.connect); Debug.Log("Client list"); break; } case Commands.OWN_ID: { NewPlayer p = JsonUtility.FromJson <NewPlayer>(returnData); myID = p.player.id; SpawnPlayers(p.player); Debug.Log("OWN_ID: " + myID); break; } default: Debug.Log("Error"); break; } }
void OnData(DataStreamReader stream) { NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); stream.ReadBytes(bytes); //Get bytes string recMsg = Encoding.ASCII.GetString(bytes.ToArray()); //convert bytes to JSON string NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg); //convert JSON to c# class switch (header.cmd) { case Commands.HANDSHAKE: HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg); Debug.Log("Handshake message received!"); break; case Commands.PLAYER_WHOAMI: PlayerUpdateMsg internalId = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); Debug.Log("Got internalId from server"); playerInfo.player.id = internalId.player.id; playerIDText.SetText(playerInfo.player.id); myIDLabel.SetText(playerInfo.player.id); break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); Debug.Log("Got data from server, player Pos: " + puMsg.player.pos); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("Server update message received!"); UpdateClientsInfo(suMsg); break; case Commands.ALREADY_HERE_PLAYERS: ServerUpdateMsg alreadyHerePlayerInfo = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("existed player info received!"); SpawnPlayersAlreadyHere(alreadyHerePlayerInfo); break; case Commands.SPAWN_NEW_PLAYER: PlayerUpdateMsg newPlayerMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); Debug.Log("new client info received!"); SpawnNewPlayer(newPlayerMsg); break; case Commands.DISCONNECTED_PLAYER: DisconnectedPlayersMsg dpMsg = JsonUtility.FromJson <DisconnectedPlayersMsg>(recMsg); Debug.Log("Player disconnected :("); DeleteDisconnectPlayer(dpMsg); break; default: Debug.Log("Unrecognized message received!"); break; } }
void OnData(DataStreamReader stream) { NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); stream.ReadBytes(bytes); string recMsg = Encoding.ASCII.GetString(bytes.ToArray()); NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg); switch (header.cmd) { case Commands.PLAYER_INIT: InitializeConnectionMsg piMsg = JsonUtility.FromJson <InitializeConnectionMsg>(recMsg); playerUpdateMsg.player.id = piMsg.yourID; HandshakeMsg m = new HandshakeMsg(); m.player = playerUpdateMsg.player; m.player.id = piMsg.yourID; SendToServer(JsonUtility.ToJson(m)); otherPlayers.Add(m.player.id, yourCharacter); Debug.Log("Initialization message received! Your ID: " + piMsg.yourID); break; case Commands.HANDSHAKE: HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg); Debug.Log("Handshake message received!"); break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); Debug.Log("Player update message received!"); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); UpdateNetworkObjects(suMsg); Debug.Log("Server update message received!"); break; case Commands.PLAYER_DROPPED: ConnectionDroppedMsg cdMsg = JsonUtility.FromJson <ConnectionDroppedMsg>(recMsg); // Destroy GameObject character = otherPlayers[cdMsg.droppedId]; otherPlayers.Remove(cdMsg.droppedId); Destroy(character); Debug.Log("Player dropped message received! " + cdMsg.droppedId); break; default: Debug.Log("Unrecognized message received!"); break; } }
void OnData(DataStreamReader stream, int i) { NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); stream.ReadBytes(bytes); string recMsg = Encoding.ASCII.GetString(bytes.ToArray()); NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg); switch (header.cmd) { case Commands.PLAYER_CONNECTED: ConnectionApprovedMsg cpMsg = JsonUtility.FromJson <ConnectionApprovedMsg>(recMsg); Debug.Log("Handshake message received!"); break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); //Debug.Log("Player update message received!"); playerData = puMsg; Debug.Log("Player ID: " + playerData.player.id + " Player Position: " + playerData.player.cubPos); //Debug.Log("Time As of Now: " + DateTime.Now); playerData.player.pulse = DateTime.Now; for (i = 0; i < ConnectedList.GameState.Count; i++) { if (playerData.player.id == ConnectedList.GameState[i].id) { ConnectedList.GameState.Insert(i, playerData.player); ConnectedList.GameState.RemoveAt(i + 1); } Debug.Log("Transfered ID: " + ConnectedList.GameState[i].id + "Transfered Pos: " + ConnectedList.GameState[i].cubPos); } break; case Commands.PLAYER_DISCONNECTED: PlayerDisconnectMsg pdMsg = JsonUtility.FromJson <PlayerDisconnectMsg>(recMsg); Debug.Log("Player Disconnect update message received!, Got This: " + i); OnDisconnect(i); break; case Commands.GAMESTATE: GameStateMsg gsMsg = JsonUtility.FromJson <GameStateMsg>(recMsg); Debug.Log("GameState update message received!"); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("Server update message received!"); break; default: Debug.Log("SERVER ERROR: Unrecognized message received!"); break; } }
void OnData(DataStreamReader stream) { NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); stream.ReadBytes(bytes); string recMsg = Encoding.ASCII.GetString(bytes.ToArray()); NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg); switch (header.cmd) { case Commands.PLAYER_CONNECT: PlayerConnectMsg pcMsg = JsonUtility.FromJson <PlayerConnectMsg>(recMsg); SpawnPlayers(pcMsg.newPlayer.id, pcMsg.newPlayer.cubeColor); Debug.Log("New Player joined"); break; case Commands.INVERT_BALL: GameObject.FindGameObjectWithTag("Ball").GetComponent <Ball>().invert = -1; break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); UpdatePlayers(puMsg.players); break; case Commands.OWNED_ID: OwnIDMsg idMsg = JsonUtility.FromJson <OwnIDMsg>(recMsg); playerID = idMsg.ownedPlayer.id; Debug.Log("Own id received! id: " + playerID); break; case Commands.PLAYER_DROPPED: PlayerDropMsg dropMsg = JsonUtility.FromJson <PlayerDropMsg>(recMsg); foreach (NetworkObjects.NetworkPlayer p in dropMsg.droppedPlayers) { DestroyPlayers(p.id); } break; case Commands.PLAYER_LIST: PlayerListMsg plMsg = JsonUtility.FromJson <PlayerListMsg>(recMsg); foreach (var it in plMsg.players) { SpawnPlayers(it.id, it.cubeColor); Debug.Log("Spawn player with id: " + it.id); } break; default: Debug.Log("Unrecognized message received!"); break; } }
void OnData(DataStreamReader stream) { NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); stream.ReadBytes(bytes); string recMsg = Encoding.ASCII.GetString(bytes.ToArray()); NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg); switch (header.cmd) { case Commands.HANDSHAKE: HandshakeMsg hsMsg = JsonUtility.FromJson <HandshakeMsg>(recMsg); Debug.Log("Handshake message received! ID : " + hsMsg.player.id); serverInternalId = hsMsg.player.id; clientList.Add(hsMsg.player.id, Instantiate(prefab)); break; //case Commands.PLAYER_UPDATE: // PlayerUpdateMsg puMsg = JsonUtility.FromJson<PlayerUpdateMsg>(recMsg); // Debug.Log("Player update message received!"); // break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("Server update message received! "); Debug.Log("# of Players : " + suMsg.players.Count); List <string> tempListId = new List <string>(); foreach (NetworkObjects.NetworkPlayer player in suMsg.players) { tempListId.Add(player.id); Debug.Log(" >>>>>> ID : " + player.id); if (!clientList.ContainsKey(player.id)) { clientList.Add(player.id, Instantiate(prefab)); } if (player.id != serverInternalId) { clientList[player.id].transform.position = player.cubPos; clientList[player.id].GetComponent <Renderer>().material.color = player.cubeColor; } } ServerListId.Clear(); ServerListId = tempListId; break; default: Debug.Log("Unrecognized message received!"); break; } }
unsafe public static string ReadString(this DataStreamReader reader, ref DataStreamReader.Context ctx, Encoding encoding) { var length = reader.ReadByte(ref ctx); fixed(byte *buf = buffer) { reader.ReadBytes(ref ctx, buf, length); } return(encoding.GetString(buffer, 0, length)); }
unsafe string ReadString(DataStreamReader reader, ref DataStreamReader.Context ctx) { var length = reader.ReadByte(ref ctx); var buffer = new byte[byte.MaxValue]; fixed(byte *bufferPtr = &buffer[0]) { reader.ReadBytes(ref ctx, bufferPtr, length); } return(encoding.GetString(buffer, 0, length)); }
public bool Send(Room room, int toConnectionId, int fromConnectionId, DataStreamReader data) { if (ServerBehaviour.Config.BandwidthLimit > 0) { // Bandwidth control logic int taxedId = room.Server.ConnectionId == fromConnectionId ? toConnectionId : fromConnectionId; if (ContainsConnectedID(room.RoomId, taxedId)) { Client clientToBeTaxed = default(Client); foreach (Client c in connectedClients.GetValuesForKey(room.RoomId)) { if (c.ConnectionId == taxedId) { clientToBeTaxed = c; } } int bandwidthLimit = clientToBeTaxed.IsInBandwidthGracePeriod ? ServerBehaviour.Config.GracePeriodBandwidthLimit : ServerBehaviour.Config.BandwidthLimit; if (clientToBeTaxed.OutgoingBytes / (DateTime.UtcNow - clientToBeTaxed.ConnectTime).TotalSeconds > bandwidthLimit) { // Client used too much bandwidth. Disconnect them Console.WriteLine("[INFO] Bandwidth exceeded, client disconnected for overdue. The client is " + (clientToBeTaxed.IsInBandwidthGracePeriod ? "" : "not ") + "on grace period"); HandleClientDisconnect(room, taxedId, true); return(false); } // This includes relay overhead!! // TODO: Strip overhead clientToBeTaxed.OutgoingBytes += (ulong)data.Length; } } // Send the data DataStreamWriter writer; writer = driver.BeginSend(pipeline, connections[toConnectionId]); writer.WriteByte((byte)MessageType.Data); writer.WriteInt(fromConnectionId); unsafe { // Copy data from reader to writer byte *buffer; int len = data.Length - data.GetBytesRead(); data.ReadBytes((byte *)&buffer, len); writer.WriteBytes((byte *)&buffer, len); } driver.EndSend(writer); return(true); }
void OnData(DataStreamReader stream) { NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); stream.ReadBytes(bytes); string recMsg = Encoding.ASCII.GetString(bytes.ToArray()); NetworkHeader header = JsonUtility.FromJson <NetworkHeader>(recMsg); switch (header.cmd) { case Commands.NEW_CLIENT: NewClientMsg ncMsg1 = JsonUtility.FromJson <NewClientMsg>(recMsg); SpawnPlayer(ncMsg1.player); Debug.Log("Client Msg: New Client message received!"); break; case Commands.CLIENT_DROP: DropPlayerMsg dpMsg = JsonUtility.FromJson <DropPlayerMsg>(recMsg); RemovePlayers(dpMsg.players); Debug.Log("Client Msg: Client drop message received!"); break; case Commands.CLIENT_LIST: PlayerListMsg nplMsg = JsonUtility.FromJson <PlayerListMsg>(recMsg); for (int i = 0; i < nplMsg.players.Length; i++) { SpawnPlayer(nplMsg.players[i]); } Debug.Log("Client Msg: Client list message received!"); break; case Commands.PLAYER_UPDATE: PlayerUpdateMsg puMsg = JsonUtility.FromJson <PlayerUpdateMsg>(recMsg); PlayerUpdate(puMsg.updatedPlayers); break; case Commands.SERVER_UPDATE: ServerUpdateMsg suMsg = JsonUtility.FromJson <ServerUpdateMsg>(recMsg); Debug.Log("Client Msg: Server update message received!"); break; case Commands.PLAYER_ID: NewClientMsg ncMsg2 = JsonUtility.FromJson <NewClientMsg>(recMsg); playerID = ncMsg2.player.id; SpawnPlayer(ncMsg2.player); break; default: Debug.Log("Client Msg: Unrecognized message received!"); break; } }