.Concat(new byte[] { (byte)buffer.Length }).Concat(buffer).ToArray(); // [servercode][nickname length][nickname bytes] public static void Receive() { NetworkStream stream = Client.GetStream(); while (true) { // the first byte we recieve tells us how long the message is. byte[] readAmt = new byte[1]; stream.Read(readAmt, 0, 1); if (readAmt[0] == 0) { continue; } // read that many bytes from the stream byte[] buffer = new byte[readAmt[0]]; stream.Read(buffer, 0, buffer.Length); // deserialize the message object ChatMessage msg = ChatMessage.FromByteArray(buffer); Console.WriteLine($"\n{msg.Source} ~> {msg}"); } }
private void ProcessMessage(Byte[] Message) { switch (Messaging.GetMessageCommand(Message)) { case Command.SendMsg: Byte[] Bytes = Messaging.GetData(Message); ChatMessage Msg = ChatMessage.FromByteArray(Bytes); String strText = Msg.UserName + " - " + Msg.MessageText; AddMessage(strText); break; } }
private void StartClient() { TankInputModule.Instance.Init(state => Send(Env.ServerName, state)); for (var i = 0; i < 15; i++) { Send(i + "." + Env.ServerName, new JoinEvent()); } _client.OnMessage += (src, data) => UnityMainThreadDispatcher.Instance().Enqueue(() => { Env.ServerName = src; switch (GetMessageType(data)) { case MessageType.InitialTankStates: Debug.Log(src); Debug.Log(_tanks); goto case MessageType.TankStates; case MessageType.TankStates: var tanks = TankStates.FromByteArray(data); for (var i = 0; i < tanks.States.Length; i++) { var state = tanks.States[i]; TankManager tank; if (tanks.Type == MessageType.InitialTankStates) { tank = AddTank(state.publicKey); if (state.publicKey.Equals(_publicKey)) { HUDController.Instance.InitHudPlayer(tank.Health); tank.Follow(); } } else { tank = _tanksList[i]; } if (state.respawned) { tank.Respawn( new Vector3(state.moveX, 0, state.moveY), new Vector3(0, state.rotation.Value, 0) ); } else { tank.SetDesiredMovementPosition(new Vector2(state.moveX, state.moveY)); } tank.SetDesiredFirePosition(new Vector3(state.fireX, 0, state.fireY)); tank.SetFireIsHeld(state.fire); } break; case MessageType.ChatMessage: var message = ChatMessage.FromByteArray(data); UIChat.ShowMessage(message.Message); break; case MessageType.JoinEvent: throw new ArgumentOutOfRangeException(); case MessageType.MovementState: throw new ArgumentOutOfRangeException(); default: throw new ArgumentOutOfRangeException(); } }); }
public void Worker() { NetworkStream stream = Client.GetStream(); while (true) { // the first byte is a command telling the server what to expect byte[] commands = new byte[1]; try { stream.Read(commands, 0, 1); } catch (System.IO.IOException except) { Console.WriteLine($"{Client.Client.RemoteEndPoint} ({Nickname}) has disconnected."); Clients.Remove(this); stream.Dispose(); Client.Close(); Client.Dispose(); break; } if (commands[0] == 0) { continue; // no command recieved, do not proceed } TransmissionType code = (TransmissionType)commands[0]; switch (code) { // The client wants to send a message case TransmissionType.Message: // the second byte we recieve tells us how long the message is byte[] readAmt = new byte[1]; stream.Read(readAmt, 0, 1); // read that many bytes from the stream byte[] buffer = new byte[readAmt[0]]; stream.Read(buffer, 0, buffer.Length); // deserialize the message object ChatMessage msg = ChatMessage.FromByteArray(buffer); // find the destination client bool resolved = false; foreach (ClientHandler remoteClient in Clients) { if (remoteClient.Nickname == msg.Destination) { // we found our destination, send him the message msg.Source = Nickname; remoteClient.Send(msg); Console.WriteLine($"Routed {msg.Source} ~> {msg.Destination}"); resolved = true; break; } } if (!resolved) { Console.WriteLine("Error: Message routed to a non-existing client."); } break; // The client want to set his nickname case TransmissionType.Nickname: string oldNickname = Nickname; // the client wants to set his nickname // this byte tells us how long the message is. byte[] amount = new byte[1]; stream.Read(amount, 0, 1); buffer = new byte[amount[0]]; stream.Read(buffer, 0, amount[0]); Nickname = Encoding.UTF8.GetString(buffer); Console.WriteLine($"{Client.Client.RemoteEndPoint} ({oldNickname}) has changed his nickname to {Nickname}."); break; } } }
private void StartServer() { _isServer = true; LocalTank = AddTank(_publicKey); LocalTank.Follow(); _client.OnMessage += (src, data) => UnityMainThreadDispatcher.Instance().Enqueue(() => { switch (GetMessageType(data)) { case MessageType.JoinEvent: AddTank(src); SendStates(true); break; case MessageType.MovementState: var tank = _tanks[src]; var state = MovementState.FromByteArray(data); var cameraDirection = new Vector3(state.moveX, state.moveY, 0); if (cameraDirection.sqrMagnitude > 0.01f) { var worldUp = Camera.main.transform.TransformDirection(Vector3.up); worldUp.y = 0; worldUp.Normalize(); var worldRight = Camera.main.transform.TransformDirection(Vector3.right); worldRight.y = 0; worldRight.Normalize(); var worldDirection = worldUp * state.moveY + worldRight * state.moveX; var desiredDir = new Vector2(worldDirection.x, worldDirection.z); if (desiredDir.magnitude > 1) { desiredDir.Normalize(); } tank.SetDesiredMovementDirection(new Vector2(state.moveX, state.moveY)); } else { tank.SetDesiredMovementDirection(Vector2.zero); } tank.SetDesiredFirePosition(new Vector3(state.fireX, 0, state.fireY)); tank.SetFireIsHeld(state.fire); break; case MessageType.ChatMessage: var message = ChatMessage.FromByteArray(data); UIChat.ShowMessage(message.Message); break; case MessageType.InitialTankStates: throw new ArgumentOutOfRangeException(); case MessageType.TankStates: throw new ArgumentOutOfRangeException(); default: throw new ArgumentOutOfRangeException(); } }); TankInputModule.Instance.Init(LocalTank); HUDController.Instance.InitHudPlayer(LocalTank.Health); }