public int AddClientToRoom(RelayClient client, int roomId) { Room room = rooms[roomId]; int clientId = room.GetNextClientId(); room.clients.Add(clientId, client); return(clientId); }
public int CreateRoom(out int clientId, RelayClient client, string password, string name) { int roomId = GetNextRoomId(); Room room = new Room() { RoomId = roomId, Password = password, Name = name }; clientId = room.GetNextClientId(); room.clients.Add(clientId, client); room.HostClientId = clientId; rooms.Add(roomId, room); Console.WriteLine("Created room " + roomId + " for client " + clientId); return(roomId); }
private void HandleDisconnectRemoteClient(int remoteClientId) { if (!IsHost) { throw new Exception("client that is not host tried to disconnect remote client"); } RelayClient clientToDisconnect = null; lock (Program.roomManager) { RoomManager.Room room = Program.roomManager.GetRoom(ConnectedRoomId); clientToDisconnect = room.clients[remoteClientId]; } if (clientToDisconnect != null) { clientToDisconnect.Stop(); } }
private void HandleDisconnect() { if (!disconnected && ConnectedRoomId != -1) { disconnected = true; List <RelayClient> clientsToStop = new List <RelayClient>(); lock (Program.roomManager) { RoomManager.Room room = Program.roomManager.GetRoom(ConnectedRoomId); if (IsHost) { foreach (RelayClient client in room.clients.Values) { if (client != this && ClientId != -1) { client.SendClientDisconnected(ClientId); } clientsToStop.Add(client); } Program.roomManager.DestroyRoom(ConnectedRoomId); } else { RelayClient hostClient = room.clients[room.HostClientId]; room.clients.Remove(ClientId); hostClient.SendClientDisconnected(ClientId); clientsToStop.Add(this); } ConnectedRoomId = -1; } if (clientsToStop.Count > 0) { Thread.Sleep(200); foreach (RelayClient client in clientsToStop) { client.Stop(); } } } }
private void HandleSendToClient(int toClientId, byte channelId, byte[] payload) { lock (Program.roomManager) { RoomManager.Room room = Program.roomManager.GetRoom(ConnectedRoomId); RelayClient targetClient = room.clients[toClientId]; using (MemoryStream ms = new MemoryStream()) { using (BinaryWriter bw = new BinaryWriter(ms)) { bw.Write((byte)CommandType.ReceiveFromClient); bw.Write(ClientId); bw.Write(channelId); bw.Write(payload); targetClient.SendPacket(ms.ToArray()); } } } }
private void HandleStartClient(int roomId, string password) { ConnectedRoomId = roomId; lock (Program.roomManager) { RoomManager.Room room = Program.roomManager.GetRoom(roomId); if (room.Password == password) { ClientId = Program.roomManager.AddClientToRoom(this, roomId); RelayClient hostClient = room.clients[room.HostClientId]; hostClient.SendClientConnected(ClientId); SendStartClientResponse(ClientId, room.HostClientId); } else { Console.WriteLine("Client connected with wrong password"); HandleDisconnect(); } } }
public static void Main(string[] args) { if (args.Length >= 1) { ListenIPAddress = args[0]; } if (args.Length >= 2) { ListenPort = int.Parse(args[1]); } TcpListener listener = new TcpListener(IPAddress.Parse(ListenIPAddress), ListenPort); listener.Server.Blocking = true; listener.Start(); Console.WriteLine("Server started on " + ListenIPAddress + " port " + ListenPort); while (true) { TcpClient client = listener.AcceptTcpClient(); RelayClient rclient = new RelayClient(client); rclient.Start(); } }