示例#1
0
        void ClientConnect(string hostname, ushort port)
        {
            if (client != null)
            {
                return;
            }

            // libuv doesn't resolve host name, and it needs ipv4.
            if (LibuvUtils.ResolveToIPV4(hostname, out IPAddress address))
            {
                // connect client
                IPEndPoint localEndPoint  = new IPEndPoint(IPAddress.Any, IPEndPoint.MinPort);
                IPEndPoint remoteEndPoint = new IPEndPoint(address, port);

                Debug.Log("Libuv connecting to: " + address + ":" + port);
                client = new TcpStream(clientLoop);
                client.NoDelay(NoDelay);
                client.onClientConnect = OnLibuvClientConnected;
                client.onMessage       = OnLibuvClientMessage;
                client.onError         = OnLibuvClientError;
                client.onClosed        = OnLibuvClientClosed;
                client.ConnectTo(localEndPoint, remoteEndPoint);
            }
            else
            {
                Debug.LogWarning("Libuv Connect: no IPv4 found for hostname: " + hostname);
            }
        }
示例#2
0
        /// <summary>
        ///     Connect to server.
        /// </summary>
        /// <param name="uri">The server <see cref="Uri"/> to connect to.</param>
        /// <returns>Returns back a new <see cref="Libuv2kConnection"/> when connected or null when failed.</returns>
        public async UniTask <IConnection> ConnectAsync(Uri uri)
        {
            try
            {
                // libuv doesn't resolve host name, and it needs ipv4.
                if (LibuvUtils.ResolveToIPV4(uri.Host, out IPAddress address))
                {
                    // connect client
                    var localEndPoint  = new IPEndPoint(IPAddress.Any, IPEndPoint.MinPort);
                    var remoteEndPoint = new IPEndPoint(address, uri.Port);

                    Libuv2kNGLogger.Log("Libuv client connecting to: " + address + ":" + uri.Port);

                    _client.onClientConnect = ConnectedAction;
                    _client.ConnectTo(localEndPoint, remoteEndPoint);
                }
                else
                {
                    Libuv2kNGLogger.Log("Libuv client connect: no IPv4 found for hostname: " + uri.Host, LogType.Warning);
                }

                _connectedComplete = new UniTaskCompletionSource();
                UniTask connectedCompleteTask = _connectedComplete.Task;

                while (await UniTask.WhenAny(connectedCompleteTask,
                                             UniTask.Delay(TimeSpan.FromSeconds(Math.Max(1, 30)))) != 0)
                {
                    Disconnect();

                    return(null);
                }

                Libuv2kNGLogger.Log("Libuv client connected to: " + address + ":" + uri.Port);

                return(this);
            }
            catch (Exception ex)
            {
                Libuv2kNGLogger.Log($"Error trying to attempting to connect. Error: {ex}", LogType.Error);

                Disconnect();
            }

            return(null);
        }