Exemple #1
0
        public async Task ConnectManyAsync(IEndPoint[] endPoints, IEndPoint sourceEndPoint, bool invokeEvents, IAsymmetricKey key, ISymmetricKey dummySymmetricKey, byte[] selfCustomData)
        {
            if (CurrentStatus != ClientStatus.NotConnected)
            {
                return;
            }
            if (selfCustomData.Length > 65000)
            {
                throw new DnmpException("Custom data length is larger than 65000 bytes");
            }
            SelfCustomData = selfCustomData;
            Initialize(sourceEndPoint, key, dummySymmetricKey);
            CurrentStatus = ClientStatus.Connecting;
            if (invokeEvents)
            {
                connectionTimeoutEvent = EventQueue.AddEvent(_ =>
                {
                    NetworkHandler.Stop();
                    MessageHandler.Stop();
                    ClientsById.Clear();
                    CurrentStatus = ClientStatus.NotConnected;
                    OnConnectionTimeout?.Invoke();
                }, null, DateTime.Now.AddMilliseconds(Config.ConnectionTimeout));
            }
            else
            {
                connectionTimeoutEvent = EventQueue.AddEvent(_ =>
                {
                    CurrentStatus = ClientStatus.NotConnected;
                }, null, DateTime.Now.AddMilliseconds(Config.ConnectionTimeout));
            }

            logger.Debug($"Trying to connect to {endPoints.Length} endpoints. First: {endPoints.FirstOrDefault()}");

            foreach (var endPoint in endPoints)
            {
                logger.Debug($"Trying to connect to {endPoint}");
                try
                {
                    NetworkHandler.SendBaseMessage(
                        new BaseMessage(new ConnectionRequestMessage(key.GetNetworkId(), true), 0xFFFF, 0xFFFF),
                        endPoint);
                }
                catch (Exception e)
                {
                    logger.Warn($"Caught exception while trying to connect: {e.GetType().Name}('{e.Message}')");
                }
            }

            if (invokeEvents)
            {
                return;
            }
            SpinWait.SpinUntil(() => CurrentStatus == ClientStatus.Connecting || CurrentStatus == ClientStatus.Handshaking);
            if (CurrentStatus == ClientStatus.NotConnected)
            {
                throw new TimeoutException("Connection timeout");
            }
            await Task.Delay(0);
        }
Exemple #2
0
        public Client?FindById(Guid clientId)
        {
            if (!ClientsById.ContainsKey(clientId))
            {
                return(null);
            }

            return(ClientsById[clientId]);
        }
 private void SendText(TPlayer player, string msg, bool doHighlight)
 {
     if (NameToIdMap.TryGetValue(player.NormalizedName, out string id))
     {
         if (ClientsById.TryGetValue(id, out (IClientProxy, TPlayer)client))
         {
             client.Item1.SendAsync("msg", HtmlTabled(msg.Boxed(), doHighlight));
         }
     }
 }
Exemple #4
0
        public void Unregister(Client client)
        {
            ClientsById.Remove(client.Id);

            if (!ClientsByWanIp.TryGetValue(client.WanIp, out List <Client> clientsForWanIp))
            {
                return;
            }
            if (clientsForWanIp.Contains(client))
            {
                clientsForWanIp.Remove(client);
            }
        }
Exemple #5
0
 public void Stop()
 {
     if (CurrentStatus == ClientStatus.NotConnected)
     {
         return;
     }
     CurrentStatus = ClientStatus.Disconnecting;
     EventQueue.RemoveEvent(connectionTimeoutEvent);
     NetworkHandler.Stop();
     MessageHandler.Stop();
     ClientsById.Clear();
     CurrentStatus = ClientStatus.NotConnected;
     OnDisconnected?.Invoke();
 }
Exemple #6
0
 public void Unregister(Client client)
 {
     ClientsById.Remove(client.Id);
 }
Exemple #7
0
 internal void RemoveClient(DnmpNode client)
 {
     logger.Debug($"Removed client #{client.Id}");
     ClientsById.TryRemove(client.Id, out _);
     OnClientDisconnected?.Invoke(client.Id);
 }
Exemple #8
0
 internal void AddClient(DnmpNode client)
 {
     logger.Debug($"Added client #{client.Id}");
     ClientsById.TryAdd(client.Id, client);
     OnClientConnected?.Invoke(client.Id);
 }