示例#1
0
        private void Client_OnPacketReceive(object sender, ReceivePacketArgs e)
        {
            var client = (Client)sender;

            if (e.Packet is AuthenticationPacket)
            {
                var account = ((AuthenticationPacket)e.Packet).Account;

                if (AllowedAccounts.Contains(account))
                {
                    IPacket packet = new InformationPacket();
                    packet.Execute(client);
                }
                else
                {
                    RemoveClient(client);
                }
            }
            else if (e.Packet is InformationPacket)
            {
                var info = ((InformationPacket)e.Packet);
                client.NetworkId = info.NetworkId;
                AddClient(client);
                OnClientConnect?.Invoke(this, new ClientStateChangeArgs(client.NetworkId, client, info));
            }
            else
            {
                OnClientPacketReceive?.Invoke(this, e);
            }
        }
示例#2
0
        public void Dispose()
        {
            if (!IsDisposed)
            {
                ServerEndPoint = null;
                InternalSocket.Disconnect(false);
                InternalSocket.Shutdown(SocketShutdown.Both);
                InternalSocket.Dispose();

                foreach (var client in ConnectedClients)
                {
                    RemoveClient(client.Value);
                }

                AllowedAccounts.RemoveRange(0, AllowedAccounts.Count);

                AllowedAccounts  = null;
                ConnectedClients = null;
                IsDisposed       = true;
            }
        }
示例#3
0
 public Server(string ip, int port, Account account)
 {
     ServerEndPoint = new IPEndPoint(IPAddress.Parse(ip), port);
     AllowedAccounts.Add(account);
 }