DSIK_C/main.c

149 lines
3.6 KiB
C
Raw Permalink Normal View History

2019-10-31 15:18:38 +01:00
#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>
2019-10-31 16:35:28 +01:00
#include <pthread.h>
2019-11-03 13:23:45 +01:00
#include <fcntl.h>
2019-10-31 15:18:38 +01:00
#include "EasySockets.h"
#include "Game.h"
#include "LinkedList.h"
2019-10-31 16:35:28 +01:00
#include "Player.h"
bool wait =false;
void* WaitForInput(){
if (wait) {char c = getchar();}
wait=false;
}
int socket_id;
struct sockaddr_in incoming;
2019-11-02 18:41:21 +01:00
void InterpretReceivedSignal(Player* p, const char* buffer, int len, bool* fileSwitch){
printf("Interpreting: %s", buffer);
if (state==0){
wait=false;
}
else {
if (buffer[0] == 'u') {
p->y = p->y - 1;
} else if (buffer[0] == 'd') {
p->y = p->y + 1;
} else if (buffer[0] == 'l') {
p->x = p->x - 1;
} else if (buffer[0] == 'r') {
p->x = p->x + 1;
}else if (buffer[0] == 'f'){
*fileSwitch=true;
}
2019-11-03 13:23:45 +01:00
2019-11-02 18:41:21 +01:00
}
}
int series =0;
void UpdateStateToClients(){
for (int i =0; i<200; i++){
if (playerConnected[i]){
Player* p_receiver = players[i];
for (int j =0; j<200; j++){
if (playerConnected[j]) {
char* buffer[45];
Player* p = players[j];
sprintf(buffer, "%d,%d,%d,%d,%d\r\n",series, j, p->x, p->y, p->points);
printf(">> %s",buffer);
send(p_receiver->connectionId, buffer, strlen(buffer), 0);
}}
char end[8] = "end\r\n";
send(p_receiver->connectionId, end, strlen(end),0);
}
}
series++;
}
void* PlayerDataReaderThread(void* arg){
char buffer[550];
Player* p = (Player*)arg;
printf("%d", p->points);
socklen_t size = sizeof(p->incoming);
bool isFile=false;
2019-11-03 13:23:45 +01:00
FILE* fileHandle = NULL;
2019-11-02 18:41:21 +01:00
while (true) {
2019-11-03 13:23:45 +01:00
int s = recv(p->connectionId, buffer, 550, 0);
2019-11-02 18:41:21 +01:00
if (s>0) {
2019-11-03 13:23:45 +01:00
if (!isFile)
{InterpretReceivedSignal(p, buffer, 550,&isFile);
continue;}
2019-11-02 18:41:21 +01:00
if (isFile){
2019-11-03 13:23:45 +01:00
printf("is file \n");
if (fileHandle==NULL) fileHandle=fopen("recv", "w+");
2019-11-02 18:41:21 +01:00
//receiving the file in parts
2019-11-03 13:23:45 +01:00
printf("buffer content: %s\n", buffer);
fwrite(buffer,s,1,fileHandle);
if (s<550){
printf("ended receiving file...\n");
//if not data was received
fflush(fileHandle);
fclose(fileHandle);
isFile=false;
}
2019-11-02 18:41:21 +01:00
continue;
}
if (state >0)
UpdateStateToClients();
2019-11-03 13:23:45 +01:00
}else{
2019-11-02 18:41:21 +01:00
}
2019-11-03 13:23:45 +01:00
2019-11-02 18:41:21 +01:00
}
}
2019-10-31 16:35:28 +01:00
void* JoiningThread(){
while (true) {
int a = AcceptClient(socket_id, &incoming);
Player* p = CreatePlayer("tttt", a, incoming);
AddPlayerToList(p);
printf("Players: %d/200", GetPlayersCount());
2019-11-02 18:41:21 +01:00
pthread_create(&p->threadId, NULL, PlayerDataReaderThread, p);
2019-10-31 16:35:28 +01:00
}
return 0;
}
int BeginGame(){
2019-11-02 18:41:21 +01:00
state=1;
2019-10-31 16:35:28 +01:00
}
2019-10-31 15:18:38 +01:00
int main() {
2019-10-31 16:35:28 +01:00
InitPlayers();
2019-10-31 15:18:38 +01:00
setvbuf(stdout, NULL, _IONBF,0);
2019-10-31 16:35:28 +01:00
socket_id = CreateSocket();
2019-10-31 15:18:38 +01:00
//Connect(socket_id, "0.0.0.0", 12345);
int c = CreateServer(socket_id, 1234);
2019-11-02 18:41:21 +01:00
2019-10-31 15:18:38 +01:00
2019-10-31 16:35:28 +01:00
printf("Waiting for players to connect. \n Press any key to start...");
wait=true;
pthread_t thread_id;
2019-11-02 18:41:21 +01:00
2019-10-31 16:35:28 +01:00
pthread_create(&thread_id, NULL, JoiningThread, NULL);
2019-11-02 18:41:21 +01:00
wait=true;
while(wait){}
2019-10-31 16:35:28 +01:00
printf("...");
2019-11-02 18:41:21 +01:00
//pthread_exit(thread_id);
2019-10-31 16:35:28 +01:00
BeginGame();
2019-11-02 18:41:21 +01:00
printf("Press any key to close the server\n");
2019-10-31 16:35:28 +01:00
getchar();
2019-10-31 15:18:38 +01:00
return 0;
}