示例#1
0
 private void HandleSetRoomVisible(bool visible)
 {
     if (!IsHost)
     {
         return;
     }
     lock (Program.roomManager)
     {
         RoomManager.Room room = Program.roomManager.GetRoom(ConnectedRoomId);
         room.Visible = visible;
     }
 }
示例#2
0
        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();
            }
        }
示例#3
0
        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();
                    }
                }
            }
        }
示例#4
0
        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());
                    }
                }
            }
        }
示例#5
0
 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();
         }
     }
 }