This commit is contained in:
emilekm2142 2019-11-02 18:41:21 +01:00
parent 146a549215
commit b5b395890f
7 changed files with 155 additions and 131 deletions

View File

@ -12,34 +12,12 @@
</component>
<component name="ChangeListManager">
<list default="true" id="97f792d3-056a-4080-95c1-9466c7ec0ade" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/Player.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/CMakeLists.txt" beforeDir="false" afterPath="$PROJECT_DIR$/CMakeLists.txt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/cmake-build-debug/CMakeCache.txt" beforeDir="false" afterPath="$PROJECT_DIR$/cmake-build-debug/CMakeCache.txt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/3.14.3/CMakeCCompiler.cmake" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/3.14.3/CMakeDetermineCompilerABI_C.bin" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/3.14.3/CMakeSystem.cmake" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/3.14.3/CompilerIdC/CMakeCCompilerId.c" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/3.14.3/CompilerIdC/a.out" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/CMakeDirectoryInformation.cmake" beforeDir="false" afterPath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/CMakeDirectoryInformation.cmake" afterDir="false" />
<change beforePath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/CMakeOutput.log" beforeDir="false" afterPath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/CMakeOutput.log" afterDir="false" />
<change beforePath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/Makefile.cmake" beforeDir="false" afterPath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/Makefile.cmake" afterDir="false" />
<change beforePath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/Makefile2" beforeDir="false" afterPath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/Makefile2" afterDir="false" />
<change beforePath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/ProjektDSIK.dir/C.includecache" beforeDir="false" afterPath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/ProjektDSIK.dir/C.includecache" afterDir="false" />
<change beforePath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/ProjektDSIK.dir/DependInfo.cmake" beforeDir="false" afterPath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/ProjektDSIK.dir/DependInfo.cmake" afterDir="false" />
<change beforePath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/ProjektDSIK.dir/build.make" beforeDir="false" afterPath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/ProjektDSIK.dir/build.make" afterDir="false" />
<change beforePath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/ProjektDSIK.dir/depend.internal" beforeDir="false" afterPath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/ProjektDSIK.dir/depend.internal" afterDir="false" />
<change beforePath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/ProjektDSIK.dir/depend.make" beforeDir="false" afterPath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/ProjektDSIK.dir/depend.make" afterDir="false" />
<change beforePath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/ProjektDSIK.dir/flags.make" beforeDir="false" afterPath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/ProjektDSIK.dir/flags.make" afterDir="false" />
<change beforePath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/ProjektDSIK.dir/link.txt" beforeDir="false" afterPath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/ProjektDSIK.dir/link.txt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/ProjektDSIK.dir/main.c.o" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/TargetDirectories.txt" beforeDir="false" afterPath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/TargetDirectories.txt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/EasySockets.h" beforeDir="false" afterPath="$PROJECT_DIR$/EasySockets.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game.h" beforeDir="false" afterPath="$PROJECT_DIR$/Game.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/ProjektDSIK.dir/main.c.o" beforeDir="false" afterPath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/ProjektDSIK.dir/main.c.o" afterDir="false" />
<change beforePath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/clion-log.txt" beforeDir="false" afterPath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/clion-log.txt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/feature_tests.bin" beforeDir="false" afterPath="$PROJECT_DIR$/cmake-build-debug/CMakeFiles/feature_tests.bin" afterDir="false" />
<change beforePath="$PROJECT_DIR$/cmake-build-debug/Makefile" beforeDir="false" afterPath="$PROJECT_DIR$/cmake-build-debug/Makefile" afterDir="false" />
<change beforePath="$PROJECT_DIR$/cmake-build-debug/ProjektDSIK" beforeDir="false" afterPath="$PROJECT_DIR$/cmake-build-debug/ProjektDSIK" afterDir="false" />
<change beforePath="$PROJECT_DIR$/cmake-build-debug/ProjektDSIK.cbp" beforeDir="false" afterPath="$PROJECT_DIR$/cmake-build-debug/ProjektDSIK.cbp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/cmake-build-debug/cmake_install.cmake" beforeDir="false" afterPath="$PROJECT_DIR$/cmake-build-debug/cmake_install.cmake" afterDir="false" />
<change beforePath="$PROJECT_DIR$/main.c" beforeDir="false" afterPath="$PROJECT_DIR$/main.c" afterDir="false" />
</list>
<ignored path="$PROJECT_DIR$/cmake-build-debug/" />
@ -59,7 +37,7 @@
<entry file="file://$PROJECT_DIR$/CMakeLists.txt">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="75">
<caret line="5" column="60" lean-forward="true" selection-start-line="5" selection-start-column="60" selection-end-line="5" selection-end-column="60" />
<caret line="5" column="60" selection-start-line="5" selection-start-column="60" selection-end-line="5" selection-end-column="60" />
</state>
</provider>
</entry>
@ -67,8 +45,8 @@
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/main.c">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="45">
<caret line="32" column="4" selection-start-line="32" selection-start-column="4" selection-end-line="32" selection-end-column="4" />
<state relative-caret-position="343">
<caret line="65" column="5" lean-forward="true" selection-start-line="65" selection-start-column="5" selection-end-line="65" selection-end-column="5" />
<folding>
<element signature="e#0#18#0" expanded="true" />
</folding>
@ -79,8 +57,8 @@
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/Player.h">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="270">
<caret line="18" column="23" selection-start-line="18" selection-start-column="23" selection-end-line="18" selection-end-column="23" />
<state relative-caret-position="870">
<caret line="58" selection-start-line="58" selection-end-line="58" />
<folding>
<element signature="e#99#118#0" expanded="true" />
</folding>
@ -91,8 +69,8 @@
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/Game.h">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="195">
<caret line="13" column="20" selection-start-line="13" selection-start-column="20" selection-end-line="13" selection-end-column="20" />
<state relative-caret-position="105">
<caret line="7" column="14" selection-start-line="7" selection-start-column="14" selection-end-line="7" selection-end-column="14" />
</state>
</provider>
</entry>
@ -112,8 +90,8 @@
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/EasySockets.h">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="199">
<caret line="51" selection-start-line="51" selection-end-line="51" />
<state relative-caret-position="270">
<caret line="18" column="30" selection-start-line="18" selection-start-column="30" selection-end-line="18" selection-end-column="30" />
<folding>
<element signature="e#110#128#0" expanded="true" />
</folding>
@ -123,24 +101,29 @@
</file>
</leaf>
</component>
<component name="FindInProjectRecents">
<findStrings>
<find>ttt</find>
</findStrings>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="IdeDocumentHistory">
<option name="CHANGED_PATHS">
<list>
<option value="$PROJECT_DIR$/EasySockets.h" />
<option value="$PROJECT_DIR$/LinkedList.h" />
<option value="$PROJECT_DIR$/Game.h" />
<option value="$PROJECT_DIR$/CMakeLists.txt" />
<option value="$PROJECT_DIR$/Player.h" />
<option value="$PROJECT_DIR$/EasySockets.h" />
<option value="$PROJECT_DIR$/Game.h" />
<option value="$PROJECT_DIR$/main.c" />
</list>
</option>
</component>
<component name="ProjectFrameBounds" extendedState="6">
<option name="y" value="32" />
<option name="width" value="950" />
<option name="width" value="790" />
<option name="height" value="868" />
</component>
<component name="ProjectView">
@ -148,6 +131,7 @@
<foldersAlwaysOnTop value="true" />
</navigator>
<panes>
<pane id="Scope" />
<pane id="ProjectPane">
<subPane>
<expand>
@ -159,7 +143,6 @@
<select />
</subPane>
</pane>
<pane id="Scope" />
</panes>
</component>
<component name="PropertiesComponent">
@ -198,24 +181,24 @@
<updated>1572428270200</updated>
<workItem from="1572428271418" duration="2763000" />
<workItem from="1572532173908" duration="3927000" />
<workItem from="1572542490103" duration="11691000" />
<workItem from="1572708950669" duration="1505000" />
</task>
<servers />
</component>
<component name="TimeTrackingManager">
<option name="totallyTimeSpent" value="6690000" />
<option name="totallyTimeSpent" value="19886000" />
</component>
<component name="ToolWindowManager">
<frame x="0" y="31" width="1600" height="869" extended-state="6" />
<editor active="true" />
<layout>
<window_info content_ui="combo" id="Project" order="0" visible="true" weight="0.14890885" />
<window_info content_ui="combo" id="Project" order="0" visible="true" weight="0.15275995" />
<window_info id="Structure" order="1" side_tool="true" weight="0.25" />
<window_info id="Favorites" order="2" side_tool="true" />
<window_info anchor="bottom" id="Arduino Support" />
<window_info anchor="bottom" id="Serial Monitor" />
<window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" />
<window_info active="true" anchor="bottom" id="Run" order="2" visible="true" weight="0.32791328" />
<window_info anchor="bottom" id="Run" order="2" weight="0.32791328" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.398374" />
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
@ -226,6 +209,8 @@
<window_info anchor="bottom" id="Event Log" order="10" side_tool="true" />
<window_info anchor="bottom" id="Version Control" order="11" />
<window_info anchor="bottom" id="CMake" order="12" weight="0.3292683" />
<window_info anchor="bottom" id="Serial Monitor" order="13" />
<window_info anchor="bottom" id="Arduino Support" order="14" />
<window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" />
<window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
<window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" />
@ -240,27 +225,48 @@
<breakpoints>
<line-breakpoint enabled="true" type="com.jetbrains.cidr.execution.debugger.OCBreakpointType">
<url>file://$PROJECT_DIR$/main.c</url>
<line>74</line>
<option name="timeStamp" value="3" />
</line-breakpoint>
<line-breakpoint enabled="true" type="com.jetbrains.cidr.execution.debugger.OCBreakpointType">
<url>file://$PROJECT_DIR$/main.c</url>
<line>48</line>
<option name="timeStamp" value="4" />
</line-breakpoint>
<line-breakpoint enabled="true" type="com.jetbrains.cidr.execution.debugger.OCBreakpointType">
<url>file://$PROJECT_DIR$/main.c</url>
<line>37</line>
<option name="timeStamp" value="5" />
<line>77</line>
<option name="timeStamp" value="17" />
</line-breakpoint>
</breakpoints>
</breakpoint-manager>
<watches-manager>
<configuration name="CMakeRunConfiguration">
<watch expression="players" language="ObjectiveC" />
</configuration>
</watches-manager>
</component>
<component name="debuggerHistoryManager">
<expressions id="watch">
<expression>
<expression-string>players</expression-string>
<language-id>ObjectiveC</language-id>
<evaluation-mode>EXPRESSION</evaluation-mode>
</expression>
</expressions>
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/CMakeLists.txt">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="75">
<caret line="5" column="60" selection-start-line="5" selection-start-column="60" selection-end-line="5" selection-end-column="60" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Player.h">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="870">
<caret line="58" selection-start-line="58" selection-end-line="58" />
<folding>
<element signature="e#99#118#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Game.h">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="195">
<caret line="13" column="20" selection-start-line="13" selection-start-column="20" selection-end-line="13" selection-end-column="20" />
<state relative-caret-position="105">
<caret line="7" column="14" selection-start-line="7" selection-start-column="14" selection-end-line="7" selection-end-column="14" />
</state>
</provider>
</entry>
@ -274,27 +280,10 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/CMakeLists.txt">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="75">
<caret line="5" column="60" lean-forward="true" selection-start-line="5" selection-start-column="60" selection-end-line="5" selection-end-column="60" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Player.h">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="270">
<caret line="18" column="23" selection-start-line="18" selection-start-column="23" selection-end-line="18" selection-end-column="23" />
<folding>
<element signature="e#99#118#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/EasySockets.h">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="199">
<caret line="51" selection-start-line="51" selection-end-line="51" />
<state relative-caret-position="270">
<caret line="18" column="30" selection-start-line="18" selection-start-column="30" selection-end-line="18" selection-end-column="30" />
<folding>
<element signature="e#110#128#0" expanded="true" />
</folding>
@ -303,8 +292,8 @@
</entry>
<entry file="file://$PROJECT_DIR$/main.c">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="45">
<caret line="32" column="4" selection-start-line="32" selection-start-column="4" selection-end-line="32" selection-end-column="4" />
<state relative-caret-position="343">
<caret line="65" column="5" lean-forward="true" selection-start-line="65" selection-start-column="5" selection-end-line="65" selection-end-column="5" />
<folding>
<element signature="e#0#18#0" expanded="true" />
</folding>

View File

@ -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);

15
Game.h
View File

@ -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

View File

@ -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

Binary file not shown.

98
main.c
View File

@ -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;