Пример #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);
        }
Пример #2
0
 public async void SendMessage(IClientMessage messageToSend, bool sendTCP = true)
 {
     if (enabled)
     {
         if (sendTCP)
         {
             await standardizedTCP.SendMessage(messageToSend);
         }
         else
         {
             await standardizedUDP.SendMessage(messageToSend);
         }
     }
 }
Пример #3
0
 public async void SendMessage(INetworkMessage networkMessage, bool sendTCP = true)
 {
     if (enabled)
     {
         if (sendTCP)
         {
             if (!await standardizedTCP.SendMessage(networkMessage))
             {
                 //Error while sending
                 Disable();
             }
         }
         else
         {
             if (!await standardizedUDP.SendMessage(networkMessage))
             {
                 //Error while sending
                 Disable();
             }
         }
     }
 }