Exemplo n.º 1
0
        private async void EstablishClient(LobbySorter lobbySorter)
        {
            //Wait for client info object
            object receivedObject;

            do
            {
                //Wait for object
                receivedObject = await standardizedTCP.ReadObject();

                //Ensure no error
                if (receivedObject == null)
                {
                    //Error in connection, close
                    Close(true, false);
                    return;
                }
            }while (!(receivedObject is ClientInfoMessage));

            //Cast to client info
            clientInfo = ((ClientInfoMessage)receivedObject).ClientInfo;

            //Send client to lobbies
            ServerInfo serverInfo;

            lobby       = lobbySorter.Invoke(this, out serverInfo);
            ClientIndex = serverInfo.ClientIndex;

            //Send server information
            SendMessage(new ServerInfoMessage(serverInfo));

            //Send client information
            lobby.ForwardToClients(this, new PeerClientInfoMessage(PeerClientInfo));

            //Request client information
            lobby.SendClientInformation(this);

            //Start client readers
            ClientReaderTCP();
            ClientReaderUDP();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a new client.
        /// </summary>
        /// <param name="tcpClient">The tcp client of the client.</param>
        public Client(TcpClient tcpClient, LobbySorter lobbySorter, Print printMethod)
        {
            this.tcpClient   = tcpClient;
            this.printMethod = printMethod;

            //Set standard stream
            standardizedTCP = new StandardizedTCP(tcpClient.GetStream());

            //Get standard udp
            IPAddress address = ((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address;

            try
            {
                standardizedUDP = new StandardizedUDP(new IPEndPoint(address, SERVER_UDP_RECEIVE), new IPEndPoint(address, SERVER_UDP_SEND));
            }
            catch
            {
                standardizedUDP = new StandardizedUDP(new IPEndPoint(address, SERVER_UDP_RECEIVE_ALT), new IPEndPoint(address, SERVER_UDP_SEND_ALT));
            }

            //Establish client
            EstablishClient(lobbySorter);
        }