private async void Communicate(TCPConnection connection)
    {
        try {
            while (connection.active)
            {
                List <byte> data = await connection.Receive();

                string channel = data.ReadString();
                if (channel == "disconnection")
                {
                    connection.active = false;
                }
                else
                {
                    //received a message from the host!
                    onMessageReception.Invoke(connection, channel, data);
                }
            }
            onConnectionEnd.Invoke(connection);
            hosts.Remove(connection);
        }catch (SocketException se) {
            Haze.Logger.LogError("[TCPClient] Socket Exception (" + se.ErrorCode + "), cannot communicate with host: " + se.ToString(), this);
        }catch (Exception e) {
            Haze.Logger.LogError("[TCPClient] Error, cannot communicate with host: " + e.ToString(), this);
        }
    }
Beispiel #2
0
    private async void Communicate(TCPConnection connection)
    {
        while (connection.active)
        {
            List <byte> data = await connection.Receive();

            if (data.Count > 0)
            {
                string channel = data.ReadString();
                if (channel == "disconnection")
                {
                    connection.active = false;
                }
                else
                {
                    //received a message from the host!
                    onMessageReception.Invoke(connection, channel, data);
                }
            }
            else
            {
                Haze.Logger.LogWarning("Received data with length == 0 from connection " + connection);
                connection.active = false;
            }
        }

        //Attempt to send a "disconnection" message before closing
        {
            List <byte> data = new List <byte>();
            data.WriteString("disconnection");
            try {
                await connection.Send(data);
            } catch (Exception e) {
                //nevermind.
            }
        }

        onConnectionEnd.Invoke(connection);
        users.Remove(connection);
        if (!connection.UDP)
        {
            connection.client.Close();
        }
    }
Beispiel #3
0
    async void Start()
    {
        Instance = this;
        if (broadcaster)
        {
            string      hostName  = Dns.GetHostName();
            IPHostEntry hostEntry = await Dns.GetHostEntryAsync(hostName);

            int ipIndex = -1;
            for (int i = 0; i < hostEntry.AddressList.Length; ++i)
            {
                IPAddress thisOne = hostEntry.AddressList[i];
                if (thisOne.AddressFamily != AddressFamily.InterNetwork && thisOne.IsIPv4MappedToIPv6)
                {
                    thisOne = thisOne.MapToIPv4();
                }
                Haze.Logger.Log("Address " + i + " = " + thisOne);
                if (thisOne.GetAddressBytes()[0] == (byte)192 && thisOne.GetAddressBytes()[1] == (byte)168)
                {
                    ipIndex = i;
                    break;
                }
            }
            IPAddress ip;
            if (ipIndex > -1)
            {
                ip = hostEntry.AddressList[ipIndex];
            }
            else
            {
                ip = IPAddress.Any;
            }

            if (ip.AddressFamily != AddressFamily.InterNetwork)
            {
                ip = ip.MapToIPv4();
            }

            Haze.Logger.Log("Ip: " + ip);

            listener = new TcpListener(ip, 0);
            listener.Start();
            IPEndPoint listenerLocalEndpoint = (IPEndPoint)listener.LocalEndpoint;

            broadcaster.StartBroadcasting(listenerLocalEndpoint.Address.ToString(), listenerLocalEndpoint.Port);
            Haze.Logger.Log("Broadcasting ip " + ip.ToString() + " and port " + listenerLocalEndpoint.Port);

            while (!stop)
            {
                try {
                    Haze.Logger.Log("Awaiting a connection request...");
                    TcpClient client = await listener.AcceptTcpClientAsync();

                    client.NoDelay = true;
                    IPEndPoint localEndpoint  = (IPEndPoint)client.Client.LocalEndPoint;
                    IPEndPoint remoteEndpoint = (IPEndPoint)client.Client.RemoteEndPoint;
                    Haze.Logger.Log("Accepted connection from " + remoteEndpoint.Address + " (port " + remoteEndpoint.Port + "), from address " + localEndpoint.Address + " (port " + localEndpoint.Port + ")");
                    TCPConnection connection = new TCPConnection();
                    connection.client = client;

                    //wait for identification message:
                    List <byte> data = await connection.Receive();

                    string channel = data.ReadString();
                    if (channel != "identification")
                    {
                        Haze.Logger.LogWarning("Device at " + remoteEndpoint.Address + " has responded with an illegal channel: " + channel);
                    }
                    else
                    {
                        connection.deviceType    = (TCPConnection.DeviceType)data.ReadByte();
                        connection.uniqueId      = data.ReadString();
                        connection.lockedId      = data.ReadString();
                        connection.xrDeviceModel = data.ReadString();

                        users.Add(connection);

                        onNewConnection.Invoke(connection);

                        Communicate(connection);
                    }
                }catch (SocketException se) {
                    Haze.Logger.LogWarning("Socket error (" + se.ErrorCode + "), could not accept connection: " + se.ToString());
                    Haze.Logger.LogWarning("Attempting to restart TCPHost::Start()");
                    listener.Stop();
                    listener = null;
                    Start();                    //retry
                    return;
                }catch (Exception e) {
                    if (listener != null)
                    {
                        Haze.Logger.LogWarning("Error, could not accept connection: " + e.ToString());
                    }
                    //else just means that we're exiting Unity.
                }
            }
        }
        else
        {
            Haze.Logger.LogError("No broadcaster...");
        }
    }