feat[OS/lab5]: completed part B
This commit is contained in:
45
OSs/lab5/part-B/common.h
Normal file
45
OSs/lab5/part-B/common.h
Normal file
@ -0,0 +1,45 @@
|
||||
#ifndef COMMON_H_
|
||||
#define COMMON_H_
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define FIFO_SERVER_PATH "/tmp/fifo.to_server" // server reads this file
|
||||
#define FIFO_CLIENT_PATH "/tmp/fifo.to_client" // client reads this file
|
||||
|
||||
#define __DATA_STRUCT_MSG_LEN 4096
|
||||
|
||||
enum ds_types {
|
||||
DS_MESSAGE,
|
||||
DS_FAIL
|
||||
};
|
||||
|
||||
struct custom_ds {
|
||||
struct {
|
||||
long len;
|
||||
long type;
|
||||
} hdr;
|
||||
char msg[__DATA_STRUCT_MSG_LEN];
|
||||
};
|
||||
|
||||
static inline ssize_t send_msg(int fd, const struct custom_ds *ds)
|
||||
{
|
||||
return write(fd, ds, sizeof(ds->hdr) + ds->hdr.len);
|
||||
}
|
||||
|
||||
static inline ssize_t recv_msg(int fd, struct custom_ds *reciever)
|
||||
{
|
||||
size_t n_hdr = read(fd, reciever, sizeof(reciever->hdr));
|
||||
if (n_hdr == 0) return 0; // eof reached
|
||||
if (n_hdr != sizeof(reciever->hdr)) return -ENOMSG;
|
||||
if (reciever->hdr.len == 0) return 0;
|
||||
|
||||
size_t n_payload = read(fd, &reciever->msg, reciever->hdr.len);
|
||||
if (n_payload != reciever->hdr.len) return -EFAULT;
|
||||
return n_hdr + n_payload;
|
||||
}
|
||||
|
||||
#define FAIL_ON(cond) if (cond) return EXIT_FAILURE
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user