Beispiel #1
0
        private void ListenForData(AtlasNode node)
        {
            var interupted = false;

            while (!interupted)
            {
                var data = new byte[1024 * 1024 * 50];
                try
                {
                    node.Socket.Receive(data);
                }
                catch (SocketException)
                {
                    var message = $"{node.Name} disconnected";
                    _logger.LogWarning(message);
                    Program.LogMessage($"**{message}**").GetAwaiter().GetResult();
                    _nodes.Remove(node);

                    return;
                }

                using (var memoryStream = new MemoryStream(data))
                {
                    var formatter = new BinaryFormatter();
                    var message   = (LogMessage)formatter.Deserialize(memoryStream);
                    HandleMessage(message, ref node);
                }
            }
        }
Beispiel #2
0
        private void HandleMessage(LogMessage message, ref AtlasNode node)
        {
            string textMessage;

            switch (message.MessageType)
            {
            case 0:
                if (message.Message != _token)
                {
                    textMessage =
                        $"{message.Node} tried to register with an invalid token, IP: {(node.Socket.RemoteEndPoint as IPEndPoint)?.Address}";
                    _nodes.Remove(node);
                    _logger.LogCritical(textMessage);
                    Program.LogMessage(textMessage).GetAwaiter().GetResult();
                    node.Socket.Disconnect(true);
                    return;
                }
                textMessage = $"[{message.Node}] [{message.Module}]: {message.Message}";
                _logger.LogInformation(textMessage);
                node.Name = message.Node;
                break;

            case 1:
                textMessage = $"[{message.Node}] [{message.Module}]: {message.Message}";
                _logger.LogInformation(textMessage);
                Program.LogMessage(textMessage).GetAwaiter().GetResult();
                break;
            }
        }
Beispiel #3
0
        public void Listen()
        {
            var ipAddress     = IPAddress.Parse(_ip);
            var localEndPoint = new IPEndPoint(ipAddress, _port);

            _listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            _listener.Bind(localEndPoint);
            _listener.Listen(100);
            _logger.LogInformation($"Listening for nodes on {localEndPoint.Address} {_port}");
            while (true)
            {
                var socket = _listener.Accept();
                var node   = new AtlasNode(_nodes.Count.ToString(), socket);
                _nodes.Add(node);
                new Thread(() => ListenForData(node)).Start();
            }
        }