project_files/frontlib/frontlib.c
changeset 7171 906e72caea7b
parent 7162 fe76d24a25d7
child 7173 7c2eb284f9f1
equal deleted inserted replaced
7169:b66eef8c8092 7171:906e72caea7b
     1 #include "frontlib.h"
     1 #include "frontlib.h"
     2 #include "logging.h"
     2 #include "logging.h"
     3 #include "nonblocksockets.h"
     3 #include "socket.h"
     4 #include "ipcconn.h"
     4 #include "ipcconn.h"
     5 
     5 
     6 #include <SDL.h>
     6 #include <SDL.h>
     7 #include <SDL_net.h>
     7 #include <SDL_net.h>
     8 #include <stdio.h>
     8 #include <stdio.h>
    27 			SDL_Quit();
    27 			SDL_Quit();
    28 		}
    28 		}
    29 		return -1;
    29 		return -1;
    30 	}
    30 	}
    31 
    31 
    32 	flib_ipcconn_init();
       
    33 	return 0;
    32 	return 0;
    34 }
    33 }
    35 
    34 
    36 void flib_quit() {
    35 void flib_quit() {
    37 	flib_ipcconn_quit();
       
    38 
       
    39 	SDLNet_Quit();
    36 	SDLNet_Quit();
    40 	if(!(flib_initflags | FRONTLIB_SDL_ALREADY_INITIALIZED)) {
    37 	if(!(flib_initflags | FRONTLIB_SDL_ALREADY_INITIALIZED)) {
    41 		SDL_Quit();
    38 		SDL_Quit();
    42 	}
    39 	}
    43 }
    40 }
    44 
    41 
    45 int main(int argc, char *argv[]) {
    42 int main(int argc, char *argv[]) {
    46 	flib_init(FRONTLIB_SDL_ALREADY_INITIALIZED);
    43 	flib_init(0);
    47 	flib_ipcconn_start(true);
    44 
       
    45 	flib_ipcconn ipc = flib_ipcconn_create(true, "Medo42");
    48 	char data[256];
    46 	char data[256];
    49 	while(flib_ipcconn_state() != IPC_NOT_CONNECTED) {
    47 	while(flib_ipcconn_state(ipc) != IPC_NOT_CONNECTED) {
    50 		flib_ipcconn_tick();
    48 		flib_ipcconn_tick(ipc);
    51 		int size = flib_ipcconn_recv_message(data);
    49 		int size = flib_ipcconn_recv_message(ipc, data);
    52 		if(size>0) {
    50 		if(size>0) {
    53 			data[size]=0;
    51 			data[size]=0;
    54 			flib_log_i("IPC IN: %s", data);
       
    55 			switch(data[0]) {
    52 			switch(data[0]) {
    56 			case 'C':
    53 			case 'C':
    57 				flib_log_i("Sending config...");
    54 				flib_log_i("Sending config...");
    58 				flib_ipcconn_send_messagestr("TL");
    55 				flib_ipcconn_send_messagestr(ipc, "TL");
    59 				flib_ipcconn_send_messagestr("eseed loremipsum");
    56 				flib_ipcconn_send_messagestr(ipc, "eseed loremipsum");
    60 				flib_ipcconn_send_messagestr("escript Missions/Training/Basic_Training_-_Bazooka.lua");
    57 				flib_ipcconn_send_messagestr(ipc, "escript Missions/Training/Basic_Training_-_Bazooka.lua");
    61 				break;
    58 				break;
    62 			case '?':
    59 			case '?':
    63 				flib_log_i("Sending pong...");
    60 				flib_log_i("Sending pong...");
    64 				flib_ipcconn_send_messagestr("!");
    61 				flib_ipcconn_send_messagestr(ipc, "!");
    65 				break;
    62 				break;
    66 			case 'Q':
    63 			case 'Q':
    67 				flib_log_i("Game interrupted.");
    64 				flib_log_i("Game interrupted.");
    68 				break;
    65 				break;
    69 			case 'q':
    66 			case 'q':
    70 				flib_log_i("Game finished.");
    67 				flib_log_i("Game finished.");
    71 				flib_constbuffer demobuf = flib_ipcconn_getdemo();
    68 				flib_constbuffer demobuf = flib_ipcconn_getdemo(ipc);
    72 				flib_log_i("Writing demo (%u bytes)...", demobuf.size);
    69 				flib_log_i("Writing demo (%u bytes)...", demobuf.size);
    73 				FILE *file = fopen("testdemo.dem", "w");
    70 				FILE *file = fopen("testdemo.dem", "wb");
    74 				fwrite(demobuf.data, 1, demobuf.size, file);
    71 				fwrite(demobuf.data, 1, demobuf.size, file);
    75 				fclose(file);
    72 				fclose(file);
    76 				file = NULL;
    73 				file = NULL;
    77 				break;
    74 				break;
    78 			case 'H':
    75 			case 'H':
    80 				break;
    77 				break;
    81 			}
    78 			}
    82 		}
    79 		}
    83 	}
    80 	}
    84 	flib_log_i("IPC connection lost.");
    81 	flib_log_i("IPC connection lost.");
       
    82 	flib_ipcconn_destroy(&ipc);
    85 	flib_quit();
    83 	flib_quit();
    86 	return 0;
    84 	return 0;
    87 }
    85 }