custom_msg rcv and snd

This commit is contained in:
Lorenzo Bianchi 2023-05-19 14:48:00 +02:00
parent 89d37b28c7
commit 1de8a88b01
2 changed files with 96 additions and 0 deletions

50
inc/custom_msgq.h Normal file
View File

@ -0,0 +1,50 @@
#ifndef CUSTOM_MSG_H
#define CUSTOM_MSG_H
#include <server.h> //FIXME: non penso vada incluso ma serve al momento
#define MAX_NAME 16
static int _MSGQID;
#define MSG 1
#define MOVE 2
#define PLAYER 3
#define GAME_END 4
typedef struct {
long mtype; // type 1
int player_id;
input_server_t server_in;
} msg_t;
typedef struct {
long mtype; // type 2
int move;
} move_t;
typedef struct {
long mtype; // type 3
int id;
char name_player[MAX_NAME];
pid_t pid;
} player_ds;
typedef struct {
long mtype; // type 4
int winner; // -1 se draw
} game_end_t;
void rcvMsg(msg_t *msg);
void rcvMove(move_t *move);
void rcvPlayer(player_ds *player);
void rcvGame_end(game_end_t *game);
void sndMsg(msg_t *msg);
void sndMove(move_t *move);
void sndPlayer(player_ds *player);
void sndGame_end(game_end_t *game);
#endif

46
src/custom_msgq.c Normal file
View File

@ -0,0 +1,46 @@
#include <custom_msgq.h>
#include <sys/msg.h>
void msgRcv(void *msgp, size_t size, long mtype) {
if (msgrcv(_MSGQID, msgp, size, mtype, 0) == -1) {
errExitMsg("msgrcv");
}
}
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) {
if (msgsnd(_MSGQID, msgp, size, 0) == -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));
}