public IAsyncResult BeginProcessRequest(HttpContext ctx, AsyncCallback cb, Object obj) { currentAsyncRequestState = new CometAsyncResult(ctx, cb, obj); _threadPool.PostRequest(new WorkRequestDelegate(ProcessServiceRequest), currentAsyncRequestState); return currentAsyncRequestState; }
/// <summary> /// original HttpHandler code - AMP pattern /// </summary> /// <param name="context"></param> /// <param name="callback"></param> /// <param name="asyncState"></param> /// <returns></returns> public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback callback, object asyncState) { Message[] request = MessageConverter.FromJson(context.Request); CometAsyncResult asyncResult = new CometAsyncResult(context, callback, asyncState); msgBus.HandleMessages(request, asyncResult); return(asyncResult); }
public void WriteResponseToClient(CometAsyncResult cometAsyncResult) { if (cometAsyncResult.Result) { cometAsyncResult.Context.Response.Write(cometAsyncResult.Message); } else { cometAsyncResult.Context.Response.Write("TOOLONG-DOITAGAIN"); // timeout - client must make request again } }
public void EndProcessRequest(IAsyncResult result) { CometAsyncResult cometAsyncResult = (CometAsyncResult)result; if (cometAsyncResult.CompletedSynchronously) { return; } WriteResponseToClient(cometAsyncResult); }
private static bool CheckTimeOut(CometAsyncResult asyncResult) { if (asyncResult.StartTime.Add(asyncResult.WaitTime) < DateTime.Now) { asyncResult.Result = false; // timeout asyncResult.Callback(asyncResult); return(true); // true for remove from list } return(false); // not remove (because not timed out) }
public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback callback, object extraData) { context.Response.ContentType = "text/plain"; //context.Session CometAsyncResult result = new CometAsyncResult(context, callback, waitTime); lock (allWaitingClientsLockObject) { allWaitingClients.Add(result); if (allWaitingClients.Count == 1) { StartClientTimeouter(); } } return(result); }
// Extension method for LoginConnectAddClient method public static void AddClient(CometAsyncResult state) { Guid newGuid; lock (_lockObjStatic) { while (true) { newGuid = Guid.NewGuid(); if (_clientStateListStatic.Find(s => s.ClientGuid == newGuid) == null) { state.ClientGuid = newGuid; break; } } _clientStateListStatic.Add(state); } }
public void startGame(CometAsyncResult state, int categoryId) { // int catId = Convert.ToInt32(categoryId); StaticMembers._questionList = new List<Question>(); StaticMembers._questionList = _questionService.GetQuestionsByCategoryId(categoryId); StaticMembers._questionListWas = StaticMembers._questionList.Count(); StaticMembers._game = new Game(); StaticMembers._game.played_category_id = categoryId; StaticMembers._game.played_date = DateTime.Now; StaticMembers._game.score = 0; StaticMembers._game.number_right_questions = 0; StaticMembers._game.player_id = Convert.ToInt32(state.HttpContext.Request.Cookies["userId"].Value); if (_gameService.Insert(StaticMembers._game)) { StaticMembers._questionNumber = 0; continueGame(true, 0); } }
// must be static only because its has extension static method "RemoveClient" public void LogoutDisconnect(CometAsyncResult state) { RemoveClient(state); // extension static method Disconnecting }
// Games allways the same content ..must be loaded one time only public IList<Category> getGames(CometAsyncResult state) { CategoryService _categoryService = new CategoryService(); return _categoryService.GetAll(); }
// Extension method for getUserInfo,continueGame methods public static void UpdateClient(CometAsyncResult state, String clientGuidKey) { Guid clientGuid = new Guid(clientGuidKey); lock (_lockObjStatic) { // checks if this state founded CometAsyncResult foundState = _clientStateListStatic.Find(s => s.ClientGuid == clientGuid); //Update this client state if this state found ( prepare for to add to all Clients State List ) if (foundState != null) { foundState.HttpContext = state.HttpContext; foundState.ExtraData = state.ExtraData; foundState.AsyncCallBack = state.AsyncCallBack; } } }
// Extension method for LogoutDisconnectRemoveClient method public static void RemoveClient(CometAsyncResult state) { lock (_lockObjStatic) { _clientStateListStatic.Remove(state); } }
private void Register(CometAsyncResult clientState, string email, string user, string pass, string passConfirm) { _playerService = new PlayerService(); _jsonHelper = new JsonHelper<Object>(); string responseJSON = null; if (pass == passConfirm) { Player newPlayer = new Player(); newPlayer.email = email; newPlayer.username = user; // kod zashivrovali v settere newPlayer.password = pass; newPlayer.image = "ehse netu picture"; newPlayer.registration_date = DateTime.Now; if (!_playerService.CheckIfExists(newPlayer)) { // do stuff here to log the user in ... if (_playerService.Insert(newPlayer)) { Login(clientState, newPlayer.email, newPlayer.password); } } else { _jsonHelper.status = 405; _jsonHelper.message = "This email is registered,Please login or register another one"; responseJSON = Newtonsoft.Json.JsonConvert.SerializeObject(_jsonHelper); clientState.HttpContext.Response.Headers.Add("Content-type", "application/json"); clientState.HttpContext.Response.Write(responseJSON); } } else { _jsonHelper.status = 406; _jsonHelper.message = "Passwords doesn't match!"; responseJSON = Newtonsoft.Json.JsonConvert.SerializeObject(_jsonHelper); HttpContext.Current.Response.Headers.Add("Content-type", "application/json"); HttpContext.Current.Response.Write(responseJSON); } }
private void Login(CometAsyncResult clientState, string email, string pass) { _jsonHelper = new JsonHelper<object>(); _cometProcessor = new CometClientProcessor(); _playerService = new PlayerService(); Player player = new Player(); player.password = pass; player.email = email; _playerService = new PlayerService(); if (_playerService.Verify(player)) { player = _playerService.FindByEmail(email); // Create Cookie HttpCookie time = new HttpCookie("LastLogined", DateTime.Now.ToString()); HttpCookie userEmail = new HttpCookie("userEMail", player.email); HttpCookie userId = new HttpCookie("userId", player.id.ToString()); time.Expires = DateTime.Now.AddDays(1); userEmail.Expires = DateTime.Now.AddDays(1); clientState.HttpContext.Response.Cookies.Add(time); clientState.HttpContext.Response.Cookies.Add(userEmail); clientState.HttpContext.Response.Cookies.Add(userId); currentAsyncRequestState.HttpContext.Session["Email"] = player.email; currentAsyncRequestState.HttpContext.Session["Username"] = player.username; _jsonHelper.message = "Welcome " + player.username + "!"; _jsonHelper.status = 200; string responseJSON = Newtonsoft.Json.JsonConvert.SerializeObject(_jsonHelper); clientState.HttpContext.Response.Write(responseJSON); } else { _jsonHelper.message = "This email and password not found,Please register or try again"; _jsonHelper.status = 404; string responseJSON = Newtonsoft.Json.JsonConvert.SerializeObject(_jsonHelper); clientState.HttpContext.Response.Write(responseJSON); } clientState.CompleteRequest(); }