Exemple #1
0
    // RECEIVING CODE

    private void Update()
    {
        try
        {
            if (_client.Available > 0)
            {
                byte[]        inBytes  = StreamUtil.Read(_client.GetStream());
                Packet        inPacket = new Packet(inBytes);
                ISerializable inObj    = inPacket.ReadObject();

                if (inObj is GetMessage)
                {
                    handleMessage(inObj as GetMessage);
                }
                else if (inObj is GetAvatars)
                {
                    handleNewAvatar(inObj as GetAvatars);
                }
                else if (inObj is HeartBeat)
                {
                }
                else if (inObj is RemoveAvatar)
                {
                    removeAvatar(inObj as RemoveAvatar);
                }
            }
        }
        catch (Exception e)
        {
            //for quicker testing, we reconnect if something goes wrong.
            Debug.Log(e.Message);
            _client.Close();
            connectToServer();
        }
    }
Exemple #2
0
    // RECEIVING CODE
    private void Update()
    {
        try {
            if (client.Available > 0)
            {
                //we are still communicating with strings at this point, this has to be replaced with either packet or object communication
                byte[] inBytes = StreamUtil.Read(client.GetStream());
                var    obj     = SerializationHelper.Deserialize(inBytes);
                ProcessObject(obj);
            }
        } catch (Exception e) {
            //for quicker testing, we reconnect if something goes wrong.
            Debug.Log(e.Message);
            client.Close();
            ConnectToServer();
        }

        if (!receivedUserId)
        {
            return;
        }
        timeSinceLastHeartbeat += Time.deltaTime;
        if (timeSinceLastHeartbeat >= serverTimeout / 2.0f)
        {
            SendObject(new Heartbeat());
        }
    }
    private void processExistingClients()
    {
        foreach (KeyValuePair <TcpClient, AvatarObject> client in clientDict)
        {
            try
            {
                if (client.Key.Available == 0)
                {
                    continue;
                }

                byte[]        inBytes  = StreamUtil.Read(client.Key.GetStream());
                Packet        inPacket = new Packet(inBytes);
                ISerializable inObj    = inPacket.ReadObject();
                Console.WriteLine($"{DateTime.Now.ToString("hh:mm:ss")} Received: {inObj} from client id: {client.Value}");

                if (inObj is SimpleMessage)
                {
                    handleMessage(client.Key, inObj as SimpleMessage);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"{DateTime.Now.ToString("hh: mm:ss")} {e.Message}");
            }
        }
    }
    // RECEIVING CODE

    private void Update()
    {
        try
        {
            if (_client.Available > 0)
            {
                byte[]        inBytes  = StreamUtil.Read(_client.GetStream());
                Packet        inPacket = new Packet(inBytes);
                ISerializable inObject = inPacket.ReadObject();

                if (inObject is SetClientParameters)
                {
                    setLocalClient(inObject as SetClientParameters);
                }
                else if (inObject is SimpleMessage)
                {
                    showMessage(inObject as SimpleMessage);
                }
            }
        }
        catch (Exception e)
        {
            //for quicker testing, we reconnect if something goes wrong.
            Debug.Log(e.Message);
            _client.Close();
            connectToServer();
        }
    }
 private void Update()
 {
     if (_client.Available > 0)
     {
         byte[] inBytes  = StreamUtil.Read(_client.GetStream());
         string inString = Encoding.UTF8.GetString(inBytes);
         _panelWrapper.AddOutput(inString);
     }
 }
Exemple #6
0
    private void Update()
    {
        try {
            if (client.Available > 0)
            {
                var stream   = client.GetStream();
                var inBytes  = StreamUtil.Read(stream);
                var received = Encoding.UTF8.GetString(inBytes);
                ProcessMessage(received);
            }
        } catch (Exception e) {
            panelWrapper.AddOutput(e.Message);
        }

        var currentTime = DateTime.Now;

        if (serverTimeout.HasValue && (currentTime - lastHeartbeatTime).Seconds > serverTimeout.Value / 2.0f)
        {
            EmitHeartbeat();
        }
    }
    private void processNewClients()
    {
        while (_listener.Pending())
        {
            TcpClient clientToAdd = _listener.AcceptTcpClient();

            byte[]        inBytes  = StreamUtil.Read(clientToAdd.GetStream());
            Packet        inPacket = new Packet(inBytes);
            ISerializable inObject = inPacket.ReadObject();

            if (inObject is AddClient)
            {
                LobbyClient newClient = new LobbyClient(clientToAdd, currentClientId++);

                SetClientParameters newClientParameters = new SetClientParameters();
                newClientParameters.id = newClient.id;

                newClientParameters.skin = newClient.id;
                //newClientParameters.skin = new System.Random().Next(1000)+_clients.Count;

                Console.WriteLine("joined client with id: " + newClientParameters.id + " and skin " + newClientParameters.skin);

                foreach (LobbyClient client in _clients)
                {
                    sendObject(client.TcpClient, newClientParameters);
                }

                _clients.Add(newClient);

                foreach (LobbyClient client in _clients)
                {
                    SetClientParameters parameters = new SetClientParameters();
                    parameters.id   = client.id;
                    parameters.skin = client.id;
                    sendObject(newClient.TcpClient, parameters);
                }
            }
            Console.WriteLine("Accepted new client.");
        }
    }
    private void processExistingClients()
    {
        for (int i = 0; i < _clients.Count; i++)
        {
            LobbyClient client = _clients[i];

            if (client.TcpClient.Available == 0)
            {
                continue;
            }

            byte[]        inBytes  = StreamUtil.Read(client.TcpClient.GetStream());
            Packet        inPacket = new Packet(inBytes);
            ISerializable inObject = inPacket.ReadObject();
            Console.WriteLine("Received:" + inObject);

            if (inObject is SimpleMessage)
            {
                SimpleMessage message = inObject as SimpleMessage;
                handleSimpleMessage(client, message);
            }
        }
    }
Exemple #9
0
    public void ProcessClients()
    {
        foreach (var client in Clients)
        {
            if (client.Key.Available <= 0)
            {
                continue;
            }
            var stream   = client.Key.GetStream();
            var inBytes  = StreamUtil.Read(stream);
            var received = Encoding.UTF8.GetString(inBytes);
            if (Verbose)
            {
                Logger.Info($"Received message [{received}] from client {client.Value}", "INFO-VERBOSE");
            }

            client.Value.OnHeartbeat();
            if (!ProcessSpecial(client.Key, received))
            {
                ProcessMessage(client.Key, received);
            }
        }
    }
Exemple #10
0
 /// <summary>
 /// Reads data from a stream and decrypts it
 /// </summary>
 /// <param name="data_in">The stream to read the encrypted data from</param>
 /// <param name="dkey">Decryption key</param>
 /// <param name="length">Number of bytes to read from stream for decryption</param>
 /// <returns>The raw, decrypted data as a byte array</returns>
 public byte[] Decrypt(Stream data_in, ICipherParameters dkey, int length) => Decrypt(StreamUtil.Read(data_in, length), dkey);
Exemple #11
0
 /// <summary>
 /// Reads data from a stream and encrypts it
 /// </summary>
 /// <param name="data_in">Stream to read from</param>
 /// <param name="ckey">Encryption key</param>
 /// <param name="length">Number of bytes to read from the stream for encryption</param>
 public byte[] Encrypt(Stream data_in, ICipherParameters ckey, int length) => Encrypt(StreamUtil.Read(data_in, length), ckey);
Exemple #12
0
    private void Update()
    {
        if (!connected)
        {
            return;
        }

        if (!_client.Connected)
        {
            connected = false;
            accepted  = false;
            _client.Close();
            _panelWrapper.ClearOutput();
            _panelWrapper.AddOutput("Disconnected from server");
        }

        if (_client.Available == 0)
        {
            return;
        }

        byte[] inBytes  = StreamUtil.Read(_client.GetStream());
        string inString = Encoding.UTF8.GetString(inBytes);

        if (inString.Length > 1)
        {
            if (inString.StartsWith("/"))
            {
                int paramToken = inString.IndexOf(' ');
                if (paramToken == 0)
                {
                    return;
                }

                string   command;
                string[] parameters = null;
                string   parameter  = "";

                if (paramToken < 0)
                {
                    command = inString.Substring(1);
                }
                else
                {
                    command   = inString.Substring(1, paramToken - 1);
                    parameter = inString.Substring(paramToken + 1);
                    if (parameter.Contains(" "))
                    {
                        parameters = parameter.Split(' ');
                    }
                }

                if (command == "accept")
                {
                    accepted = true;
                    _panelWrapper.ClearOutput();
                    return;
                }
                else if (command == "disconnect")
                {
                    connected = false;
                    accepted  = false;
                    _client.Close();
                    _panelWrapper.ClearOutput();
                    _panelWrapper.AddOutput("Disconnected from server");
                    return;
                }
            }
        }

        _panelWrapper.AddOutput(inString);
    }
Exemple #13
0
 public byte[] Read()
 {
     return(StreamUtil.Read(GetStream()));
 }