Example #1
0
        private void OnRegister(object sender, EventArgs e)
        {
            ServerCommandEventArgs args = (ServerCommandEventArgs)e;

            string oldClientName = (string)args.Context.Tag;
            string newClientName = args.Command.GetArgumentValue <string>("clientName");

            if (string.IsNullOrEmpty(newClientName) || newClientName.Length > Commands.REGISTER_CLIENT_NAME_MAXLEN)
            {
                // TODO(jweyrich): Handle invalid registration msg.
                return;
            }

            lock (ClientsByNameLock)
            {
                if (ClientsByName.ContainsKey(newClientName))
                {
                    Send(args.Context, Commands.ReportError((int)Commands.ErrorCode.NAME_ALREADY_IN_USE, "This name is already in use"));
                    return;
                }

                ClientState state = ClientsByName[oldClientName];
                state.IsRegistered = true;
                args.Context.Tag   = newClientName;

                // Remove & re-add state using the new client name.
                ClientsByName.Remove(oldClientName);
                ClientsByName.Add(newClientName, state);

                // Remove this client from the list of unknown target hits if it's there.
                UnknownTargetHits_Remove(newClientName);
            }
        }
Example #2
0
        private void OnError(object sender, EventArgs e)
        {
            ServerCommandEventArgs args = (ServerCommandEventArgs)e;
            string message = args.Command.GetArgumentValue <string>("message");

            logger.Warn("ERROR FROM CLIENT: {0}", message);
        }
Example #3
0
        private void OnBroadcast(object sender, EventArgs e)
        {
            ServerCommandEventArgs args = (ServerCommandEventArgs)e;

            string message = args.Command.GetArgumentValue <string>("message");

            lock (ClientsByNameLock)
            {
                foreach (var state in ClientsByName.Values)
                {
                    // Do not send it to the sender.
                    if (state.Context.ClientKey.Equals(args.Context.ClientKey))
                    {
                        continue;
                    }

                    Send(state.Context, message);
                }
            }
        }
Example #4
0
        private void OnRoute(object sender, EventArgs e)
        {
            ServerCommandEventArgs args = (ServerCommandEventArgs)e;

            string targetName = args.Command.GetArgumentValue <string>("targetName");
            string message    = args.Command.GetArgumentValue <string>("message");

            if (string.IsNullOrEmpty(targetName) || targetName.Length > Commands.REGISTER_CLIENT_NAME_MAXLEN)
            {
                // Don't spam the sender with error messages if there's no GUI app running.
                if (!targetName.Equals(Commands.IPC_DEFAULT_GUI_CLIENT_NAME))
                {
                    Send(args.Context, Commands.ReportError((int)Commands.ErrorCode.INVALID_ROUTE_MSG, "Invalid route message"));
                }
                return;
            }

            ClientState targetState = null;

            bool found;

            lock (ClientsByNameLock)
                found = ClientsByName.TryGetValue(targetName, out targetState);
            if (!found)
            {
                bool canProceed = UnknownTargetHits_Hit(targetName);
                if (canProceed)
                {
                    Send(args.Context, Commands.ReportError((int)Commands.ErrorCode.UNKNOWN_TARGET, "Unknown target {0}", targetName));
                }
                return;
            }

            //Console.WriteLine("SENDING TO {0} -> {1}", targetName, message);
            Send(targetState.Context, message);
        }