Added P1/P2 functionality

This commit is contained in:
2017-09-05 21:11:15 -05:00
parent 2f4f8c1a02
commit 0001ea92db
2 changed files with 34 additions and 19 deletions

View File

@@ -1,11 +1,11 @@
/*
Compiler: g++
Compiler: MinGW g++ 6.3.0
Flags: -std=c++11 -static-libgcc -static-libstdc++ -lws2_32
Run ex: cardnet.exe -c1="G:\card0.txt" -c2="H:\card0.txt" -l1="G:\check.txt" -l2="H:\check.txt" -p="4500"
TODO:
Fix regex validation, should be 16 character hexadecimal starting with e004
Fix validation for file checker file
Change card0.txt file based on information given by client
Change card0.txt file based on information given by client (p1/p2)
*/
#include <iostream>
@@ -21,11 +21,12 @@ void exampleMessage();
int main (int argc, char* argv[])
{
std::string file, check; // file = card0.txt ; check = check.txt
std::string card_p1, card_p2, check_p1, check_p2; // file = card0.txt ; check = check.txt
int port = 0; // port number to listen
argh::parser cmdl; // command line parser
cmdl.parse(argc, argv, argh::parser::PREFER_PARAM_FOR_UNREG_OPTION);
std::cout << std::endl; // space out for formatting
if(cmdl["-h"]) // help
{
@@ -34,13 +35,21 @@ int main (int argc, char* argv[])
for(auto& param: cmdl.params())
{
if(!param.first.compare("f")) // card0 file
if(!param.first.compare("c1")) // card0 p1
{
file = param.second;
card_p1 = param.second;
}
if(!param.first.compare("c")) // checker file
if(!param.first.compare("c2")) // card0 p2
{
check = param.second;
card_p2 = param.second;
}
if(!param.first.compare("l1")) // checker p1
{
check_p1 = param.second;
}
if(!param.first.compare("l2")) // checker p2
{
check_p2 = param.second;
}
if(!param.first.compare("p")) // port
{
@@ -57,12 +66,14 @@ int main (int argc, char* argv[])
}
}
if(file == "" || check == "" || port == 0) // failed validation
if(card_p1 == "" || check_p1 == "" || port == 0) // failed validation
exampleMessage();
std::cout << "File:\t" << file << std::endl;
std::cout << "Check:\t" << check << std::endl;
std::cout << "Port:\t" << port << std::endl;
std::cout << "Card P1:\t" << card_p1 << std::endl;
std::cout << "Card P2:\t" << card_p2 << std::endl;
std::cout << "Check P1:\t" << check_p1 << std::endl;
std::cout << "Check P2:\t" << check_p2 << std::endl;
std::cout << "Port:\t\t" << port << std::endl;
// begin socket work - copy and pasted below (http://www.binarytides.com/code-tcp-socket-server-winsock/)
WSADATA wsa;
@@ -71,7 +82,7 @@ int main (int argc, char* argv[])
int max_clients = 30 , activity, addrlen, i, valread;
//size of our receive buffer, this is string length.
int MAXRECV = 16;
int MAXRECV = 19;
//set of socket descriptors
fd_set readfds;
//1 extra for null character, string termination
@@ -160,7 +171,7 @@ int main (int argc, char* argv[])
}
//inform user of socket number - used in send and receive commands
printf("New connection , socket fd is %d , ip is : %s , port : %d \n" , new_socket , inet_ntoa(address.sin_addr) , ntohs(address.sin_port));
printf("New connection - socket fd: %d , ip: %s , port: %d \n" , new_socket , inet_ntoa(address.sin_addr) , ntohs(address.sin_port));
/*//send new connection greeting message
if( send(new_socket, message, strlen(message), 0) != strlen(message) )
@@ -230,7 +241,7 @@ int main (int argc, char* argv[])
buffer[valread] = '\0';
// the 'buffer' is the string to use to edit our card0.txt file
std::regex cardRegex("[e,E]004[(0-9)(a-f)(A-F)]{12}"); // 16 digit hexadecimal starting with e004
std::regex cardRegex("[p,P][1,2]:[e,E]004[(0-9)(a-f)(A-F)]{12}"); // 16 digit hexadecimal starting with e004; ex: p1:e004abcdef123456
if(std::regex_match(buffer, cardRegex))
{
@@ -239,7 +250,8 @@ int main (int argc, char* argv[])
}
else // don't do anything if not valid
{
std::cout << "Data recieved is not a valid game card" << std::endl;
// this needs work, it may get an extra character in the buffer and goes in here
//std::cout << "Data recieved is not a valid game card" << std::endl;
}
// check if the check file exists
//if(std::ifstream(check))
@@ -269,10 +281,13 @@ int main (int argc, char* argv[])
void exampleMessage() // help menu
{
std::cout << "Availble options:" << std::endl;
std::cout << " -f\tLocation of card0.txt file" << std::endl;
std::cout << " -c\tLocation of check file" << std::endl;
std::cout << " -c1\tLocation of player 1's card0.txt file" << std::endl;
std::cout << " -c2\tLocation of player 2's card0.txt file" << std::endl;
std::cout << " -l1\tLocation of player 1's check file" << std::endl;
std::cout << " -l2\tLocation of player 2's check file" << std::endl;
std::cout << " -p\tPort number to listen on (1-65535)" << std::endl;
std::cout << std::endl << "Example usage: cardnet.exe -f G:\\card0.txt -c G:\\check.txt -p 4500" << std::endl;
std::cout << std::endl << "Ex (1P): cardnet.exe -c1=\"G:\\card0.txt\" -l1=\"G:\\check.txt\" -p=\"4500\"" << std::endl;
std::cout << "Ex (2P): cardnet.exe -c1=\"G:\\card0.txt\" -c2=\"H:\\card0.txt\" -l1=\"G:\\check.txt\" -l2=\"H:\\check.txt\" -p=\"4500\"" << std::endl;
exit(0);
}

Binary file not shown.