-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.cpp
More file actions
64 lines (56 loc) · 1.27 KB
/
util.cpp
File metadata and controls
64 lines (56 loc) · 1.27 KB
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
#include "util.hpp"
Datagram createMessage(int type, std::string value){
Datagram datagram;
zerosDatagram(&datagram);
datagram.type = type;
int size = value.size();
datagram.size = size;
memcpy((void *) &datagram.data,
(void *) value.c_str(),
size
);
return datagram;
}
int generatePort(){
static int port = 30000;
return port++;
}
Semaphore::Semaphore() {
if (initialized) {
sem_destroy(&this->c_sem);
}
sem_init(&c_sem, 0, 0);
initialized = true;
}
void Semaphore::init(int initValue) {
if (initialized) {
sem_destroy(&this->c_sem);
}
sem_init(&c_sem, 0, initValue);
initialized = true;
}
Semaphore::~Semaphore(void) {
int status = sem_destroy(&this->c_sem);
if (status) {
std::cout << "[Error] Could not destroy semaphore." << std::endl;
} else {
this->initialized = false;
}
}
int Semaphore::wait() {
int status = sem_wait(&this->c_sem);
if (status) {
std::cout << "[Error] Could not wait semaphore." << std::endl;
}
return status;
}
int Semaphore::post() {
int status = sem_post(&this->c_sem);
if (status) {
std::cout << "[Error] Could not post semaphore." << std::endl;
}
return status;
}
void vlog(const char* text){
std::cout << text << std::endl;
}