Elaborato_SO/src/custom_msgq.c

118 lines
2.2 KiB
C
Raw Permalink Normal View History

2023-05-19 14:48:00 +02:00
#include <sys/msg.h>
#include <errno.h>
2023-05-22 10:07:57 +02:00
#include <sys/stat.h>
#include <stdio.h>
2023-05-31 11:52:32 +02:00
#include <unistd.h>
2023-05-19 14:48:00 +02:00
2023-05-19 15:21:15 +02:00
#include <custom_msgq.h>
#include <errExit.h>
2023-06-18 15:54:51 +02:00
#include <server.h>
2023-05-29 13:06:22 +02:00
#include <client.h>
#include <forza4.h>
2023-05-23 15:47:34 +02:00
int _MSGQID;
2023-05-19 15:21:15 +02:00
2023-05-22 10:27:46 +02:00
void msgRcv(void *msgp, size_t size, long mtype);
void msgSnd(void *msgp, size_t size);
void openMsgq();
void sndId();
2023-05-29 13:06:22 +02:00
void rcvId();
2023-05-22 10:27:46 +02:00
void setupServerMsgq() {
openMsgq();
sndId();
}
2023-05-29 12:35:04 +02:00
void setupClientMsgq() {
2023-05-29 13:06:22 +02:00
openMsgq();
rcvId();
2023-05-29 12:35:04 +02:00
}
2023-05-22 10:07:57 +02:00
void openMsgq() {
2023-05-29 15:01:11 +02:00
key_t msgKey = ftok(KEYFILE, MSGQKEY);
2023-05-22 10:07:57 +02:00
_MSGQID = msgget(msgKey, IPC_CREAT | S_IRUSR | S_IWUSR);
if (_MSGQID == -1){
2023-05-29 13:06:22 +02:00
errExitMsg("msgget");
2023-05-22 10:07:57 +02:00
}
}
2023-05-22 10:27:46 +02:00
void sndId() {
msg_t msg = {
.mtype = 1,
.server_in = _INPUT_S,
2023-05-31 11:52:32 +02:00
.player_id = 0,
.server_pid = getpid()
2023-05-22 10:27:46 +02:00
};
sndMsg(&msg);
msg.player_id = 1;
sndMsg(&msg);
}
2023-05-29 13:06:22 +02:00
void rcvId() {
printf(PCLIENT "Waiting for message...\n");
2023-05-29 16:14:01 +02:00
msg_t msg;
2023-05-29 13:06:22 +02:00
rcvMsg(&msg);
setRows(msg.server_in.rows);
setCollums(msg.server_in.collums);
setTokens(msg.server_in.player1Token, msg.server_in.player2Token);
2023-05-29 16:14:01 +02:00
ID = msg.player_id;
2023-05-31 11:52:32 +02:00
SERVER_PID = msg.server_pid;
2023-05-29 13:06:22 +02:00
}
2023-05-22 10:07:57 +02:00
2023-05-19 14:48:00 +02:00
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) {
2023-06-18 15:54:51 +02:00
errExit("msgrcv", "msgRcv");
2023-05-19 14:48:00 +02:00
}
}
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) {
2023-05-19 14:48:00 +02:00
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));
}