Fixed checker validation, added debug msg
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
/*
|
||||
Compiler: MinGW g++ 6.3.0
|
||||
Compiler: MinGW g++ 6.3.0 (Windows)
|
||||
MinGW g++ 5.3.1 (Ubuntu 16.04)
|
||||
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 extra buffer character (maybe, it occurs in telnet)
|
||||
Fix validation for file checker file
|
||||
Change card0.txt file based on information given by client (p1/p2)
|
||||
*/
|
||||
|
||||
@@ -20,7 +20,9 @@ Change card0.txt file based on information given by client (p1/p2)
|
||||
#pragma comment(lib,"ws2_32.lib") //Winsock Library
|
||||
|
||||
void exampleMessage();
|
||||
void debugOutput(std::string);
|
||||
bool checkExist(std::string);
|
||||
bool overwriteCard(std::string, std::string);
|
||||
|
||||
int main (int argc, char* argv[])
|
||||
{
|
||||
@@ -70,11 +72,11 @@ int main (int argc, char* argv[])
|
||||
if(card_p1 == "" || check_p1 == "" || port < 1 || port > 65535) // failed validation
|
||||
exampleMessage();
|
||||
|
||||
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;
|
||||
debugOutput("Card P1:\t" + card_p1 + "\n");
|
||||
debugOutput("Card P2:\t" + card_p2 + "\n");
|
||||
debugOutput("Check P1:\t" + check_p1 + "\n");
|
||||
debugOutput("Check P2:\t" + check_p2 + "\n");
|
||||
debugOutput("Port\t\t" + std::to_string(port) + "\n");
|
||||
|
||||
// begin socket work - copy and pasted below (http://www.binarytides.com/code-tcp-socket-server-winsock/)
|
||||
WSADATA wsa;
|
||||
@@ -83,7 +85,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 = 19;
|
||||
int MAXRECV = 18;
|
||||
//set of socket descriptors
|
||||
fd_set readfds;
|
||||
//1 extra for null character, string termination
|
||||
@@ -242,36 +244,51 @@ 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("[p,P][1,2]:[e,E]004[(0-9)(a-f)(A-F)]{12}"); // 16 digit hexadecimal starting with e004; ex: p1:e004abcdef123456
|
||||
std::regex cardRegex("[1,2]:[e,E]004[(0-9)(a-f)(A-F)]{12}"); // 16 digit hexadecimal starting with e004; ex: 1:e004abcdef123456
|
||||
if(std::regex_match(buffer, cardRegex))
|
||||
{
|
||||
|
||||
printf("\nRecieved by: %s:%d contains: %s \n" , inet_ntoa(address.sin_addr) , ntohs(address.sin_port), buffer);
|
||||
std::cout << "Data recieved is a valid game card" << std::endl;
|
||||
debugOutput("Data recieved is a valid game card\n");
|
||||
|
||||
// validate if buffer string is for p1 or p2
|
||||
if(buffer[0] == '1') //p1
|
||||
{
|
||||
debugOutput("Looking for P1 check file in: " + check_p1 + "... ");
|
||||
if(checkExist(check_p1))
|
||||
{
|
||||
debugOutput("Found!\n");
|
||||
|
||||
overwriteCard(buffer, card_p1);
|
||||
}
|
||||
else
|
||||
{
|
||||
debugOutput("Not found!\nSkipping overwrite of card0\n");
|
||||
}
|
||||
}
|
||||
else //p2
|
||||
{
|
||||
debugOutput("Looking for P2 check file in: " + check_p2 + "... ");
|
||||
if(checkExist(check_p2))
|
||||
{
|
||||
debugOutput("Found!\n");
|
||||
|
||||
overwriteCard(buffer, card_p2);
|
||||
}
|
||||
else
|
||||
{
|
||||
debugOutput("Not found!\nSkipping overwrite of card0\n");
|
||||
}
|
||||
}
|
||||
|
||||
// check for the checker file apporiately
|
||||
std::cout << "Looking for check file in: "<< check_p1 << "... "<< (checkExist(check_p1) ? "Found" : "Not found") << std::endl; //test
|
||||
//std::cout << "Looking for check file in: "<< check_p1 << "... "<< (checkExist(check_p1) ? "Found" : "Not found") << std::endl; //test
|
||||
}
|
||||
else // don't do anything if not valid
|
||||
{
|
||||
// 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))
|
||||
//{
|
||||
// check file exists, rewrite
|
||||
// regex to validate buffer is a e004 hex number
|
||||
|
||||
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// no check file
|
||||
// std::cout << "No check file! Will not re-write: " << file << std::endl;
|
||||
//}
|
||||
//send( s , "recieved" , valread , 0 );
|
||||
}
|
||||
}
|
||||
@@ -297,8 +314,23 @@ void exampleMessage() // help menu
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void debugOutput(std::string text) // debugging text output
|
||||
{
|
||||
std::cout << text;
|
||||
}
|
||||
|
||||
bool checkExist (std::string fileName)
|
||||
{
|
||||
std::ifstream infile(fileName);
|
||||
return infile.good();
|
||||
}
|
||||
|
||||
bool overwriteCard(std::string buffer, std::string location)
|
||||
{
|
||||
// split card up from data
|
||||
std::string card (buffer);
|
||||
card = card.substr(2, 16); // remove player number and colon (1:) to produce card number only
|
||||
|
||||
// check if card exists
|
||||
debugOutput("Card data: " + card + "\n");
|
||||
}
|
Binary file not shown.
Reference in New Issue
Block a user