forked from treefrogframework/treefrog-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtfcore.h
155 lines (130 loc) · 2.95 KB
/
tfcore.h
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#ifndef TFCORE_H
#define TFCORE_H
#include <QtGlobal>
#include <cstdio>
#include <cerrno>
#ifdef Q_OS_WIN
# include <windows.h>
# include <io.h>
# include <winbase.h>
#else
# include <unistd.h>
# include <fcntl.h>
# include <sys/file.h>
# include <sys/socket.h>
#endif
#define TF_EINTR_LOOP(func) \
int ret; \
do { \
errno = 0; \
ret = (func); \
} while (ret == -1 && errno == EINTR); \
return ret;
static inline int tf_close(int fd)
{
#ifdef Q_OS_WIN
return ::_close(fd);
#else
TF_EINTR_LOOP(::close(fd));
#endif
}
static inline int tf_read(int fd, void *buf, size_t count)
{
#ifdef Q_OS_WIN
return ::_read(fd, buf, (uint)count);
#else
TF_EINTR_LOOP(::read(fd, buf, count));
#endif
}
static inline int tf_write(int fd, const void *buf, size_t count)
{
#ifdef Q_OS_WIN
return ::_write(fd, buf, (uint)count);
#else
TF_EINTR_LOOP(::write(fd, buf, count));
#endif
}
static inline int tf_send(int sockfd, const void *buf, size_t len, int flags)
{
#ifdef Q_OS_WIN
Q_ASSERT(0);
Q_UNUSED(sockfd);
Q_UNUSED(buf);
Q_UNUSED(len);
Q_UNUSED(flags);
return 0;
#else
TF_EINTR_LOOP(::send(sockfd, buf, len, flags));
#endif
}
static inline int tf_recv(int sockfd, void *buf, size_t len, int flags)
{
#ifdef Q_OS_WIN
Q_ASSERT(0);
Q_UNUSED(sockfd);
Q_UNUSED(buf);
Q_UNUSED(len);
Q_UNUSED(flags);
return 0;
#else
TF_EINTR_LOOP(::recv(sockfd, buf, len, flags));
#endif
}
static inline int tf_dup(int fd)
{
#ifdef Q_OS_WIN
return ::_dup(fd);
#else
return ::fcntl(fd, F_DUPFD, 0);
#endif
}
static inline int tf_flock(int fd, int op)
{
#ifdef Q_OS_WIN
Q_UNUSED(fd);
Q_UNUSED(op);
return 0;
#else
TF_EINTR_LOOP(::flock(fd, op));
#endif
}
// advisory lock. exclusive:true=exclusive lock, false=shared lock
static inline int tf_lockfile(int fd, bool exclusive, bool blocking)
{
#ifdef Q_OS_WIN
auto handle = reinterpret_cast<HANDLE>(_get_osfhandle(fd));
DWORD dwFlags = (exclusive) ? LOCKFILE_EXCLUSIVE_LOCK : 0;
dwFlags |= (blocking) ? 0 : LOCKFILE_FAIL_IMMEDIATELY;
OVERLAPPED ov;
memset(&ov, 0, sizeof(OVERLAPPED));
BOOL res = LockFileEx(handle, dwFlags, 0, 0, 0, &ov);
return (res) ? 0 : -1;
#else
struct flock lck;
memset(&lck, 0, sizeof(struct flock));
lck.l_type = (exclusive) ? F_WRLCK : F_RDLCK;
lck.l_whence = SEEK_SET;
auto cmd = (blocking) ? F_SETLKW : F_SETLK;
TF_EINTR_LOOP(::fcntl(fd, cmd, &lck));
#endif
}
static inline int tf_unlink(const char *pathname)
{
#ifdef Q_OS_WIN
return ::_unlink(pathname);
#else
return ::unlink(pathname);
#endif
}
static inline int tf_fileno(FILE *stream)
{
#ifdef Q_OS_WIN
return ::_fileno(stream);
#else
return ::fileno(stream);
#endif
}
#ifdef Q_OS_UNIX
# include "tfcore_unix.h"
#endif
#endif // TFCORE_H