Example #1
0
        /// <summary>
        /// The entry point of the program
        /// </summary>
        /// <param name="args">The command line arguments.</param>
        /// <returns>The error code.</returns>
        public static int Main(string[] args)
        {
            // Create an instance of the service so it can be registered to handle requests.
            globalRpcService = new GlobalRpcService();

            // Find all registerable game classes
            var gameTypeList = (from domainAssembly in AppDomain.CurrentDomain.GetAssemblies()
                                from assemblyType in domainAssembly.GetTypes()
                                where typeof(GameRpcService).IsAssignableFrom(assemblyType)
                                where !assemblyType.IsAbstract
                                select assemblyType).ToArray();

            // And attempt to register them
            var gameList = new List <GameRpcService>();

            foreach (Type gameType in gameTypeList)
            {
                GameRpcService game = (GameRpcService)Activator.CreateInstance(gameType);
                if (Register(game))
                {
                    gameList.Add(game);
                }
            }

            var rpcResultHandler = new AsyncCallback(
                state =>
            {
                var async  = (JsonRpcStateAsync)state;
                var result = async.Result;
                var writer = (StreamWriter)async.AsyncState;

                writer.Write(result);
                writer.Flush();
            });

            SocketListener.start(
                ListenPort,
                (writer, line) =>
            {
                var async = new JsonRpcStateAsync(rpcResultHandler, writer)
                {
                    JsonRpc = line
                };
                JsonRpcProcessor.Process(async, writer);
            });

            return(0);
        }
Example #2
0
        /// <summary>
        /// Registers a game with the server.
        /// </summary>
        /// <param name="game">The game to register.</param>
        /// <returns>Whether the registration was successful or not.</returns>
        private static bool Register(GameRpcService game)
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://merknera.com/rpc");

            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method      = "POST";
            httpWebRequest.KeepAlive   = false;
            httpWebRequest.ServicePoint.Expect100Continue = false;

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = File.ReadAllText("registration.json");
                json = json.Replace("<GAME_NAME>", game.GameName);
                json = json.Replace("<GAME_DESCRIPTION", game.GameDescription);
                streamWriter.Write(json);
            }

            try
            {
                var          httpResponse      = (HttpWebResponse)httpWebRequest.GetResponse();
                MemoryStream stream1           = new MemoryStream();
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(RegistrationResponse));
                var request = (RegistrationResponse)ser.ReadObject(httpResponse.GetResponseStream());
                if (request == null)
                {
                    Console.WriteLine("No request could be found in the resposne stream.");
                    return(false);
                }
                else if (request.Error != null)
                {
                    Console.WriteLine($"The server responded with an error message: {request.Error}");
                    return(false);
                }
                else
                {
                    Console.WriteLine($"{game.GameName} was registered successfully: {request.Result.Message}");
                    return(true);
                }
            }
            catch (WebException ex)
            {
                Console.WriteLine($"An exception occurred when registereing the game {game.GameName}: {ex}");
                return(false);
            }
        }