diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 73fcbea..b666903 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -12,34 +12,12 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
-
-
-
-
@@ -59,7 +37,7 @@
-
+
@@ -67,8 +45,8 @@
-
-
+
+
@@ -79,8 +57,8 @@
-
-
+
+
@@ -91,8 +69,8 @@
-
-
+
+
@@ -112,8 +90,8 @@
-
-
+
+
@@ -123,24 +101,29 @@
+
+
+ ttt
+
+
-
+
@@ -148,6 +131,7 @@
+
@@ -159,7 +143,6 @@
-
@@ -198,24 +181,24 @@
1572428270200
+
+
-
+
-
+
-
-
-
+
@@ -226,6 +209,8 @@
+
+
@@ -240,27 +225,48 @@
file://$PROJECT_DIR$/main.c
- 74
-
-
-
- file://$PROJECT_DIR$/main.c
- 48
-
-
-
- file://$PROJECT_DIR$/main.c
- 37
-
+ 77
+
+
+
+
+
+
+
+
+
+
+ players
+ ObjectiveC
+ EXPRESSION
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
@@ -274,27 +280,10 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
@@ -303,8 +292,8 @@
-
-
+
+
diff --git a/EasySockets.h b/EasySockets.h
index f1af99f..c865eef 100644
--- a/EasySockets.h
+++ b/EasySockets.h
@@ -32,23 +32,24 @@ int Connect(const int socket, const char* ip, const int port){
if (connectionStatus<0) return connectionStatus;
return socket;
}
+struct sockaddr_in endpoint;
int CreateServer(const int socket, const int port){
char myhostname[1024];
- gethostname(myhostname, 1023);
+ gethostname(myhostname, 1024);
struct hostent * heLocalHost = malloc(sizeof(struct hostent *));
heLocalHost = gethostbyname(myhostname);
- struct sockaddr_in* endpoint = malloc(sizeof(struct sockaddr_in *));
- endpoint->sin_family = AF_INET;
- endpoint->sin_port = htons(port);
- endpoint->sin_addr = *(struct in_addr*) heLocalHost->h_addr;
+ endpoint.sin_family = AF_INET;
+ endpoint.sin_port = htons(port);
+ endpoint.sin_addr.s_addr = INADDR_ANY;
int retval = bind(socket,
- (struct sockaddr*)endpoint,
+ (struct sockaddr*)&endpoint,
sizeof(struct sockaddr));
- listen(socket, 10);
- return 1;
+ int listen_val = listen(socket, 10);
+ return retval;
}
int AcceptClient(const int socket, struct sockaddr_in* incoming){
socklen_t sin_size;
+ printf("Waiting to accept...");
return accept(socket,
(struct sockaddr*) &incoming,
&sin_size);
diff --git a/Game.h b/Game.h
index 055d74a..b9ee17a 100644
--- a/Game.h
+++ b/Game.h
@@ -5,18 +5,5 @@
#ifndef PROJEKTDSIK_GAME_H
#define PROJEKTDSIK_GAME_H
#include "LinkedList.h"
-
-struct Game{
- const char* name;
- int id;
-} typedef Game;
-Game* games[100];
-int lastGameIndex=0;
-Game* CreateNewGame(){
- Game* g = malloc(sizeof(Game));
- games[lastGameIndex] = g;
- lastGameIndex++;
- // push(&list, g, sizeof(Game*));
- return g;
-}
+int state = 0;
#endif //PROJEKTDSIK_GAME_H
diff --git a/cmake-build-debug/CMakeFiles/ProjektDSIK.dir/main.c.o b/cmake-build-debug/CMakeFiles/ProjektDSIK.dir/main.c.o
index 23680f6..440524e 100644
Binary files a/cmake-build-debug/CMakeFiles/ProjektDSIK.dir/main.c.o and b/cmake-build-debug/CMakeFiles/ProjektDSIK.dir/main.c.o differ
diff --git a/cmake-build-debug/CMakeFiles/clion-log.txt b/cmake-build-debug/CMakeFiles/clion-log.txt
index 2ac7be6..ce7b4d1 100644
--- a/cmake-build-debug/CMakeFiles/clion-log.txt
+++ b/cmake-build-debug/CMakeFiles/clion-log.txt
@@ -1,9 +1,4 @@
/home/emile/Downloads/clion-2018.3.3/bin/cmake/linux/bin/cmake -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - Unix Makefiles" /home/emile/DSIK_C
-CMake Error at CMakeLists.txt:5 (target_link_libraries):
- Cannot specify link libraries for target "pthread" which is not built by
- this project.
-
-
--- Configuring incomplete, errors occurred!
-See also "/home/emile/DSIK_C/cmake-build-debug/CMakeFiles/CMakeOutput.log".
-See also "/home/emile/DSIK_C/cmake-build-debug/CMakeFiles/CMakeError.log".
+-- Configuring done
+-- Generating done
+-- Build files have been written to: /home/emile/DSIK_C/cmake-build-debug
diff --git a/cmake-build-debug/ProjektDSIK b/cmake-build-debug/ProjektDSIK
index bbb2d1f..f118acf 100755
Binary files a/cmake-build-debug/ProjektDSIK and b/cmake-build-debug/ProjektDSIK differ
diff --git a/main.c b/main.c
index d3f26f3..45b2030 100644
--- a/main.c
+++ b/main.c
@@ -20,35 +20,86 @@ void* WaitForInput(){
}
int socket_id;
struct sockaddr_in incoming;
+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;
+ }
+ }
+}
+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;
+ while (true) {
+ int s = recv(p->connectionId, buffer, 1000, 0);
+ if (s>0) {
+ InterpretReceivedSignal(p, buffer, 1000,&isFile);
+ if (isFile){
+ //receiving the file in parts
+ continue;
+ }
+ if (state >0)
+ UpdateStateToClients();
+ }
+ }
+
+}
void* JoiningThread(){
while (true) {
int a = AcceptClient(socket_id, &incoming);
Player* p = CreatePlayer("tttt", a, incoming);
AddPlayerToList(p);
printf("Players: %d/200", GetPlayersCount());
+ pthread_create(&p->threadId, NULL, PlayerDataReaderThread, p);
}
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);
- }
- }
+ state=1;
}
@@ -58,19 +109,20 @@ int main() {
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();
+ wait=true;
+ while(wait){}
printf("...");
- Player* p = CreatePlayer("test", 15, incoming);
- AddPlayerToList(p);
- pthread_cancel(thread_id);
- printf("Press any key to close the server");
+ //pthread_exit(thread_id);
BeginGame();
+ printf("Press any key to close the server\n");
+
getchar();
return 0;