TreeFrogFramework  2.8
tfcore_win.h
Go to the documentation of this file.
1 #pragma once
2 #define NOMINMAX
3 #include <winsock2.h>
4 #include <ws2tcpip.h>
5 #include <windows.h>
6 #include <io.h>
7 #include <winbase.h>
8 #include "tfcore.h"
9 
10 #ifndef Q_OS_WIN
11 #error "tfcore_win.h included on a non-Windows system"
12 #endif
13 
14 namespace {
15 
16 inline int tf_poll(pollfd *fds, int nfds, int timeout)
17 {
18  return ::WSAPoll(fds, nfds, timeout);
19 }
20 
25 inline int tf_poll_recv(int socket, int timeout)
26 {
27  pollfd pfd = {(SOCKET)socket, POLLIN, 0};
28  int ret = tf_poll(&pfd, 1, timeout);
29  return ret;
30 }
31 
36 inline int tf_poll_send(int socket, int timeout)
37 {
38  pollfd pfd = {(SOCKET)socket, POLLOUT, 0};
39  int ret = tf_poll(&pfd, 1, timeout);
40  return ret;
41 }
42 
43 
44 inline int tf_close(int fd)
45 {
46  return ::_close(fd);
47 }
48 
49 
50 inline int tf_read(int fd, void *buf, size_t count)
51 {
52  return ::_read(fd, buf, (uint)count);
53 }
54 
55 
56 inline int tf_write(int fd, const void *buf, size_t count)
57 {
58  return ::_write(fd, buf, (uint)count);
59 }
60 
61 
62 inline int tf_send(int sockfd, const void *buf, size_t len, int flags = 0)
63 {
64  Q_UNUSED(flags);
65  return ::send((SOCKET)sockfd, (const char *)buf, (int)len, 0);
66 }
67 
68 
69 inline int tf_recv(int sockfd, void *buf, size_t len, int flags = 0)
70 {
71  Q_UNUSED(flags);
72  return ::recv((SOCKET)sockfd, (char *)buf, (int)len, 0);
73 }
74 
75 
76 inline int tf_close_socket(int sockfd)
77 {
78  return ::closesocket((SOCKET)sockfd);
79 }
80 
81 
82 inline int tf_dup(int fd)
83 {
84  return ::_dup(fd);
85 }
86 
87 
88 inline int tf_flock(int fd, int op)
89 {
90  Q_UNUSED(fd);
91  Q_UNUSED(op);
92  return 0;
93 }
94 
95 // advisory lock. exclusive:true=exclusive lock, false=shared lock
96 inline int tf_lockfile(int fd, bool exclusive, bool blocking)
97 {
98  auto handle = reinterpret_cast<HANDLE>(_get_osfhandle(fd));
99  DWORD dwFlags = (exclusive) ? LOCKFILE_EXCLUSIVE_LOCK : 0;
100  dwFlags |= (blocking) ? 0 : LOCKFILE_FAIL_IMMEDIATELY;
101  OVERLAPPED ov;
102  std::memset(&ov, 0, sizeof(OVERLAPPED));
103  BOOL res = LockFileEx(handle, dwFlags, 0, 0, 0, &ov);
104  return (res) ? 0 : -1;
105 }
106 
107 
108 inline int tf_unlink(const char *pathname)
109 {
110  return ::_unlink(pathname);
111 }
112 
113 
114 inline int tf_fileno(FILE *stream)
115 {
116  return ::_fileno(stream);
117 }
118 
119 } // namespace