Fix bug where we passed client info to the thread by ref, like an ape
This commit is contained in:
parent
56e767bb5b
commit
e01a718ac6
2 changed files with 17 additions and 10 deletions
|
@ -39,13 +39,15 @@ int runClient(int argc, char* argv[])
|
||||||
portno = TTRTS_PORT;
|
portno = TTRTS_PORT;
|
||||||
|
|
||||||
// Create a new socket
|
// Create a new socket
|
||||||
// AF_INET is general internetsocked domain
|
// AF_INET is general internetsocket domain
|
||||||
// SOCK_STREAM as messages will be read in on this socket, SOCK_DGRAM would be for packets
|
// SOCK_STREAM as messages will be read in on this socket, SOCK_DGRAM would be for packets
|
||||||
// 0 is for default protocol
|
// 0 is for default protocol
|
||||||
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (sockfd < 0)
|
if (sockfd < 0)
|
||||||
error("ERROR opening socket");
|
error("ERROR opening socket");
|
||||||
|
|
||||||
|
std::cout<<"Opened socket on "<<sockfd<<std::endl;
|
||||||
|
|
||||||
// Get the hostent information for the host by name
|
// Get the hostent information for the host by name
|
||||||
server = gethostbyname(argv[1]);
|
server = gethostbyname(argv[1]);
|
||||||
if (server == NULL)
|
if (server == NULL)
|
||||||
|
@ -73,6 +75,7 @@ int runClient(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
memset(buffer,0,sizeof(buffer));
|
memset(buffer,0,sizeof(buffer));
|
||||||
|
|
||||||
|
std::cout<<"Waiting for gamestate"<<std::endl;
|
||||||
// Receive gamestate
|
// Receive gamestate
|
||||||
if (read(sockfd,buffer,sizeof(buffer)-1) < 0)
|
if (read(sockfd,buffer,sizeof(buffer)-1) < 0)
|
||||||
error("ERROR reading from client");
|
error("ERROR reading from client");
|
||||||
|
@ -80,16 +83,16 @@ int runClient(int argc, char* argv[])
|
||||||
std::cout<<buffer<<std::endl;
|
std::cout<<buffer<<std::endl;
|
||||||
|
|
||||||
// Output orders
|
// Output orders
|
||||||
memset(buffer,0,sizeof(buffer));
|
std::string orders = std::to_string(sockfd);
|
||||||
strcpy(buffer, "ORDER:F id:1\nEND");
|
|
||||||
|
|
||||||
// Place a test message into the buffer
|
|
||||||
strncpy(buffer,buffer,sizeof(buffer));
|
|
||||||
|
|
||||||
|
std::cout<<"Sending orders"<<std::endl;
|
||||||
|
std::cout<<orders<<std::endl;
|
||||||
// Write to the socket with the buffer
|
// Write to the socket with the buffer
|
||||||
n = write(sockfd,buffer,strlen(buffer));
|
n = write(sockfd,orders.c_str(),orders.length());
|
||||||
if (n < 0)
|
if (n < 0)
|
||||||
error("ERROR writing to socket");
|
error("ERROR writing to socket");
|
||||||
|
|
||||||
|
std::cout<<"Order Sent"<<std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -28,11 +28,13 @@ struct ClientInfo
|
||||||
int clientsockfd;
|
int clientsockfd;
|
||||||
};
|
};
|
||||||
|
|
||||||
int waitForOrdersFromClient(const ClientInfo& info, std::mutex& mut, CTTRTSGame& game )
|
int waitForOrdersFromClient(const ClientInfo info, std::mutex& mut, CTTRTSGame& game )
|
||||||
{
|
{
|
||||||
char buffer[1028]; // buffer for orders
|
char buffer[1028]; // buffer for orders
|
||||||
memset(buffer,0,sizeof(buffer));
|
memset(buffer,0,sizeof(buffer));
|
||||||
|
|
||||||
|
std::cout<<"Waiting for "<<inet_ntoa(info.cli_addr.sin_addr)<<std::endl;
|
||||||
|
|
||||||
// Read in the new socket
|
// Read in the new socket
|
||||||
// read will block until the client has called write
|
// read will block until the client has called write
|
||||||
// up to the full size of the buffer
|
// up to the full size of the buffer
|
||||||
|
@ -40,6 +42,7 @@ int waitForOrdersFromClient(const ClientInfo& info, std::mutex& mut, CTTRTSGame&
|
||||||
error("ERROR reading from client");
|
error("ERROR reading from client");
|
||||||
|
|
||||||
std::cout<<"Recieved orders from "<<inet_ntoa(info.cli_addr.sin_addr)<<std::endl;
|
std::cout<<"Recieved orders from "<<inet_ntoa(info.cli_addr.sin_addr)<<std::endl;
|
||||||
|
std::cout<<buffer<<std::endl;
|
||||||
|
|
||||||
mut.lock();
|
mut.lock();
|
||||||
game.IssueOrders(player_t::Red , buffer);
|
game.IssueOrders(player_t::Red , buffer);
|
||||||
|
@ -127,7 +130,7 @@ int runServer(int argc, char* argv[])
|
||||||
if (clientsockfd < 0)
|
if (clientsockfd < 0)
|
||||||
error("ERROR on accept");
|
error("ERROR on accept");
|
||||||
|
|
||||||
std::cout<<"Client connected from "<<inet_ntoa(cli_addr.sin_addr)<<std::endl;
|
std::cout<<"Client connected from "<<inet_ntoa(cli_addr.sin_addr)<<" socket "<<clientsockfd<<std::endl;
|
||||||
myClients.push_back({cli_addr,clientsockfd});
|
myClients.push_back({cli_addr,clientsockfd});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,7 +159,7 @@ int runServer(int argc, char* argv[])
|
||||||
std::vector<std::thread> clientThreads;
|
std::vector<std::thread> clientThreads;
|
||||||
for(auto client : myClients)
|
for(auto client : myClients)
|
||||||
{
|
{
|
||||||
std::thread clientThread(waitForOrdersFromClient,std::ref(client), std::ref(gameMutex), std::ref(game));
|
std::thread clientThread(waitForOrdersFromClient, client, std::ref(gameMutex), std::ref(game));
|
||||||
clientThreads.push_back(std::move(clientThread));
|
clientThreads.push_back(std::move(clientThread));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -166,6 +169,7 @@ int runServer(int argc, char* argv[])
|
||||||
thread.join();
|
thread.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::cout<<"Orders recieved, simulating turn"<<std::endl;
|
||||||
// Step to the next turn
|
// Step to the next turn
|
||||||
gameMutex.lock();
|
gameMutex.lock();
|
||||||
game.SimulateToNextTurn();
|
game.SimulateToNextTurn();
|
||||||
|
|
Loading…
Add table
Reference in a new issue