private static void ClientSocketGame_OnMessage(object sender, MessageEventArgs e) { if (e.IsBinary) { NetPackett packet = new NetPackett(); packet = Serializator.DeserializeNetPackett(e.RawData); receiving.Add(packet); } }
// NETPACKETT // custom modified to read Message Type public static byte[] serialize(NetPackett netpackett) { var s = new MemoryStream(); var bW = new BinaryWriter(s); bW.Write((int)netpackett.messageType); bW.Write(netpackett.data.Length); foreach (var item in netpackett.data) { bW.Write(item); } return(s.ToArray()); }
// custom modified to read Message Type private static NetPackett DeserializeNetPackett(ref byte[] b, ref MemoryStream s, ref BinaryReader bR) { var obj = new NetPackett(); obj.messageType = (MessageType)bR.ReadInt32(); int dataArraySize = bR.ReadInt32(); obj.data = new Byte[dataArraySize]; for (int i = 0; i < dataArraySize; i++) { obj.data[i] = bR.ReadByte(); } return(obj); }
// custom modified to read Message Type public static NetPackett DeserializeNetPackett(byte[] b) { var s = new MemoryStream(b); var bR = new BinaryReader(s); var obj = new NetPackett(); obj.messageType = (MessageType)bR.ReadInt32(); int dataArraySize = bR.ReadInt32(); obj.data = new Byte[dataArraySize]; for (int i = 0; i < dataArraySize; i++) { obj.data[i] = bR.ReadByte(); } return(obj); }
public void startMatchmaking() { // disabling elo input and joining matchmaking Net.myElo = inputElo.text; btnJoinGame.gameObject.SetActive(false); inputElo.gameObject.SetActive(false); textMatchmaking.gameObject.SetActive(true); NetPackett netPackett = new NetPackett() { data = Serializator.serialize(Net.myElo), messageType = MessageType.SendingElo }; Net.sendLobbyPacket(netPackett); }
private void Moving() { if (myPlayerIndex != -1) { var myPlayer = players[myPlayerIndex].transform; var direction = Vector3.zero; if (Input.GetKey(KeyCode.W)) { direction += Vector3.forward; } if (Input.GetKey(KeyCode.A)) { direction += Vector3.left; } if (Input.GetKey(KeyCode.S)) { direction += Vector3.back; } if (Input.GetKey(KeyCode.D)) { direction += Vector3.right; } direction.Normalize(); if (direction != Vector3.zero) { var position = myPlayer.position + direction * myMovementSpeed * Time.deltaTime; //Vector3 relativePos = position - myPlayer.position; myPlayer.position = position; NetPackett packett = new NetPackett { messageType = MessageType.ClientMoved, data = Serializator.serialize(new PlayerPosition() { playerIndex = myPlayerIndex, matchID = Net.myMatchID, posX = myPlayer.position.x, posY = myPlayer.position.y, posZ = myPlayer.position.z }) }; Net.sendGamePacket(packett); } } }
public static void sendGamePacket(NetPackett packet) { clientSocketGame.Send(Serializator.serialize(packet)); }
public static void sendLobbyPacket(NetPackett packet) { clientSocketLobby.Send(Serializator.serialize(packet)); }