Exemple #1
0
 public BoggleWebServer()
 {
     server = new TcpListener(IPAddress.Any, 60000);
     server.Start();
     server.BeginAcceptSocket(ConnectionRequested, null);
     service = new BoggleService();
 }
        /// <summary>
        /// creates a server and calls begin receive
        /// </summary>
        /// <param name="s"></param>
        public SocketComm(SS s)
        {
            socket  = s;
            service = new BoggleService();


            socket.BeginReceive(Received, null);
        }
Exemple #3
0
        public void testToken()
        {
            String        token = Guid.NewGuid().ToString();
            BoggleService A     = new BoggleService();

            Assert.IsTrue(A.checkUserTokenValid(token));
            Assert.IsFalse(A.checkUserTokenValid("aaa"));
        }
Exemple #4
0
 public HttpRequest(StringSocket stringSocket, BoggleService boggleservice)
 {
     this.ss            = stringSocket;
     this.boggleservice = boggleservice;
     ss.BeginReceive(LineReceived, null);
     ������������ //initialize httpstatus to 400 Bad Request -- change in instance that request is valid/successful
     ������������ //httpstatus = "400 Bad Request";
         newpayload = new string[3];
 }
Exemple #5
0
        public void testCalculateScore()
        {
            BoggleService boggle = new BoggleService();

            Assert.AreEqual(0, boggle.CalculateScore("a"));
            Assert.AreEqual(7, boggle.CalculateScore("aaaaaaaa"));
            Assert.AreEqual(1, boggle.CalculateScore("aaa"));
            Assert.AreEqual(2, boggle.CalculateScore("aaaaa"));
            Assert.AreEqual(3, boggle.CalculateScore("aaaaaa"));
            Assert.AreEqual(5, boggle.CalculateScore("aaaaaaa"));
        }
        /// <summary>
        /// Creates a ClientConnection from the socket, then begins communicating with it.
        /// </summary>
        public ClientConnection(Socket s, BoggleService Server)
        {
            // Record the socket and clear incoming
            socket   = s;
            incoming = new StringBuilder();
            outgoing = new StringBuilder();

            server = Server;

            // Ask the socket to call MessageReceive as soon as up to 1024 bytes arrive.
            socket.BeginReceive(incomingBytes, 0, incomingBytes.Length, SocketFlags.None, MessageReceived, null);
        }
        /// <summary>
        /// Creates a SimpleChatServer that listens for connection requests on port 4000.
        /// </summary>
        public BoggleServer(int port)
        {
            // A TcpListener listens for incoming connection requests
            server = new TcpListener(IPAddress.Any, port);
            InternalBoggleServer = new BoggleService();
            // Start the TcpListener
            server.Start();

            // Ask the server to call ConnectionRequested at some point in the future when
            // a connection request arrives.  It could be a very long time until this happens.
            // The waiting and the calling will happen on another thread.  BeginAcceptSocket
            // returns immediately, and the constructor returns to Main.
            server.BeginAcceptSocket(ConnectionRequested, null);
        }
Exemple #8
0
        private void ContentReceived(string s, Exception e, object payload)
        {
            if (s != null)
            {
                Name name = JsonConvert.DeserializeObject <Name>(s);

                // Call service method
                BoggleService service = new BoggleService();
                string        result  =
                    JsonConvert.SerializeObject(
                        service.MakeUser(name),
                        new JsonSerializerSettings {
                    NullValueHandling = NullValueHandling.Ignore
                });


                ss.BeginSend("HTTP/1.1 " + (int)service.ResponseStatus + " " + service.ResponseStatus + "\r\n", Ignore, null);
                ss.BeginSend("Content-Type: application/json\r\n", Ignore, null);
                ss.BeginSend("Content-Length: " + result.Length + "\r\n", Ignore, null);
                ss.BeginSend("\r\n", Ignore, null);
                ss.BeginSend(result, (ex, py) => { ss.Shutdown(); }, null);
            }
        }
Exemple #9
0
 public HttpRequest(StringSocket stringSocket, BoggleService s)
 {
     this.ss      = stringSocket;
     this.service = s;
     ss.BeginReceive(LineReceived, null);
 }
        /// <summary>
        /// Handles the request after all relevant info has been parsed out
        /// </summary>
        /// <param name="line"></param>
        /// <param name="p"></param>
        private void ProcessRequest(string line, object p = null)
        {
            String result = "";

            // this handles 'make user' request
            if (makeUserPattern.IsMatch(firstLine))
            {
                UserNickames name = JsonConvert.DeserializeObject <UserNickames>(line);
                dynamic      user = new BoggleService().Register(name, out HttpStatusCode status);
                result = ComposeResponse(user, status);
            }
            // this handles 'join game' request
            else if (joinGamePattern.IsMatch(firstLine))
            {
                GameRequest  request  = JsonConvert.DeserializeObject <GameRequest>(line);
                JoinResponse response = new BoggleService().Join(request, out HttpStatusCode status);
                result = ComposeResponse(response, status);
            }
            // this handles 'update game status' with brief parameter on or off
            else if (updateNoBriefPattern.IsMatch(firstLine) || updateBriefPattern.IsMatch(firstLine))
            {
                Match  m;
                string gameID     = "";
                string briefParam = null;
                string brief      = "";

                // if brief parameter is not provided
                if (updateNoBriefPattern.Match(firstLine).Success)
                {
                    m      = updateNoBriefPattern.Match(firstLine);
                    gameID = m.Groups[1].ToString();
                }

                // or not provided, parse brief parameter
                if (updateBriefPattern.Match(firstLine).Success)
                {
                    m = updateBriefPattern.Match(firstLine);

                    // game ID is embedded in first group of regex pattern
                    gameID = m.Groups[1].ToString();

                    // brief parameter is embedded in second group of regex pattern
                    briefParam = m.Groups[2].ToString();

                    // remove irrelevent strings
                    brief = briefParam.Substring(7);
                }

                GameStatus     response;
                HttpStatusCode status;
                if (briefParam == null)
                {
                    response = new BoggleService().Update(gameID, "No", out status);
                }
                else
                {
                    response = new BoggleService().Update(gameID, brief, out status);
                }

                result = ComposeResponse(response, status);
            }
            // this handles playword request
            else if (playWordPattern.IsMatch(firstLine))
            {
                string       gameID   = playWordPattern.Match(firstLine).Groups[1].ToString();
                PlayRequest  request  = JsonConvert.DeserializeObject <PlayRequest>(line);
                PlayResponse response = new BoggleService().PlayWord(request, gameID, out HttpStatusCode status);
                result = ComposeResponse(response, status);
            }
            // this handles cancel game request
            else if (cancelPattern.IsMatch(firstLine))
            {
                UserObject user = JsonConvert.DeserializeObject <UserObject>(line);
                new BoggleService().CancelJoinRequest(user, out HttpStatusCode status);
                result = ComposeResponse(null, status);
            }
            // capturing whatever string requests that does not match any of the above regex patterns
            else
            {
                result = "HTTP/1.1 " + "403" + " Forbidden" + "\r\n\r\n";
            }
            socket.BeginSend(result, (x, y) => { socket.Shutdown(SocketShutdown.Both); }, null);
        }
Exemple #11
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);
                    }
                }