Exemplo n.º 1
0
        private void UdpWorker(CancellationToken token)
        {
            log.LogInformation($"UDP listener started on port {udpEndpoint.Port}.");

            while (!token.IsCancellationRequested)
            {
                try
                {
                    byte[] receivedBytes = Udp.Receive(ref udpEndpoint);

                    if (receivedBytes != null)
                    {
                        // Retrieve the message from the network stream. This will handle everything from message headers, body and type parsing.
                        Platform.Networking.Messages.BaseMessage message = messageSerializer.Deserialize(receivedBytes);

                        // Don't log KeepAlive messages.
                        if (message.Command != 3)
                        {
                            // Log all message as JSON output during development, simplifies debugging.
                            log.LogInformation("UDP:" + System.Environment.NewLine + JsonConvert.SerializeObject(message));
                        }

                        messageProcessing.Process(message, ProtocolType.Udp, udpEndpoint);
                    }
                }
                catch (Exception ex)
                {
                    log.LogError(ex, "UDP error");
                }
            }
        }
Exemplo n.º 2
0
        private void TcpWorker(CancellationToken token)
        {
            StartTcp();

            while (!token.IsCancellationRequested)
            {
                try
                {
                    TcpClient newClient = Tcp.AcceptTcpClient();

                    Action <object> processData = new Action <object>(delegate(object tcp)
                    {
                        TcpClient client = (TcpClient)tcp;
                        client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);

                        while (client.Connected)
                        {
                            try
                            {
                                // Retrieve the message from the network stream. This will handle everything from message headers, body and type parsing.
                                Platform.Networking.Messages.BaseMessage message = messageSerializer.Deserialize(client.GetStream());

                                // Don't log KeepAlive messages.
                                if (message.Command != 3)
                                {
                                    // Log all message as JSON output during development, simplifies debugging.
                                    log.LogInformation("UDP:" + System.Environment.NewLine + JsonConvert.SerializeObject(message));
                                }

                                messageProcessing.Process(message, ProtocolType.Tcp, null, client);
                            }
                            catch (Exception ex)
                            {
                                log.LogError(ex, "Failed to process incoming message.");
                                Disconnect(client);
                            }
                        }

                        Disconnect(client);
                    });

                    Thread threadProcessData = new Thread(new ParameterizedThreadStart(processData));
                    threadProcessData.Start(newClient);
                }
                catch (Exception ex)
                {
                    log.LogError(ex, "TCP error");

                    // We'll sleep a short while before connecting, to avoid extensive resource usage.
                    Thread.Sleep(250);
                }
            }
        }