Esempio n. 1
0
    public async Task <ILog> Connect()
    {
        if (socket == null)
        {
            return(new Error("[TCP] Client cannot connect without a socket defined"));
        }
        if (remoteEndPoint == null)
        {
            return(new Error("[TCP] Client remote end point not provided"));
        }
        if (clientState.Connected)
        {
            return(new Error("[TCP] Client already connected"));
        }
        if (clientState.Connecting)
        {
            return(new Error("[TCP] Client already busy connecting"));
        }

        clientState.Connecting = true;
        try
        {
            await socket.ConnectAsync(remoteEndPoint.IPEndPoint.Address, remoteEndPoint.IPEndPoint.Port);

            // Debug.LogFormat("[TCP] {0} -> {1}", socket.LocalEndPoint, socket.RemoteEndPoint);
            clientState.Connecting = false;
            if (!socket.Connected)
            {
                return(new Error("[TCP] Client connected but weird that the socket state is stil not connected"));
            }
            // all good
            clientState.Connected = true;
            connectionHandler.HandleConnect();
            return(null);
        }
        catch (Exception n)
        {
            clientState.Connecting = false;
            return(new Error("[TCP] Client failed to connect to remote server: " + n));
        }
    }