Exemplo n.º 1
0
        public void ReadCallback(IAsyncResult ar)
        {
            ClientState  state = (ClientState)ar.AsyncState;
            Client       client;
            SocketError  errorCode = SocketError.SocketError;
            int          bytesRead = 0;
            ClientPacket receivedPacket;

            GameLog.Debug($"SocketConnected: {state.WorkSocket.Connected}, IAsyncResult: Completed: {ar.IsCompleted}, CompletedSynchronously: {ar.CompletedSynchronously}, queue size: {state.Buffer.Length}");
            GameLog.Debug("Running read callback");

            if (!GlobalConnectionManifest.ConnectedClients.TryGetValue(state.Id, out client))
            {
                // Is this a redirect?
                Redirect redirect;
                if (!GlobalConnectionManifest.TryGetRedirect(state.Id, out redirect))
                {
                    GameLog.ErrorFormat("Receive: data from unknown client (id {0}, closing connection", state.Id);
                    state.WorkSocket.Close();
                    state.WorkSocket.Dispose();
                    return;
                }
                client             = redirect.Client;
                client.ClientState = state;
                if (client.EncryptionKey == null)
                {
                    client.EncryptionKey = redirect.EncryptionKey;
                }
                GlobalConnectionManifest.RegisterClient(client);
            }

            try
            {
                bytesRead = state.WorkSocket.EndReceive(ar, out errorCode);
                if (bytesRead == 0 || errorCode != SocketError.Success)
                {
                    GameLog.Error($"bytesRead: {bytesRead}, errorCode: {errorCode}");
                    client.Disconnect();
                }
            }
            catch (Exception e)
            {
                GameLog.Fatal($"EndReceive Error:  {e.Message}");
                client.Disconnect();
            }

            try
            {
                // TODO: improve / refactor
                while (client.ClientState.TryGetPacket(out receivedPacket))
                {
                    client.Enqueue(receivedPacket);
                }
            }
            catch (Exception e)
            {
                GameLog.Error($"ReadCallback error: {e.Message}");
            }
            ContinueReceiving(state, client);
        }
Exemplo n.º 2
0
        public void Remove(VisibleObject obj)
        {
            lock (_lock)
            {
                if (Objects.Remove(obj))
                {
                    EntityTree.Remove(obj);

                    if (obj is User)
                    {
                        var user = obj as User;
                        Users.Remove(user.Name);
                        if (user.ActiveExchange != null)
                        {
                            user.ActiveExchange.CancelExchange(user);
                        }
                    }

                    var affectedObjects = EntityTree.GetObjects(obj.GetViewport());

                    foreach (var target in affectedObjects)
                    {
                        // If the target of a Remove is a player, we insert a 250ms delay to allow the animation
                        // frame to complete, or a slight delay to allow a kill animation to finish animating.
                        // Yes, this is a thing we do.
                        if (target is User && obj is User)
                        {
                            ((User)target).AoiDeparture(obj, 250);
                        }
                        else if (target is User && obj is Creature)
                        {
                            ((User)target).AoiDeparture(obj, 100);
                        }
                        else
                        {
                            target.AoiDeparture(obj);
                        }

                        obj.AoiDeparture(target);
                    }

                    obj.Map = null;
                }
                else
                {
                    GameLog.Fatal("Failed to remove gameobject id: {0} name: {1}", obj.Id, obj.Name);
                }
            }
        }
Exemplo n.º 3
0
        public void Start()
        {
            var x = 0;

            while (true)
            {
                // Ignore processing if no one is logged in, what's the point

                try
                {
                    foreach (var map in _maps)
                    {
                        if (map.Users.Count == 0)
                        {
                            continue;
                        }

                        foreach (var obj in map.Objects.Where(x => x is Monster).ToList())
                        {
                            if (obj is Monster mob)
                            {
                                if (mob.Active)
                                {
                                    Evaluate(mob, map);
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    GameLog.Fatal("Monolith thread error: {e}", e);
                }
                Thread.Sleep(1000);
                x++;
                // Refresh our list every 15 seconds in case of XML reloading
                if (x == 30)
                {
                    _maps = Game.World.WorldData.Values <Map>().ToList();
                    x     = 0;
                }
            }
        }
Exemplo n.º 4
0
 public bool TryGetUser(string name, out User userobj)
 {
     userobj = null;
     try
     {
         userobj = Redis.Get <User>(User.GetStorageKey(name));
         if (userobj != null)
         {
             // Ensure our UUID reference is created when we deserialize a user (if it doesn't already exist)
             GetUuidReference(userobj);
             return(true);
         }
     }
     catch (Exception e)
     {
         GameLog.Fatal("{name}: DESERIALIZATION ERROR, bug or corrupt user data: {e}", name, e);
         return(false);
     }
     return(false);
 }
Exemplo n.º 5
0
 private void ContinueReceiving(ClientState state, Client client)
 {
     // Continue getting dem bytes
     try
     {
         state.WorkSocket.BeginReceive(state.Buffer, state.BytesReceived, state.Buffer.Length - state.BytesReceived, 0,
                                       new AsyncCallback(this.ReadCallback), state);
         GameLog.DebugFormat("Triggering receive callback");
     }
     catch (ObjectDisposedException e)
     {
         GameLog.Fatal(e.Message);
         //client.Disconnect();
         state.WorkSocket.Close();
     }
     catch (SocketException e)
     {
         GameLog.Fatal(e.Message);
         client.Disconnect();
     }
 }