Task <string> ICommunicationListener.OpenAsync(CancellationToken cancellationToken)
            {
                var endpoint = FabricRuntime.GetActivationContext().GetEndpoint(this._endpointName);

                var address    = FabricRuntime.GetNodeContext().IPAddressOrFQDN;
                var serverUrls =
                    ImmutableArray.Create(
                        $"{endpoint.Protocol}://{address}:{endpoint.Port}");

                if (!LocalAddresses.Contains(address))
                {
                    serverUrls = serverUrls.Add($"{endpoint.Protocol}://localhost:{endpoint.Port}");
                }

                this.webHost =
                    new WebHostBuilder().UseKestrel()
                    .UseContentRoot(Directory.GetCurrentDirectory())
                    .UseStartup <Startup>()
                    .UseUrls(serverUrls.ToArray())
                    .UseServiceFabricContext(this.Context)
                    .Build();
                this.webHost.Start();

                return(Task.FromResult(serverUrls[0]));
            }
Exemple #2
0
        protected virtual void ClientPacketReceived(object sender, PacketEventArgs e)
        {
            if (!(sender is ISocket socket))
            {
                return;
            }

            Stats.PacketsReceived++;
            Stats.AddInput(e.Transferred);

            //only allow connections from WebSockets?
            if (!Config.UseTcpSockets && !socket.IsWebSocket)
            {
                socket.Disconnect();
                return;
            }

            if ((AresId)e.Packet.Id == AresId.MSG_CHAT_CLIENT_LOGIN)
            {
                IClient user = Users.Find(s => s.Socket == socket);

                if (user == null)
                {
                    if (HandlePending(socket))
                    {
                        int id     = idpool.Pop();
                        var client = new AresClient(this, socket, (ushort)id);

                        if (IPAddress.IsLoopback(client.ExternalIp) ||
                            client.ExternalIp.Equals(ExternalIp) ||
                            LocalAddresses.Contains(client.ExternalIp) ||
                            (Config.LocalAreaIsHost && client.ExternalIp.IsLocalAreaNetwork()))
                        {
                            client.LocalHost = true;
                        }

                        lock (Users) {
                            Users.Add(client);
                            Users.Sort(sorter);
                            Stats.PeakUsers = Math.Max(Users.Count, Stats.PeakUsers);
                        }

                        client.HandleJoin(e);
                    }
                    else
                    {
                        socket.Disconnect();
                    }
                }
                else
                {
                    //sending too many login packets
                    SendAnnounce(user, Errors.LoginFlood);
                    Logging.Info("AresServer", "User '{0}' has been disconnected for login flooding.", user.Name);

                    user.Disconnect();
                }
            }
        }
Exemple #3
0
    private void ReceiveClient(IAsyncResult ar)
    {
        if (socketClient != null)
        {
            try
            {
                int    size    = socketClient.EndReceiveFrom(ar, ref remoteEndPoint);
                string address = remoteEndPoint.ToString().Split(':')[0];

                // This is not ourself and we do not already have this address
                if (!LocalAddresses.Contains(address) && !Addresses.Contains(address))
                {
                    Addresses.Add(address);
                }

                socketClient.BeginReceiveFrom(new byte[1024], 0, 1024, SocketFlags.None,
                                              ref remoteEndPoint, new AsyncCallback(ReceiveClient), null);
            }
            catch (Exception ex)
            {
                Debug.Log(ex.ToString());
            }
        }
    }
Exemple #4
0
        protected virtual void ClientPacketReceived(object sender, PacketEventArgs e)
        {
            if (!(sender is ISocket socket))
            {
                return;
            }

            Stats.PacketsReceived++;
            Stats.AddInput(e.Transferred);

            if (e.Packet.Id == (byte)AresId.MSG_CHAT_CLIENT_LOGIN)
            {
                IClient user = Users.Find(s => s.Socket == socket);

                if (user == null)
                {
                    if (HandlePending(socket))
                    {
                        if (idpool.Count == 0)
                        {
                            socket.SendAsync(new Announce(Errors.RoomFull));
                            socket.Disconnect();
                            Logging.Info(
                                "AresServer",
                                "Chatroom has reached capacity ({0}). If this happens frequently consider raising Max Clients",
                                Config.MaxClients
                                );
                        }
                        else
                        {
                            int id     = idpool.Pop();
                            var client = new AresClient(this, socket, (ushort)id);

                            if (IPAddress.IsLoopback(client.ExternalIp) ||
                                client.ExternalIp.Equals(ExternalIp) ||
                                LocalAddresses.Contains(client.ExternalIp) ||
                                (Config.LocalAreaIsHost && client.ExternalIp.IsLocalAreaNetwork()))
                            {
                                client.LocalHost = true;
                            }

                            lock (Users) {
                                Users.Add(client);
                                Users.Sort(sorter);
                                Stats.PeakUsers = Math.Max(Users.Count, Stats.PeakUsers);
                            }

                            client.HandleJoin(e);
                        }
                    }
                }
                else
                {
                    //sending too many login packets
                    SendAnnounce(user, Errors.LoginFlood);
                    Logging.Info("AresServer", "User '{0}' has been disconnected for login flooding.", user.Name);

                    user.Disconnect();
                }
            }
        }