Pull base netcode functions out into net files
This commit is contained in:
parent
b43927a1da
commit
b34b933dcd
3 changed files with 88 additions and 71 deletions
|
@ -1 +1,63 @@
|
|||
#include "net.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int WaitForOrdersFromClient(const ClientInfo info, std::mutex &mut, CTTRTSGame &game)
|
||||
{
|
||||
char buffer[1028]; // buffer for orders
|
||||
memset(buffer,0,sizeof(buffer));
|
||||
|
||||
std::cout<<"Waiting for "<<inet_ntoa(info.cli_addr.sin_addr)<<std::endl;
|
||||
|
||||
// Read in the new socket
|
||||
// read will block until the client has called write
|
||||
// up to the full size of the buffer
|
||||
if (read(info.clientsockfd,buffer,sizeof(buffer)-1) < 0)
|
||||
error("ERROR reading from client");
|
||||
|
||||
std::cout<<"Recieved orders from "<<inet_ntoa(info.cli_addr.sin_addr)<<std::endl;
|
||||
std::cout<<buffer<<std::endl;
|
||||
|
||||
mut.lock();
|
||||
game.IssueOrders(info.player , buffer);
|
||||
mut.unlock();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void GetOrdersFromClients(std::vector<ClientInfo> &myClients, CTTRTSGame &game, std::mutex &gameMutex)
|
||||
{
|
||||
// Spawn threads
|
||||
std::vector<std::thread> clientThreads;
|
||||
for(auto client : myClients)
|
||||
{
|
||||
std::thread clientThread(WaitForOrdersFromClient, client, ref(gameMutex), std::ref(game));
|
||||
clientThreads.push_back(move(clientThread));
|
||||
}
|
||||
|
||||
// Join up all the threads
|
||||
for ( std::thread& thread : clientThreads )
|
||||
{
|
||||
thread.join();
|
||||
}
|
||||
}
|
||||
|
||||
void SendGameInfoToClients(std::vector<ClientInfo> &myClients, const CTTRTSGame &game, std::mutex &gameMutex)
|
||||
{
|
||||
gameMutex.lock();
|
||||
std::string gamestate_string = GetStringFromGame(game);
|
||||
gameMutex.unlock();
|
||||
|
||||
for (auto client : myClients)
|
||||
{
|
||||
// Write to the socket with the buffer
|
||||
if ( write( client.clientsockfd, gamestate_string.c_str(), gamestate_string.length() ) < 0 )
|
||||
error("ERROR sending to client");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,34 @@
|
|||
#ifndef _TTRTS_NET_H_
|
||||
#define _TTRTS_NET_H_
|
||||
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <game.h>
|
||||
#include <formatters.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// Struct for net client info
|
||||
struct ClientInfo
|
||||
{
|
||||
sockaddr_in cli_addr;
|
||||
int clientsockfd;
|
||||
player_t player;
|
||||
};
|
||||
|
||||
int WaitForOrdersFromClient(const ClientInfo info, std::mutex &mut, CTTRTSGame &game);
|
||||
|
||||
void GetOrdersFromClients(std::vector<ClientInfo> &myClients, CTTRTSGame &game, std::mutex &gameMutex);
|
||||
|
||||
void SendGameInfoToClients(std::vector<ClientInfo> &myClients, const CTTRTSGame &game, std::mutex &gameMutex);
|
||||
|
||||
inline void error(const char *msg)
|
||||
{
|
||||
perror(msg);
|
||||
|
|
|
@ -10,79 +10,9 @@
|
|||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <game.h>
|
||||
#include <formatters.h>
|
||||
|
||||
#include "net.h"
|
||||
#include "filesystem.h"
|
||||
|
||||
// Struct for net client info
|
||||
struct ClientInfo
|
||||
{
|
||||
sockaddr_in cli_addr;
|
||||
int clientsockfd;
|
||||
player_t player;
|
||||
};
|
||||
|
||||
int WaitForOrdersFromClient(const ClientInfo info, std::mutex &mut, CTTRTSGame &game)
|
||||
{
|
||||
char buffer[1028]; // buffer for orders
|
||||
memset(buffer,0,sizeof(buffer));
|
||||
|
||||
std::cout<<"Waiting for "<<inet_ntoa(info.cli_addr.sin_addr)<<std::endl;
|
||||
|
||||
// Read in the new socket
|
||||
// read will block until the client has called write
|
||||
// up to the full size of the buffer
|
||||
if (read(info.clientsockfd,buffer,sizeof(buffer)-1) < 0)
|
||||
error("ERROR reading from client");
|
||||
|
||||
std::cout<<"Recieved orders from "<<inet_ntoa(info.cli_addr.sin_addr)<<std::endl;
|
||||
std::cout<<buffer<<std::endl;
|
||||
|
||||
mut.lock();
|
||||
game.IssueOrders(info.player , buffer);
|
||||
mut.unlock();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void GetOrdersFromClients(std::vector<ClientInfo> &myClients, CTTRTSGame &game, std::mutex &gameMutex)
|
||||
{
|
||||
// Spawn threads
|
||||
std::vector<std::thread> clientThreads;
|
||||
for(auto client : myClients)
|
||||
{
|
||||
std::thread clientThread(WaitForOrdersFromClient, client, ref(gameMutex), std::ref(game));
|
||||
clientThreads.push_back(move(clientThread));
|
||||
}
|
||||
|
||||
// Join up all the threads
|
||||
for ( std::thread& thread : clientThreads )
|
||||
{
|
||||
thread.join();
|
||||
}
|
||||
}
|
||||
|
||||
void SendGameInfoToClients(std::vector<ClientInfo> &myClients, const CTTRTSGame &game, std::mutex &gameMutex)
|
||||
{
|
||||
gameMutex.lock();
|
||||
std::string gamestate_string = GetStringFromGame(game);
|
||||
gameMutex.unlock();
|
||||
|
||||
for (auto client : myClients)
|
||||
{
|
||||
// Write to the socket with the buffer
|
||||
if ( write( client.clientsockfd, gamestate_string.c_str(), gamestate_string.length() ) < 0 )
|
||||
error("ERROR sending to client");
|
||||
}
|
||||
}
|
||||
|
||||
int runServer(int argc, char* argv[])
|
||||
{
|
||||
std::cout<<"Setting up server on port "<<TTRTS_PORT<<std::endl;
|
||||
|
|
Loading…
Add table
Reference in a new issue