コード例 #1
0
        async Task NewConnection(IPEndPoint ipEndPoint, MessageType type)
        {
            var list = type switch
            {
                MessageType.HandShake => ConnectServers,
                MessageType.SurfaceHandShake => ConnectSurfaces,
                _ => null
            };

            if (list is null)
            {
                return;
            }
            lock (list)
            {
                if (list.Any(x => Equals(x.Address, ipEndPoint.Address)))
                {
                    return;
                }
                list.Add(ipEndPoint);
            }
            _logger.LogInformation($"Server: New Connection from {ipEndPoint} > {type}");
            if (type != MessageType.HandShake)
            {
                return;
            }
            try
            {
                await SendMessageAsync(ipEndPoint.Address, NetworkConstant.SERVER_PORT, HandShake.CreateMessage(ConnectServers));
            }
            catch (SocketException)
            {
                RemoveEndPoint(ipEndPoint, list);
                await BroadcastEndPointsAsync();
            }
        }

        Task MessageHandle(Message msg, IPEndPoint endPoint)
        {
            _logger.LogInformation($"Server: Message has arrived from {endPoint} : MSG_TYPE: {msg.Type} : {DateTime.Now:ss.FFFF}");
            return(msg.Type switch
            {
                MessageType.HandShake => HandShakeHandle(JsonSerializer.Deserialize <HandShake>(msg.Payload), endPoint),
                MessageType.Addr => AddrHandle(JsonSerializer.Deserialize <AddrPayload>(msg.Payload), endPoint),
                MessageType.Inventory => Task.CompletedTask,
                MessageType.Notice => Task.CompletedTask,
                MessageType.Ping => Task.CompletedTask,
                MessageType.SurfaceHandShake => SurfaceHandShakeHandle(endPoint),
                _ => Task.CompletedTask
            });
        }
コード例 #2
0
 public async Task ConnectAsync(IPEndPoint endPoint)
 {
     try
     {
         await SendMessageAsync(endPoint.Address, NetworkConstant.SERVER_PORT, HandShake.CreateMessage(ConnectServers));
     }
     catch (SocketException)
     {
         _logger.LogInformation($"Server: {endPoint} > No response");
     }
 }