Esempio n. 1
0
                private void ProcessRequest(string line, object p = null)
                {
                    // Determine which service method to invoke from

                    // Handle "create user" requests
                    if (createUserPattern.IsMatch(firstLine))
                    {
                        UserInfo user = JsonConvert.DeserializeObject <UserInfo>(line);
                        user = new BoggleService().CreateUser(user, out HttpStatusCode status);
                        String result = "HTTP/1.1 " + (int)status + " " + status + "\r\n";
                        if ((int)status / 100 == 2) // status is OK
                        {
                            string res = JsonConvert.SerializeObject(user);
                            result += "Content-Length: " + Encoding.UTF8.GetByteCount(res) + "\r\n";
                            result += res;
                        }
                        result += "\r\n";
                        ss.BeginSend(result, (x, y) =>
                        {
                            ss.Dispose();
                            //ss.Shutdown(System.Net.Sockets.SocketShutdown.Both);
                        }, null);
                    }
                    // Handle "Join game" request
                    else if (joinGamePattern.IsMatch(firstLine))
                    {
                        UserInfo user   = JsonConvert.DeserializeObject <UserInfo>(line);
                        GameInfo game   = new BoggleService().JoinGame(user, out HttpStatusCode status);
                        String   result = "HTTP/1.1 " + (int)status + " " + status + "\r\n";
                        if ((int)status / 100 == 2) // status is OK
                        {
                            string res = JsonConvert.SerializeObject(game);
                            result += "Content-Length: " + Encoding.UTF8.GetByteCount(res) + "\r\n";
                            result += res;
                        }
                        result += "\r\n";

                        ss.BeginSend(result, (x, y) =>
                        {
                            ss.Dispose();
                            //ss.Shutdown(System.Net.Sockets.SocketShutdown.Both);
                        }, null);
                    }

                    // Handle Play word request
                    else if (playWordPattern.IsMatch(firstLine))
                    {
                        UserInfo user   = JsonConvert.DeserializeObject <UserInfo>(line);
                        string   gameid = "";
                        foreach (char a in firstLine)
                        {
                            if (Int32.TryParse(a.ToString(), out int IdontCare))
                            {
                                gameid += a.ToString();
                            }
                            if (a == 'H')
                            {
                                break;
                            }
                        }
                        WordPlayed wp     = new BoggleService().PlayWord(user, gameid, out HttpStatusCode status);
                        String     result = "HTTP/1.1 " + (int)status + " " + status + "\r\n";
                        if ((int)status / 100 == 2) // status is OK
                        {
                            string res = JsonConvert.SerializeObject(wp);
                            result += "Content-Length: " + Encoding.UTF8.GetByteCount(res) + "\r\n";
                            result += res;
                        }
                        result += "\r\n";
                        ss.BeginSend(result, (x, y) =>
                        {
                            ss.Dispose();
                            //ss.Shutdown(System.Net.Sockets.SocketShutdown.Both);
                        }, null);
                    }
                    // Handle cancel join request
                    else if (cancelJoinPattern.IsMatch(firstLine))
                    {
                        UserInfo user = JsonConvert.DeserializeObject <UserInfo>(line);
                        new BoggleService().CancelJoinRequest(user, out HttpStatusCode status);
                        String result = "HTTP/1.1 " + (int)status + " " + status + "\r\n";
                        result += "\r\n";
                        ss.BeginSend(result, (x, y) =>
                        {
                            ss.Dispose();
                            //ss.Shutdown(System.Net.Sockets.SocketShutdown.Both);
                        }, null);
                    }
                    // Handle game status request
                    else if (gameStatusPattern.IsMatch(firstLine))
                    {
                        string Brief      = "hgf";
                        int    briefIndex = firstLine.IndexOf("Brief");
                        if (briefIndex != -1)
                        {
                            if (firstLine.Substring(briefIndex + 6, 12) == "yes HTTP/1.1")
                            {
                                Brief = "yes";
                            }
                        }
                        string gameid = "";
                        foreach (char a in firstLine)
                        {
                            if (Int32.TryParse(a.ToString(), out int IdontCare))
                            {
                                gameid += a.ToString();
                            }
                            if (a == 'H')
                            {
                                break;
                            }
                        }
                        GameInfo gi     = new BoggleService().GameStatus(Brief, gameid, out HttpStatusCode status);
                        String   result = "HTTP/1.1 " + (int)status + " " + status + "\r\n";
                        if ((int)status / 100 == 2) // status is OK
                        {
                            string res = JsonConvert.SerializeObject(gi);
                            result += "Content-Length: " + Encoding.UTF8.GetByteCount(res) + "\r\n";
                            result += res;
                        }
                        result += "\r\n";
                        ss.BeginSend(result, (x, y) =>
                        {
                            ss.Dispose();
                            //ss.Shutdown(System.Net.Sockets.SocketShutdown.Both);
                        }, null);
                    }
                    else // handle invalid requests
                    {
                        String result = "HTTP/1.1 400 INVALID REQUEST\r\n\r\n";
                        ss.BeginSend(result, (x, y) =>
                        {
                            ss.Dispose();
                            //ss.Shutdown(System.Net.Sockets.SocketShutdown.Both);
                        }, null);
                    }
                }