private void Run() { ConcurrentQueue<Message> brokerInbox = new ConcurrentQueue<Message>(); ConcurrentQueue<Message> photonInbox = new ConcurrentQueue<Message>(); ConcurrentQueue<Message> gameInbox = new ConcurrentQueue<Message>(); ConcurrentQueue<Message> uiInbox = new ConcurrentQueue<Message>(); Broker broker = new Broker(brokerInbox, photonInbox, gameInbox, uiInbox, outbox: mainInbox); PhotonClientCachedData cache = new PhotonClientCachedData(); PhotonWebRpcHandler webrpc = new PhotonWebRpcHandler(cache); PhotonOperationResponseHandler opresponse = new PhotonOperationResponseHandler(cache); PhotonTurnbasedClient photonClient = new PhotonTurnbasedClient(Clan, PlayerName, cache, webrpc, opresponse, new QueuedConsoleLogger(brokerInbox)); PhotonActor photonActor = new PhotonActor(photonInbox, brokerInbox, photonClient); //create delegate instances and bind them //to the observer's OnGameList, OnGames, .. method PhotonWebRpcHandler.OnGameListDelegate gameListDelegate = new PhotonWebRpcHandler.OnGameListDelegate(photonActor.OnGameList); webrpc.OnGameList += gameListDelegate; PhotonWebRpcHandler.OnGamesDelegate gamesDelegate = new PhotonWebRpcHandler.OnGamesDelegate(photonActor.OnGames); webrpc.OnGames += gamesDelegate; PhotonWebRpcHandler.OnPlayerSaveDelegate playerSaveDelegate = new PhotonWebRpcHandler.OnPlayerSaveDelegate(photonActor.OnPlayerSave); webrpc.OnPlayerSave += playerSaveDelegate; PhotonWebRpcHandler.OnPlayerLoadDelegate playerLoadDelegate = new PhotonWebRpcHandler.OnPlayerLoadDelegate(photonActor.OnPlayerLoad); webrpc.OnPlayerLoad += playerLoadDelegate; PhotonOperationResponseHandler.OnSetPropertiesDelegate setPropertiesDelegate = new PhotonOperationResponseHandler.OnSetPropertiesDelegate(photonActor.OnSetProperties); webrpc.OnPlayerLoad += playerLoadDelegate; CommandUI commandUI = new CommandUI(uiInbox, brokerInbox, photonClient); Game game = new Game(gameInbox, brokerInbox); Thread brokerThread = new Thread(new ThreadStart(broker.UpdateLoop)); brokerThread.IsBackground = true; brokerThread.Start(); Thread gameThread = new Thread(new ThreadStart(game.UpdateLoop)); gameThread.IsBackground = true; gameThread.Start(); Thread uiThread = new Thread(new ThreadStart(commandUI.UpdateLoop)); uiThread.IsBackground = true; uiThread.Start(); Thread photonThread = new Thread(new ThreadStart(photonActor.UpdateLoop)); photonThread.IsBackground = true; photonThread.Start(); Actor joinActor = new Actor(mainInbox, null); joinActor.UpdateLoop(); }
private void UpdateLoop(object obj) { string roomname = MyClan(); var command = string.Empty; var data = string.Empty; var keybuffer = string.Empty; const int FOR_50ms = 50; var command = new CommandUI(); while (!done) { while (this.loadBalancingPeer.DispatchIncomingCommands()) { // You could count dispatch calls to limit them to X, if they take too much time of a single frame } while (Console.KeyAvailable) { var key = Console.ReadKey(); if (Char.IsLetterOrDigit(key.KeyChar) || key.KeyChar == '\\' || key.Key == ConsoleKey.Enter) { if (key.Key == ConsoleKey.Enter) { if (keybuffer.StartsWith(@"\")) { command = keybuffer.Substring(0, 2); data = keybuffer.Substring(2, keybuffer.Length - 2); } else { command = keybuffer; } keybuffer = string.Empty; Console.WriteLine(); break; } keybuffer += key.KeyChar; } } switch (command) { case "": break; case @"\a": int.TryParse(data, out this.actorNr); Console.WriteLine("set to room:{0} actor#:{1}", roomname, this.actorNr); break; case @"\r": if (!string.IsNullOrEmpty(data)) { roomname = data; } Console.WriteLine("set to room:{0} actor#:{1}", roomname, this.actorNr); break; case @"\c": if (this.State == ClientState.JoinedLobby) { this.CreateRoomForMatchmaking(roomname); } else { Console.WriteLine("CreateRoom only allowed when in Lobby"); } break; case @"\j": if (this.State == ClientState.JoinedLobby) { if (!string.IsNullOrEmpty(data)) { int.TryParse(data, out this.actorNr); } Console.WriteLine("joining room:{0} with actor#:{1}", roomname, this.actorNr); this.OpJoinRoom(roomname, this.actorNr == 0 ? false : true, this.actorNr); } else { Console.WriteLine("JoinRoom only allowed when in Lobby"); } break; case @"\d": this.loadBalancingPeer.Disconnect(); break; case @"\l": if (this.State == ClientState.Joined) { this.loadBalancingPeer.OpCustom(OperationCode.Leave, null, true); } break; case @"\g": if (this.State == ClientState.JoinedLobby || this.State == ClientState.Joined) { //this.OpWebRpc("GetGameList?getpar1=1&getpar2=xyz", new Dictionary<string, object>() { { "p1", "one" }, { "p2", "two" } }); //this.OpWebRpc("GetGameList?getpar1=1&getpar2=xyz", new int[] { 1, 2, 3 }); this.OpWebRpc("GetGameList?getpar1=1&getpar2=xyz", null); } break; case @"\q": this.loadBalancingPeer.Disconnect(); Thread.Sleep(1000); done = true; break; case @"\e": if (this.State == ClientState.Joined && !string.IsNullOrEmpty(data)) { var op = new OperationRequest() { OperationCode = OperationCode.RaiseEvent, Parameters = new Dictionary<byte, object>() { { ParameterCode.Code, (byte)0 }, { ParameterCode.Data, data }, { ParameterCode.ActorList, this.CurrentRoom.Players.Keys.ToArray() }, { ParameterCode.Cache, EventCaching.DoNotCache }, { (byte)ParameterCode.HttpForward, true }, } }; this.loadBalancingPeer.OpCustom(op, true, 0, false); } break; case @"\s": int index = -1; int.TryParse(data, out index); if (index >= 0 && index < roomList.LongCount()) { roomname = roomList[index]; this.actorNr = actorList[index]; } Console.WriteLine("set to room:{0} actor#:{1}", roomname, this.actorNr); break; default: break; } command = string.Empty; data = string.Empty; this.loadBalancingPeer.SendOutgoingCommands(); Thread.Sleep(FOR_50ms); } }