77 lines
1.9 KiB
C
77 lines
1.9 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <netinet/in.h>
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <arpa/inet.h>
|
|
#include <netdb.h>
|
|
#include <pthread.h>
|
|
#include "EasySockets.h"
|
|
#include "Game.h"
|
|
#include "LinkedList.h"
|
|
#include "Player.h"
|
|
|
|
bool wait =false;
|
|
void* WaitForInput(){
|
|
if (wait) {char c = getchar();}
|
|
wait=false;
|
|
}
|
|
int socket_id;
|
|
struct sockaddr_in incoming;
|
|
void* JoiningThread(){
|
|
while (true) {
|
|
int a = AcceptClient(socket_id, &incoming);
|
|
Player* p = CreatePlayer("tttt", a, incoming);
|
|
AddPlayerToList(p);
|
|
printf("Players: %d/200", GetPlayersCount());
|
|
}
|
|
return 0;
|
|
}
|
|
void InterpretReceivedSignal(Player* p, const char* buffer, int len){
|
|
|
|
}
|
|
void* PlayerDataReaderThread(void* arg){
|
|
char buffer[1000];
|
|
Player* p = (Player*)arg;
|
|
printf("%d", p->points);
|
|
socklen_t size = sizeof(p->incoming);
|
|
recvfrom(socket_id, buffer, 1000, 0, (struct sockaddr*)&p->incoming, &size );
|
|
|
|
}
|
|
int BeginGame(){
|
|
//Due to security, we use recvfrom instead of regular recv
|
|
for (int i=0; i<200; i++) {
|
|
if (playerConnected[i]) {
|
|
Player* p = players[i];
|
|
printf("\n.");
|
|
pthread_create(&p->threadId, NULL, PlayerDataReaderThread, p);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
int main() {
|
|
InitPlayers();
|
|
setvbuf(stdout, NULL, _IONBF,0);
|
|
socket_id = CreateSocket();
|
|
//Connect(socket_id, "0.0.0.0", 12345);
|
|
int c = CreateServer(socket_id, 1234);
|
|
printf("%d",c);
|
|
|
|
printf("Waiting for players to connect. \n Press any key to start...");
|
|
wait=true;
|
|
pthread_t thread_id;
|
|
pthread_create(&thread_id, NULL, JoiningThread, NULL);
|
|
getchar();
|
|
printf("...");
|
|
Player* p = CreatePlayer("test", 15, incoming);
|
|
AddPlayerToList(p);
|
|
pthread_cancel(thread_id);
|
|
printf("Press any key to close the server");
|
|
BeginGame();
|
|
getchar();
|
|
return 0;
|
|
|
|
} |