#include #include #include #include #include #include #include //FIXME: da togliere poi int _MSGQID; void msgRcv(void *msgp, size_t size, long mtype); void msgSnd(void *msgp, size_t size); void openMsgq(); void sndId(); void setupServerMsgq() { openMsgq(); sndId(); } void setupClientMsgq() { printf(PCLIENT "Setting up msgq\n"); key_t msgKey = ftok(KEYFILE, 'M'); _MSGQID = msgget(msgKey, IPC_CREAT | S_IRUSR | S_IWUSR); if (_MSGQID == -1) { errExit("msgget", "F4Client"); } } void openMsgq() { key_t msgKey = ftok(KEYFILE, 'M'); _MSGQID = msgget(msgKey, IPC_CREAT | S_IRUSR | S_IWUSR); if (_MSGQID == -1){ errExit("msgget", "F4Server"); } } void sndId() { msg_t msg = { .mtype = 1, .server_in = _INPUT_S, .player_id = 0 }; sndMsg(&msg); msg.player_id = 1; sndMsg(&msg); } void msgRcv(void *msgp, size_t size, long mtype) { int res; do { res = msgrcv(_MSGQID, msgp, size, mtype, 0); } while (errno == EINTR); if (res == -1) { errExit("msgrcv", "msgRcv"); //FIXME: mi ha dato un errore una volta quando ho chiuso prima i figli e fatto ctrl+c * 2, da controllare } } void rcvMsg(msg_t *msg) { msgRcv(msg, sizeof(msg_t) - sizeof(long), MSG); } void rcvMove(move_t *move) { msgRcv(move, sizeof(move_t) - sizeof(long), MOVE); } void rcvPlayer(player_ds *player) { msgRcv(player, sizeof(player_ds) - sizeof(long), PLAYER); } void rcvGame_end(game_end_t *game) { msgRcv(game, sizeof(game_end_t) - sizeof(long), GAME_END); } void msgSnd(void *msgp, size_t size) { int res; do { res = msgsnd(_MSGQID, msgp, size, 0); } while (errno == EINTR); if (res == -1) { errExitMsg("msgsnd"); } } void sndMsg(msg_t *msg) { msgSnd(msg, sizeof(msg_t) - sizeof(long)); } void sndMove(move_t *move) { msgSnd(move, sizeof(move_t) - sizeof(long)); } void sndPlayer(player_ds *player) { msgSnd(player, sizeof(player_ds) - sizeof(long)); } void sndGame_end(game_end_t *game) { msgSnd(game, sizeof(game_end_t) - sizeof(long)); }