custom_msg.h
This commit is contained in:
parent
fb864683bc
commit
9f4df5f1aa
|
@ -0,0 +1,33 @@
|
|||
#ifndef CUSTOM_MSG_H
|
||||
#define CUSTOM_MSG_H
|
||||
|
||||
#include <server.h>
|
||||
|
||||
#define MAX_NAME 16
|
||||
|
||||
static int _MSGQID;
|
||||
|
||||
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;
|
||||
|
||||
#endif
|
|
@ -31,30 +31,4 @@ typedef enum {
|
|||
#define DEFAULT_SIGINT 2
|
||||
#define TIME_TO_RESET 10
|
||||
|
||||
//message queue
|
||||
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;
|
||||
|
||||
#define MAX_NAME 16
|
||||
|
||||
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;
|
||||
|
||||
#endif
|
|
@ -15,8 +15,8 @@
|
|||
#include <custom_shm.h>
|
||||
#include <errExit.h>
|
||||
#include <nonsodovemetterle.h>
|
||||
#include <custom_msg.h>
|
||||
|
||||
int msgid;
|
||||
int game_state;
|
||||
|
||||
tile_t *boardClient = NULL;
|
||||
|
@ -50,7 +50,7 @@ void sigHandlerClient(int sig) {
|
|||
// Fine partita
|
||||
if (sig == SIGUSR1) {
|
||||
game_end_t winner;
|
||||
if (msgrcv(msgid, &winner, sizeof(game_end_t) - sizeof(long), 4, 0) == -1) {
|
||||
if (msgrcv(_MSGQID, &winner, sizeof(game_end_t) - sizeof(long), 4, 0) == -1) {
|
||||
errExit("msgrcv", "fine partita");
|
||||
}
|
||||
game_state = winner.winner;
|
||||
|
@ -90,8 +90,8 @@ int main(int argc, char *argv[]){
|
|||
// MSGQ
|
||||
printfClient("Setting up msgq\n");
|
||||
key_t msgKey = ftok(KEYFILE, 'M');
|
||||
msgid = msgget(msgKey, IPC_CREAT | S_IRUSR | S_IWUSR);
|
||||
if (msgid == -1) {
|
||||
_MSGQID = msgget(msgKey, IPC_CREAT | S_IRUSR | S_IWUSR);
|
||||
if (_MSGQID == -1) {
|
||||
errExit("msgget", "F4Client");
|
||||
}
|
||||
|
||||
|
@ -101,7 +101,7 @@ int main(int argc, char *argv[]){
|
|||
|
||||
// msgrcv
|
||||
printfClient("Waiting for message...\n");
|
||||
if (msgrcv(msgid, &msg, sizeof(msg_t) - sizeof(long), 1, 0) == -1) {
|
||||
if (msgrcv(_MSGQID, &msg, sizeof(msg_t) - sizeof(long), 1, 0) == -1) {
|
||||
errExit("msgrcv", "F4Client");
|
||||
}
|
||||
|
||||
|
@ -135,7 +135,7 @@ int main(int argc, char *argv[]){
|
|||
// mando i miei dati al server
|
||||
player_ds player = {.mtype = 3, .id = msg.player_id, .pid = getpid()};
|
||||
strcpy(player.name_player, argv[1]);
|
||||
if (msgsnd(msgid, &player, sizeof(player_ds) - sizeof(long), 0) == -1) {
|
||||
if (msgsnd(_MSGQID, &player, sizeof(player_ds) - sizeof(long), 0) == -1) {
|
||||
errExit("msgsnd", "mandare nome");
|
||||
}
|
||||
|
||||
|
@ -165,7 +165,7 @@ int main(int argc, char *argv[]){
|
|||
|
||||
// mando mossa al server
|
||||
move.move = current_move;
|
||||
if (msgsnd(msgid, &move, sizeof(move_t) - sizeof(long), 0) == -1) {
|
||||
if (msgsnd(_MSGQID, &move, sizeof(move_t) - sizeof(long), 0) == -1) {
|
||||
errExit("msgsnd", "partita_client");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,9 +16,10 @@
|
|||
#include <errExit.h>
|
||||
#include <forza4.h>
|
||||
#include <nonsodovemetterle.h>
|
||||
#include <custom_msg.h>
|
||||
|
||||
int semid = 0;
|
||||
int msgid = 0;
|
||||
int _MSGQID = 0;
|
||||
int shmid = 0;
|
||||
|
||||
tile_t *board = NULL;
|
||||
|
@ -45,10 +46,10 @@ void sigHandlerServer(int sig) {
|
|||
}
|
||||
|
||||
// msgq
|
||||
if (msgid) {
|
||||
if (_MSGQID) {
|
||||
printf("\n");
|
||||
printfServer("Deleting msg");
|
||||
if (msgctl(msgid, IPC_RMID, NULL) == -1) {
|
||||
if (msgctl(_MSGQID, IPC_RMID, NULL) == -1) {
|
||||
errExit("msgctl", "sigHandlerServer");
|
||||
}
|
||||
}
|
||||
|
@ -113,8 +114,8 @@ int main(int argc, char *argv[]){
|
|||
//TODO: forse bisogna fare il reset della shm, perche in teoria mette a 0 quando crea
|
||||
//ma nel caso in cui il campo non si fosse chiuso per qualche motivo forse quando lo riapre non lo azzera
|
||||
key_t msgKey = ftok(KEYFILE, 'M');
|
||||
msgid = msgget(msgKey, IPC_CREAT | S_IRUSR | S_IWUSR);
|
||||
if (msgid == -1){
|
||||
_MSGQID = msgget(msgKey, IPC_CREAT | S_IRUSR | S_IWUSR);
|
||||
if (_MSGQID == -1){
|
||||
errExit("msgget", "F4Server");
|
||||
}
|
||||
// msg
|
||||
|
@ -125,12 +126,12 @@ int main(int argc, char *argv[]){
|
|||
};
|
||||
|
||||
// msgsnd
|
||||
if (msgsnd(msgid, &msg, sizeof(msg_t) - sizeof(long), 0) == -1) {
|
||||
if (msgsnd(_MSGQID, &msg, sizeof(msg_t) - sizeof(long), 0) == -1) {
|
||||
errExit("msgsnd1", "F4Server");
|
||||
}
|
||||
|
||||
msg.player_id = 1;
|
||||
if (msgsnd(msgid, &msg, sizeof(msg_t) - sizeof(long), 0) == -1) {
|
||||
if (msgsnd(_MSGQID, &msg, sizeof(msg_t) - sizeof(long), 0) == -1) {
|
||||
errExit("msgsnd2", "F4Server");
|
||||
}
|
||||
|
||||
|
@ -169,7 +170,7 @@ int main(int argc, char *argv[]){
|
|||
pids[2];
|
||||
|
||||
// ricevuta nome primo client
|
||||
if (msgrcv(msgid, &player, sizeof(player_ds) - sizeof(long), 3, 0) == -1) {
|
||||
if (msgrcv(_MSGQID, &player, sizeof(player_ds) - sizeof(long), 3, 0) == -1) {
|
||||
errExit("msgrcv", "ricevuta primo nome");
|
||||
}
|
||||
|
||||
|
@ -182,7 +183,7 @@ int main(int argc, char *argv[]){
|
|||
// aspetto secondo client
|
||||
semOp(semid, 2, -1);
|
||||
// ricevuta nome secondo client
|
||||
if (msgrcv(msgid, &player, sizeof(player_ds) - sizeof(long), 3, 0) == -1) {
|
||||
if (msgrcv(_MSGQID, &player, sizeof(player_ds) - sizeof(long), 3, 0) == -1) {
|
||||
errExit("msgrcv", "ricevuta secondo nome");
|
||||
}
|
||||
strcpy(name[player.id], player.name_player);
|
||||
|
@ -212,7 +213,7 @@ int main(int argc, char *argv[]){
|
|||
printfServer("");
|
||||
printf("%s's turn\n", name[turn]);
|
||||
|
||||
if (msgrcv(msgid, &move, sizeof(move_t) - sizeof(long), 2, 0) == -1) {
|
||||
if (msgrcv(_MSGQID, &move, sizeof(move_t) - sizeof(long), 2, 0) == -1) {
|
||||
errExit("msgrcv", "F4Client");
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
#include <custom_msg.h>
|
||||
|
||||
void setMsgqid(int id) {
|
||||
_MSGQID = id;
|
||||
}
|
||||
|
||||
void rcvMsg(msg_t *msg) {
|
||||
if (msgrcv(_MSGQID, msg, sizeof(msg_t) - sizeof(long), 1, 0) == -1) {
|
||||
errExitMsg("msgrcv");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue