protected void Dispatch(NetworkMessage message, object token)
        {
            if (message == null) throw new ArgumentNullException("message");

            try
            {
                if (message.Destinations.HasFlag(ListenerEntry.Local))
                {
                    InternalDispatch(message, token);
                }

                if (Client != null && message.Destinations.HasFlag(ListenerEntry.Client))
                {
                    Client.Send(message);
                }

                if (Server != null && message.Destinations.HasFlag(ListenerEntry.Server))
                {
                    Server.Send(message);
                }

                message.OnDispatched();
                OnMessageDispatched(message);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Cannot dispatch {0}", message), ex);
            }
        }
        private void OnServerMessageReceived(ServerConnection server, NetworkMessage message)
        {
            message.From = ListenerEntry.Server;
            message.Destinations = ListenerEntry.Client | ListenerEntry.Local;

            base.OnMessageReceived(message);
        }
 public override void Send(NetworkMessage message)
 {
     if (message.Destinations.HasFlag(ListenerEntry.Server))
         SendToServer(message);
     if (message.Destinations.HasFlag(ListenerEntry.Client))
         base.Send(message);
     else if (message.Destinations == ListenerEntry.Undefined)
         base.Send(message);
 }
        /// <summary>
        /// Send a message
        /// </summary>
        /// <param name="message">Network Message</param>
        public void SendMessage(NetworkMessage message)
        {
            if (message == null) throw new ArgumentNullException("message");
            m_bot.SendToServer(message);

            if (message is ChatClientPrivateMessage)
                logger.Debug("Private Message Sent to Server");
            else
                logger.Debug("Message Sent to Server");
        }
        private void InternalDispatch(NetworkMessage message, object token)
        {
            if (message == null) throw new ArgumentNullException("message");
            var handlers = GetHandlers(message.GetType(), token);

            foreach (var handler in handlers)
            {
                if (handler.Attribute.DestinationFilter != ListenerEntry.Undefined &&
                    handler.Attribute.DestinationFilter != message.Destinations &&
                    (handler.Attribute.DestinationFilter & message.Destinations) == ListenerEntry.Undefined)
                    continue;

                if (handler.Attribute.FromFilter != ListenerEntry.Undefined &&
                    handler.Attribute.FromFilter != message.From &&
                    (handler.Attribute.FromFilter & message.From) == ListenerEntry.Undefined)
                    continue;

                handler.Action(handler.Container, token, message);

                if (message.Canceled)
                    break;
            }
        }
Beispiel #6
0
        public void Send(NetworkMessage message, ListenerEntry dest)
        {
            if (message == null) throw new ArgumentNullException("message");
            message.Destinations = dest;
            message.From = ListenerEntry.Local;

            Dispatcher.Enqueue(message, this);
        }
Beispiel #7
0
        private void OnWorldClientMessageReceived(Client client, NetworkMessage message)
        {
            if (!( client is ConnectionMITM ))
                throw new ArgumentException("client is not of type ConnectionMITM");

            var mitm = client as ConnectionMITM;

            if (message is AuthenticationTicketMessage && mitm.Bot == null)
            {
                // special handling to connect and retrieve the bot instance
                HandleAuthenticationTicketMessage(mitm, message as AuthenticationTicketMessage);
            }
            else
            {
                if (mitm.Bot == null)
                    throw new NullReferenceException("mitm.Bot");

                if (mitm.Bot.Dispatcher.Stopped)
                    logger.Warn("Enqueue a message but the dispatcher is stopped !");

                mitm.Bot.Dispatcher.Enqueue(message, mitm.Bot);
            }

            logger.Debug("{0} FROM {1}", message, message.From);
        }
Beispiel #8
0
        private void OnAuthClientMessageReceived(Client client, NetworkMessage message)
        {
            if (!( client is ConnectionMITM ))
                throw new ArgumentException("client is not of type ConnectionMITM");

            var mitm = client as ConnectionMITM;

            if (mitm.Bot == null)
                throw new NullReferenceException("mitm.Bot");

            mitm.Bot.Dispatcher.Enqueue(message, mitm.Bot);

            logger.Debug("{0} FROM {1}", message, message.From);
        }
        public void SendToServer(NetworkMessage message)
        {
            if (!IsBoundToServer)
                throw new InvalidOperationException("Client is not bound to server");

            Server.Send(message);
        }
Beispiel #10
0
        public void Send(NetworkMessage message, ListenerEntry dest)
        {
            if (message == null) throw new ArgumentNullException("message");
            message.Destinations = dest;
            message.From = ListenerEntry.Local;

            Dispatcher.Enqueue(message, this);

            if (Disposed)
                logger.Error("Error the message {0} wont be dispatched because the bot {1} is disposed !", message, this);
            else if (!Running)
                logger.Warn("Warning, enqueue {0} but the bot is stopped, the message will be processed once the bot {1} restart", message, this);
        }
 private void OnMessageSended(NetworkMessage message)
 {
     var handler = MessageSent;
     if (handler != null)
         handler(this, message);
 }
 private void OnMessageReceived(NetworkMessage message)
 {
     var handler = MessageReceived;
     if (handler != null)
         handler(this, message);
 }
        public void Send(NetworkMessage message)
        {
            lock (this)
            {
                if (!IsConnected)
                    return;

                var args = new SocketAsyncEventArgs();
                args.Completed += OnSendCompleted;
                args.UserToken = message;

                byte[] data;
                using (var writer = new BigEndianWriter())
                {
                    message.Pack(writer);
                    data = writer.Data;
                }

                args.SetBuffer(data, 0, data.Length);

                if (!Socket.SendAsync(args))
                {
                    OnMessageSended(message);
                    args.Dispose();
                }

                LastActivity = DateTime.Now;
            }
        }
Beispiel #14
0
 protected virtual void OnMessageSended(NetworkMessage message)
 {
     var handler = MessageSent;
     if (handler != null)
         handler(this, message);
 }
Beispiel #15
0
 public void SendLocal(NetworkMessage message)
 {
     Send(message, ListenerEntry.Local);
 }
Beispiel #16
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="message"></param>
 /// <param name="direct">If true it doesn't reprocess the message internally</param>
 public void SendToServer(NetworkMessage message, bool direct = false)
 {
     if (message == null) throw new ArgumentNullException("message");
     if (direct)
         Send(message, ListenerEntry.Server);
     else
         Send(message, ListenerEntry.Server | ListenerEntry.Local);
 }
        protected override void OnMessageReceived(NetworkMessage message)
        {
            message.From = ListenerEntry.Client;
            message.Destinations = ListenerEntry.Server | ListenerEntry.Local;

            base.OnMessageReceived(message);
        }