private bool HandleMessage(Server.ClientContext context, string message) { string errorMessage = null; Message msg = null; try { msg = new Message(message); } catch (Exception ex) { errorMessage = string.Format("Couldn't construct message: {0}", ex.Message); logger.Warn(errorMessage); Send(context, Commands.ReportError((int)Commands.ErrorCode.INVALID_CMD, errorMessage)); return(false); } BoundCommand command = Commands.ServerParser.ParseMessage(msg, out errorMessage); if (command == null) { logger.Warn("Did not accept the message: {0}", message); Send(context, Commands.ReportError((int)Commands.ErrorCode.INVALID_CMD, errorMessage)); return(false); } // Requires authentication? if (command.RequiresAuth && !IsRegistered(context)) { // errorMessage = "Not authorized"; return(false); } command.InvokeHandler(this, new ServerCommandEventArgs { Context = context, Command = command }); return(true); }
private bool IsRegistered(Server.ClientContext context) { string clientName = (string)context.Tag; ClientState state = null; bool found; lock (ClientsByNameLock) found = ClientsByName.TryGetValue(clientName, out state); if (!found) { logger.Error("The client named {0} is not connected?", clientName); return(false); } if (!state.IsRegistered) { Send(context, Commands.ReportError((int)Commands.ErrorCode.NOT_AUTHORIZED, "Not authorized")); return(false); } return(true); }
public void Send(Server.ClientContext context, string cmd) { byte[] data = StringToBytes(cmd); Server.Send(context, data); }