Exemplo n.º 1
0
        public bool ConnectToRemote(string nIp)
        {
            if (IPAddress.TryParse(nIp, out IPAddress ip))
            {
                if (Connected)
                {
                    Disconnect();
                }

                // Connect with Tcp first for the handshake
                // Tcp will be used for all KeepAlive and heartbeat communications
                LocalTcpPort = TcpPortPool.GetPort();
                Tcp          = new TcpClient(new IPEndPoint(LocalIp, LocalTcpPort));
                LocalUdpPort = UdpPortPool.GetPort();
                Udp          = new UdpClient(LocalUdpPort);

                // Set TCP connection to immediately close
                LingerOption closeImmediate = new LingerOption(true, 0);
                Tcp.LingerState = closeImmediate;
                Tcp.Connect(ip, RemoteTcpPort);

                if (Tcp.Connected)
                {
                    RemoteIp  = ip;
                    TcpStream = new NetworkStream(Tcp.Client);

                    // No actual active connection with Udp, but we get the party started
                    Udp.Connect(RemoteIp, RemoteUdpPort);
                    Udp.BeginReceive(new AsyncCallback(ReceiveUdp), null);

                    // Prepare to receive Tcp data
                    StartTcpRoutine();

                    Connected = true;

                    // We have liftoff
                    return(true);
                }
                else
                {
                    LocalConsole.Instance.Log("Could not connect to " + ip);
                    return(false);
                }
            }
            else
            {
                LocalConsole.Instance.Log("Unable to parse IP [" + nIp + "]");
                return(false);
            }
        }
Exemplo n.º 2
0
        public void OnEnable()
        {
            if (Instance == null)
            {
                Instance = this;
            }
            else
            {
                CleanUp();
            }

            LocalUdpPort = UdpPortPool.GetPort();
            Udp          = new UdpClient(LocalUdpPort);

            if (!Local)             // Connect to Google DNS to find favored IPEndPoint fast and dirty
            {
                Udp.Connect("8.8.8.8", RemoteUdpPort);
            }
            else                    // Establish a connection to localhost
            {
                Udp.Connect("127.0.0.1", RemoteUdpPort);
            }

            UdpSock = Udp.Client;

            if (UdpSock != null)
            {
                IPEndPoint ep = (IPEndPoint)UdpSock.LocalEndPoint;
                LocalIp = ep.Address;
                Ready   = true;

                if (IsServer)
                {
                    IPEndPoint server = new IPEndPoint(LocalIp, RemoteTcpPort);
                    TcpListen = new TcpListener(server);
                    _serverCo = StartCoroutine(ServerRoutine());
                }

                // _udpCo = StartCoroutine(ClientUdpRoutine());
                // _tcpCo = StartCoroutine(ClientTcpRoutine());
            }
        }