Ejemplo n.º 1
0
        public static void Route(PlayerWebsocket socket, string socketMessage)
        {
            SocketMessage message;

            try {
                message = new SocketMessage(socketMessage);
            }
            catch (Exception e) {
                string msg = string.Format("Can not parse message {1}: \n {0}", e.Message, socketMessage);
                Logger.log(12, Category.WebSocket, Severity.Error, msg);
                return;
            }
            try {
                if (message.controllerType != null)
                {
                    RouteToController(socket, message);
                }
                else
                {
                    RouteToEvent(socket, message);
                }
            }
            catch (Exception e) {
                string msg = string.Format("Can not route message {1}: \n {0}", e.ToString(), socketMessage);
                Logger.log(46, Category.WebSocket, Severity.Error, msg);
            }
        }
Ejemplo n.º 2
0
        public SocketServer()
        {
            FleckLog.LogAction = (level, message, ex) => {
                if (ex != null)
                {
                    message += ex.ToString();
                }
                switch (level)
                {
                case LogLevel.Debug:
                    //Logger.log(13, Category.SocketServer, Severity.Debug, message);
                    break;

                case LogLevel.Error:
                    Logger.log(14, Category.SocketServer, Severity.Error, message);
                    break;

                case LogLevel.Warn:
                    Logger.log(15, Category.SocketServer, Severity.Warn, message);
                    break;

                default:
                    Logger.log(16, Category.SocketServer, Severity.Info, message);
                    break;
                }
            };

            Fleck.WebSocketServer server = new Fleck.WebSocketServer(Settings.socketServerUrl);
            server.Start(socket => {
                var playerWebSocket = new PlayerWebsocket(socket);
                socket.OnOpen       = () => {
                    if (playerWebSocket.isAuthenticated)
                    {
                        PlayerWebsocketConnections.AddSocket(playerWebSocket);
                    }
                    else
                    {
                        playerWebSocket.Close();
                    }
                };
                socket.OnClose = () => {
                    if (playerWebSocket.isAuthenticated)
                    {
                        PlayerWebsocketConnections.RemoveSocket(playerWebSocket);
                    }
                };
                socket.OnMessage = message => {
                    if (playerWebSocket.isAuthenticated)
                    {
                        Router.Route(playerWebSocket, message);
                    }
                };
            });


            this.server = server;
        }
Ejemplo n.º 3
0
        private static bool RouteToController(PlayerWebsocket socket, SocketMessage message)
        {
            MethodInfo methodInfo = message.controllerType.GetMethod("OnMessage");
            var        args       = new object[2];

            args[0] = socket;
            args[1] = message.jsonPayload;
            methodInfo.Invoke(null, args);
            return(true);
        }
Ejemplo n.º 4
0
        public static void RemoveSocket(PlayerWebsocket socket)
        {
            lock (socketsLock) {
                if (sockets.ContainsKey(socket.playerID))
                {
                    sockets[socket.playerID].Close();
                    sockets.Remove(socket.playerID);
                }
            }
            EventEngine.AddEvent(new PlayerOnline(socket.playerID, false));

            Player player = Browsergame.Game.Engine.StateEngine.GetState().GetPlayer(socket.playerID);
            string msg    = string.Format("Socket closed. Player {0}. Token: {1}. Thread: {2}", player.Name, player.token.Substring(0, 5), Thread.CurrentThread.ManagedThreadId);

            Logger.log(11, Category.WebSocket, Severity.Debug, msg);
        }
Ejemplo n.º 5
0
        private static bool RouteToEvent(PlayerWebsocket socket, SocketMessage message)
        {
            if (!message.eventType.GetCustomAttributes(typeof(RoutableEvent)).Any())
            {
                throw new Exception(string.Format("Event '{0}' is not marked as routable", message.action));
            }
            var constructor       = message.eventType.GetConstructors()[0];
            var constructorParams = new object[constructor.GetParameters().Count()];

            foreach (var param in constructor.GetParameters())
            {
                try {
                    Type   type       = param.ParameterType;
                    object paramValue = null;
                    if (param.Name == "playerID")
                    {
                        paramValue = socket.playerID;
                    }
                    else
                    {
                        var jsonParam = message.jsonPayload[param.Name];
                        if (jsonParam == null)
                        {
                            throw new ArgumentException(string.Format("{0}. parameter named '{1}' not found in payload", param.Position, param.Name));
                        }
                        if (type.GetTypeInfo().IsEnum)
                        {
                            if (jsonParam.Value.GetType().Name == "String")
                            {
                                paramValue = Enum.Parse(type, (string)jsonParam.Value);
                            }
                            else
                            {
                                paramValue = Enum.ToObject(type, jsonParam.Value);
                            }
                        }
                        else
                        {
                            if (type == typeof(string))
                            {
                                paramValue = jsonParam.ToString();
                            }
                            if (type == typeof(bool))
                            {
                                paramValue = JsonConvert.DeserializeObject(jsonParam.ToString().ToLower(), type);
                            }
                            else
                            {
                                paramValue = JsonConvert.DeserializeObject(jsonParam.ToString(), type);
                            }
                        }
                    }
                    constructorParams[param.Position] = paramValue;
                }
                catch (Exception e) {
                    throw new Exception(string.Format("Failed to parse param '{0}'.", param.Name), e);
                }
            }

            var eventObject = (Event)Activator.CreateInstance(message.eventType, constructorParams); //constructorParams

            EventEngine.AddEvent(eventObject);

            return(true);
        }