public void Execute(Entity entity, int index, ref NetworkServerConnection serverConnection) { DataStreamReader stream; NetworkEvent.Type command; while ((command = driver.PopEventForConnection(serverConnection.connection, out stream)) != NetworkEvent.Type.Empty) { if (command == NetworkEvent.Type.Data) { /* * Structure of a message to be sent over the server * [size of message, not including this part, sizeof(int)][command type sizeof(int)][other data] */ Debug.Log("data command on server"); DataStreamReader.Context readerCtx = default(DataStreamReader.Context); byte[] lengthOfDataAsBytes = stream.ReadBytesAsArray(ref readerCtx, sizeof(int)); if (BitConverter.IsLittleEndian) { Array.Reverse(lengthOfDataAsBytes); } int lengthOfData = BitConverter.ToInt32(lengthOfDataAsBytes, 0); byte[] dataRead = stream.ReadBytesAsArray(ref readerCtx, lengthOfData); byte[] cmdTypeRead = new byte[sizeof(int)]; Buffer.BlockCopy(dataRead, 0, cmdTypeRead, 0, sizeof(int)); if (BitConverter.IsLittleEndian) { Array.Reverse(cmdTypeRead); } int commandType = BitConverter.ToInt32(cmdTypeRead, 0); Debug.Log("command type received " + commandType); if (commandType == CommandType.SpawnNewPlayer) { int networkId = id[0]; byte[] cmdTypeAsBytes = BitConverter.GetBytes(CommandType.SpawnNewPlayer); byte[] idAsBytes = BitConverter.GetBytes(networkId); if (BitConverter.IsLittleEndian) { Array.Reverse(cmdTypeAsBytes); Array.Reverse(idAsBytes); } byte[] result = NetworkingUtils.CombineBytes(cmdTypeAsBytes, idAsBytes); int lengthOfMsg = result.Length * sizeof(byte); // in bytes, one int, just for the command type and the id byte[] lengthAsBytes = BitConverter.GetBytes(lengthOfMsg); if (BitConverter.IsLittleEndian) { Array.Reverse(lengthAsBytes); } result = NetworkingUtils.CombineBytes(lengthAsBytes, result); DataStreamWriter data = new DataStreamWriter(sizeof(byte) * result.Length, Allocator.Temp); data.Write(result); id[0] += 1; driver.Send(NetworkPipeline.Null, serverConnection.connection, data); commandBuffer.SetComponent(index, entity, new NetworkServerConnection { networkId = networkId }); } } else if (command == NetworkEvent.Type.Disconnect) { commandBuffer.DestroyEntity(index, entity); } } }
public void Execute(Entity entity, int index, ref NetworkClientConnection clientConnection) { if (!serverEndPoint.IsValid) { // TODO: disconnect if the server is not running on the end point commandBuffer.DestroyEntity(index, entity); } else { DataStreamReader stream; NetworkEvent.Type command; while ((command = driver.PopEventForConnection(clientConnection.connection, out stream)) != NetworkEvent.Type.Empty) { if (command == NetworkEvent.Type.Connect) { int lengthOfMsg = sizeof(int); // in bytes, one int, just for the command type, not including the length integer itself byte[] lengthAsBytes = BitConverter.GetBytes(lengthOfMsg); byte[] cmdTypeAsBytes = BitConverter.GetBytes(CommandType.SpawnNewPlayer); if (BitConverter.IsLittleEndian) { Array.Reverse(lengthAsBytes); Array.Reverse(cmdTypeAsBytes); } byte[] result = NetworkingUtils.CombineBytes(lengthAsBytes, cmdTypeAsBytes); DataStreamWriter data = new DataStreamWriter(sizeof(byte) * result.Length, Allocator.Temp); data.Write(result); Debug.Log("size of result " + sizeof(byte) * result.Length); Debug.Log("Sending confirmation of connection to server"); driver.Send(NetworkPipeline.Null, clientConnection.connection, data); } else if (command == NetworkEvent.Type.Data) { /* * Structure of a message to be sent over the server * [size of message, not including this part, sizeof(int)][command type sizeof(int)][other data] */ DataStreamReader.Context readerCtx = default(DataStreamReader.Context); byte[] lengthOfDataAsBytes = stream.ReadBytesAsArray(ref readerCtx, sizeof(int)); if (BitConverter.IsLittleEndian) { Array.Reverse(lengthOfDataAsBytes); } int lengthOfData = BitConverter.ToInt32(lengthOfDataAsBytes, 0); byte[] dataRead = stream.ReadBytesAsArray(ref readerCtx, lengthOfData); byte[] cmdTypeRead = new byte[sizeof(int)]; Buffer.BlockCopy(dataRead, 0, cmdTypeRead, 0, sizeof(int)); if (BitConverter.IsLittleEndian) { Array.Reverse(cmdTypeRead); } int commandType = BitConverter.ToInt32(cmdTypeRead, 0); if (commandType == CommandType.SpawnNewPlayer) { byte[] idAsBytes = new byte[sizeof(int)]; Buffer.BlockCopy(dataRead, sizeof(int), idAsBytes, 0, sizeof(int)); if (BitConverter.IsLittleEndian) { Array.Reverse(idAsBytes); } int id = BitConverter.ToInt32(idAsBytes, 0); Debug.Log("player id from server " + id); commandBuffer.SetComponent(index, entity, new NetworkClientConnection { networkId = id }); } } else if (command == NetworkEvent.Type.Disconnect) { commandBuffer.DestroyEntity(index, entity); } } } }