Exemplo n.º 1
0
        public bool Connect(ClientInfo clientInfo)
        {
            tcpClient = new TcpClient();

            //Connect
            IPAddress ipAddress = null;

            if (CurrentConnectionType == ConnectionType.Public)
            {
                ipAddress = IPAddress.Parse(GetPublicIPAddress());
            }
            else if (CurrentConnectionType == ConnectionType.Private)
            {
                if (!IPAddress.TryParse(PrivateServerIP, out ipAddress))
                {
                    return(false);
                }
            }

            try
            {
                //Connect tcp
                tcpClient.Connect(ipAddress, PUBLIC_SERVER_PORT_TCP);
            }
            catch
            {
                return(false);
            }

            try
            {
                standardizedUDP = new StandardizedUDP(new IPEndPoint(ipAddress, CLIENT_UDP_RECEIVE), new IPEndPoint(ipAddress, CLIENT_UDP_SEND));
            }
            catch
            {
                try
                {
                    standardizedUDP = new StandardizedUDP(new IPEndPoint(ipAddress, CLIENT_UDP_RECEIVE_ALT), new IPEndPoint(ipAddress, CLIENT_UDP_SEND_ALT));
                }
                catch
                {
                    return(false);
                }
            }

            standardizedTCP = new StandardizedTCP(tcpClient.GetStream());

            enabled = true;

            //Send client info
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
            standardizedTCP.SendMessage(new ClientInfoMessage(clientInfo));
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed

            //Start accepting messages
            AcceptMessage();
            AcceptMessageUDP();

            return(true);
        }
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);
        }