Skip to content
Snippets Groups Projects
Commit 9ec8bae9 authored by weijiel6's avatar weijiel6
Browse files

final submit one

parent 40353e6d
No related branches found
No related tags found
No related merge requests found
...@@ -25,6 +25,8 @@ ...@@ -25,6 +25,8 @@
// used to check whether the game is finished // used to check whether the game is finished
bool check_end(int sockfd, char* word_putin); bool check_end(int sockfd, char* word_putin);
bool make_file(int sockfd, char *filepath);
// constants // constants
static char const * const HTTP_200_FORMAT = "HTTP/1.1 200 OK\r\n\ static char const * const HTTP_200_FORMAT = "HTTP/1.1 200 OK\r\n\
Content-Type: text/html\r\n\ Content-Type: text/html\r\n\
...@@ -123,30 +125,8 @@ static bool handle_http_request(int sockfd) ...@@ -123,30 +125,8 @@ static bool handle_http_request(int sockfd)
if (*curr == ' ') if (*curr == ' ')
if (method == GET) if (method == GET)
{ {
// get the size of the file // send the intro html
struct stat st; make_file(sockfd , "1_intro.html");
stat("1_intro.html", &st);
n = sprintf(buff, HTTP_200_FORMAT, st.st_size);
// send the header first
if (write(sockfd, buff, n) < 0)
{
perror("write");
return false;
}
// send the file
int filefd = open("1_intro.html", O_RDONLY);
do
{
n = sendfile(sockfd, filefd, NULL, 2048);
}
while (n > 0);
if (n < 0)
{
perror("sendfile");
close(filefd);
return false;
}
close(filefd);
} }
else if (method == POST) else if (method == POST)
{ {
...@@ -161,30 +141,8 @@ static bool handle_http_request(int sockfd) ...@@ -161,30 +141,8 @@ static bool handle_http_request(int sockfd)
player2_r = false; player2_r = false;
quit_game = true; quit_game = true;
} }
// get the size of the file // send the game over html
struct stat st; make_file(sockfd , "7_gameover.html");
stat("7_gameover.html", &st);
n = sprintf(buff, HTTP_200_FORMAT, st.st_size);
// send the header first
if (write(sockfd, buff, n) < 0)
{
perror("write");
return false;
}
// send the file
int filefd = open("7_gameover.html", O_RDONLY);
do
{
n = sendfile(sockfd, filefd, NULL, 2048);
}
while (n > 0);
if (n < 0)
{
perror("sendfile");
close(filefd);
return false;
}
close(filefd);
} }
else if (strstr(buff, "user=") != NULL) { else if (strstr(buff, "user=") != NULL) {
// locate the username, it is safe to do so in this sample code, but usually the result is expected to be // locate the username, it is safe to do so in this sample code, but usually the result is expected to be
...@@ -249,36 +207,14 @@ static bool handle_http_request(int sockfd) ...@@ -249,36 +207,14 @@ static bool handle_http_request(int sockfd)
else if (*curr == '?') { else if (*curr == '?') {
if (method == GET) if (method == GET)
{ {
// change the players state // change the players active state
if (sockfd == player1_s) { if (sockfd == player1_s) {
player1_r = true; player1_r = true;
} else if (sockfd == player2_s) { } else if (sockfd == player2_s) {
player2_r = true; player2_r = true;
} }
// get the size of the file // send the first turn in html
struct stat st; make_file(sockfd , "3_first_turn.html");
stat("3_first_turn.html", &st);
n = sprintf(buff, HTTP_200_FORMAT, st.st_size);
// send the header first
if (write(sockfd, buff, n) < 0)
{
perror("write");
return false;
}
// send the file
int filefd = open("3_first_turn.html", O_RDONLY);
do
{
n = sendfile(sockfd, filefd, NULL, 2048);
}
while (n > 0);
if (n < 0)
{
perror("sendfile");
close(filefd);
return false;
}
close(filefd);
} }
else if (method == POST) else if (method == POST)
{ {
...@@ -293,30 +229,8 @@ static bool handle_http_request(int sockfd) ...@@ -293,30 +229,8 @@ static bool handle_http_request(int sockfd)
player2_r = false; player2_r = false;
quit_game = true; quit_game = true;
} }
// get the size of the file // send the game over html
struct stat st; make_file(sockfd , "7_gameover.html");
stat("7_gameover.html", &st);
n = sprintf(buff, HTTP_200_FORMAT, st.st_size);
// send the header first
if (write(sockfd, buff, n) < 0)
{
perror("write");
return false;
}
// send the file
int filefd = open("7_gameover.html", O_RDONLY);
do
{
n = sendfile(sockfd, filefd, NULL, 2048);
}
while (n > 0);
if (n < 0)
{
perror("sendfile");
close(filefd);
return false;
}
close(filefd);
} }
else if (strstr(buff, "guess=") != NULL) { else if (strstr(buff, "guess=") != NULL) {
...@@ -325,30 +239,9 @@ static bool handle_http_request(int sockfd) ...@@ -325,30 +239,9 @@ static bool handle_http_request(int sockfd)
// if the quit game flag is true would quit the game // if the quit game flag is true would quit the game
if (quit_game) { if (quit_game) {
// get the size of the file
struct stat st; // send the game over html
stat("7_gameover.html", &st); make_file(sockfd , "7_gameover.html");
n = sprintf(buff, HTTP_200_FORMAT, st.st_size);
// send the header first
if (write(sockfd, buff, n) < 0)
{
perror("write");
return false;
}
// send the file
int filefd = open("7_gameover.html", O_RDONLY);
do
{
n = sendfile(sockfd, filefd, NULL, 2048);
}
while (n > 0);
if (n < 0)
{
perror("sendfile");
close(filefd);
return false;
}
close(filefd);
} }
// if the end game flag is ture would end the game // if the end game flag is ture would end the game
...@@ -362,30 +255,8 @@ static bool handle_http_request(int sockfd) ...@@ -362,30 +255,8 @@ static bool handle_http_request(int sockfd)
player2_r = false; player2_r = false;
player2_w_len = 0; player2_w_len = 0;
} }
// get the size of the file // send the end game html
struct stat st; make_file(sockfd , "6_endgame.html");
stat("6_endgame.html", &st);
n = sprintf(buff, HTTP_200_FORMAT, st.st_size);
// send the header first
if (write(sockfd, buff, n) < 0)
{
perror("write");
return false;
}
// send the file
int filefd = open("6_endgame.html", O_RDONLY);
do
{
n = sendfile(sockfd, filefd, NULL, 2048);
}
while (n > 0);
if (n < 0)
{
perror("sendfile");
close(filefd);
return false;
}
close(filefd);
} }
// if both players are ready, would send accepted file // if both players are ready, would send accepted file
...@@ -404,32 +275,12 @@ static bool handle_http_request(int sockfd) ...@@ -404,32 +275,12 @@ static bool handle_http_request(int sockfd)
player2_r = false; player2_r = false;
player2_w_len = 0; player2_w_len = 0;
} }
// get the size of the file // send the end game html
struct stat st; make_file(sockfd , "6_endgame.html");
stat("6_endgame.html", &st);
n = sprintf(buff, HTTP_200_FORMAT, st.st_size);
// send the header first
if (write(sockfd, buff, n) < 0)
{
perror("write");
return false;
}
// send the file
int filefd = open("6_endgame.html", O_RDONLY);
do
{
n = sendfile(sockfd, filefd, NULL, 2048);
}
while (n > 0);
if (n < 0)
{
perror("sendfile");
close(filefd);
return false;
}
close(filefd);
} else { } else {
// keep put ward in and guess // keep put ward in and guess
// get the size of the file // get the size of the file
struct stat st; struct stat st;
stat("4_accepted.html", &st); stat("4_accepted.html", &st);
...@@ -457,9 +308,31 @@ static bool handle_http_request(int sockfd) ...@@ -457,9 +308,31 @@ static bool handle_http_request(int sockfd)
} }
} else { } else {
// not both players active so send discarded file // not both players active so send discarded file
// send the end game html
make_file(sockfd , "5_discarded.html");
}
}
else
// never used, just for completeness
fprintf(stderr, "no other methods supported");
}
}
// send 404
else if (write(sockfd, HTTP_404, HTTP_404_LENGTH) < 0)
{
perror("write");
return false;
}
return true;
}
bool make_file(int sockfd, char *filepath) {
char buff[2049];
int n;
// get the size of the file // get the size of the file
struct stat st; struct stat st;
stat("5_discarded.html", &st); stat(filepath, &st);
n = sprintf(buff, HTTP_200_FORMAT, st.st_size); n = sprintf(buff, HTTP_200_FORMAT, st.st_size);
// send the header first // send the header first
if (write(sockfd, buff, n) < 0) if (write(sockfd, buff, n) < 0)
...@@ -468,7 +341,7 @@ static bool handle_http_request(int sockfd) ...@@ -468,7 +341,7 @@ static bool handle_http_request(int sockfd)
return false; return false;
} }
// send the file // send the file
int filefd = open("5_discarded.html", O_RDONLY); int filefd = open(filepath, O_RDONLY);
do do
{ {
n = sendfile(sockfd, filefd, NULL, 2048); n = sendfile(sockfd, filefd, NULL, 2048);
...@@ -481,23 +354,10 @@ static bool handle_http_request(int sockfd) ...@@ -481,23 +354,10 @@ static bool handle_http_request(int sockfd)
return false; return false;
} }
close(filefd); close(filefd);
}
}
else
// never used, just for completeness
fprintf(stderr, "no other methods supported");
}
}
// send 404
else if (write(sockfd, HTTP_404, HTTP_404_LENGTH) < 0)
{
perror("write");
return false;
}
return true; return true;
} }
// check the word list and change the end game flag and put the word into list // check the word list and change the end game flag and put the word into list
bool check_end(int sockfd, char* word_putin) { bool check_end(int sockfd, char* word_putin) {
if (sockfd == player1_s) { if (sockfd == player1_s) {
......
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment