-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemClient.c
85 lines (73 loc) · 2.24 KB
/
MemClient.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "MemClient.h"
MemClient* memc_open()
{
MemClient* ret = (MemClient*)malloc(sizeof(MemClient));
/* attach semaphore */
ret->socket_lock = sem_open(DEFAULT_SERVER_SEMAPHORE_NAME, O_CREAT /* only open if it exists*/, 0644, 1);
if (ret->socket_lock == SEM_FAILED) {
perror("sem_open");
exit(-1);
}
/* create shared memory */
ret->shm_id = shmget(ftok(DEFAULT_CONNECTION_SHM, 0), DEFAULT_CONNECTION_BUFFER_SIZE, 0666); // create shared memory
/* check if shared memory was created successfully */
if (ret->shm_id <0)
{
perror("Check if server is running, Error creating shared memory");
_Exit(-1);
}
/* attach shared memory to the process's address space */
ret->socket = shmat(ret->shm_id, (void*) 0, 0);
if (ret->socket < (char*) 0)
{
perror("Error attaching shared memory");
return 1;
}
ret->socket+=3;
if(memcon_getState(ret) == MCON_UNVAIL)
{
printf("Error: Server not running.\n");
if (shmdt(ret->socket) <0)
perror("Error detaching shared memory");
/* mark shared memory for removal */
if (shmctl(ret->shm_id, IPC_RMID, NULL) <0)
perror("Error removing shared memory");
free(ret);
_Exit(-1);
}
return ret;
}
void memc_send(MemClient* client, void* data, _nmap_size size)
{
// printf("MemClient.c:50 wait\n");
memcon_awaitAndSetState(MCON_EMPTY, MCON_TRANSFER, client);
memcpy(client->socket, data, size);
memcon_updateState(MCON_WAITING, client);
sem_post(client->socket_lock);
// printf("MemClient.c:56 wait\n");
memcon_awaitState(MCON_RESPONSED, client);
// printf("MemClient.c:58 done\n");
}
extern inline void memc_cleanbuf(MemClient* client)
{
memset(client->socket, 0, DEFAULT_CONNECTION_BUFFER_SIZE);
}
extern inline void memc_accept(MemClient* client)
{
memcon_updateState(MCON_EMPTY, client);
}
void memc_close(MemClient* client)
{
if(sem_close(client->socket_lock) < 0)
{
perror("Error closing socket lock");
_Exit(-1);
}
/* detach shared memory */
if (shmdt(client->socket - 3) <0)
{
perror("Error detaching shared memory");
_Exit(-1);
}
free(client);
}