blob: aa361ffbffda5cf89f5ef218980b4a82e0cd5fbd [file] [log] [blame]
Roger Meier84e4a3c2011-09-16 20:58:44 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#ifndef _THRIFT_WINDOWS_CONFIG_H_
21#define _THRIFT_WINDOWS_CONFIG_H_ 1
22
23#if defined(_MSC_VER) && (_MSC_VER > 1200)
24#pragma once
25#endif // _MSC_VER
26
27#ifndef _WIN32
28#error This is a MSVC header only.
29#endif
30
Roger Meier8cd3b1a2011-09-23 14:25:22 +000031#pragma warning(disable: 4996) // Depreciated posix name.
32#pragma warning(disable: 4250) // Inherits via dominance.
33
Jake Farrell99010692011-11-30 02:09:46 +000034#define VERSION "0.9.0-dev"
Roger Meier8cd3b1a2011-09-23 14:25:22 +000035#define HAVE_GETTIMEOFDAY 1
Roger Meier3faaedf2011-10-02 10:51:45 +000036#define HAVE_SYS_STAT_H 1
Roger Meier8cd3b1a2011-09-23 14:25:22 +000037
Roger Meier84e4a3c2011-09-16 20:58:44 +000038#include "TargetVersion.h"
39#include "GetTimeOfDay.h"
40#include "Operators.h"
41#include "TWinsockSingleton.h"
Roger Meier878a9482011-10-11 19:02:37 +000042#include "WinFcntl.h"
Roger Meier84e4a3c2011-09-16 20:58:44 +000043#include "SocketPair.h"
44
45// boost
46#include <boost/cstdint.hpp>
47
48typedef boost::int64_t int64_t;
49typedef boost::uint32_t uint32_t;
50typedef boost::uint8_t uint8_t;
51
52// windows
53#include <Winsock2.h>
54#include <ws2tcpip.h>
55#pragma comment(lib, "Ws2_32.lib")
Roger Meierd1bf5d02012-04-11 21:38:56 +000056#pragma comment(lib, "advapi32.lib") //For security APIs in TPipeServer
Roger Meier84e4a3c2011-09-16 20:58:44 +000057
58// pthreads
Roger Meier3faaedf2011-10-02 10:51:45 +000059#if 0
60# include <pthread.h>
61#else
62struct timespec {
Roger Meier64af51b2012-02-06 20:46:06 +000063 int64_t tv_sec;
64 int64_t tv_nsec;
Roger Meier3faaedf2011-10-02 10:51:45 +000065};
66# define USE_BOOST_THREAD 1
67# define ctime_r( _clock, _buf ) \
68 ( strcpy( (_buf), ctime( (_clock) ) ), \
69 (_buf) )
70#endif
Roger Meier84e4a3c2011-09-16 20:58:44 +000071
Roger Meier8cd3b1a2011-09-23 14:25:22 +000072typedef ptrdiff_t ssize_t;
Roger Meier84e4a3c2011-09-16 20:58:44 +000073
74// Missing functions.
75#define usleep(ms) Sleep(ms)
Roger Meier6c26fad2011-12-08 20:44:16 +000076inline int sleep(DWORD ms)
77{
78 Sleep(ms);
79 return 0;
80}
Roger Meier84e4a3c2011-09-16 20:58:44 +000081
Roger Meier64af51b2012-02-06 20:46:06 +000082#if WINVER <= 0x0502 //XP, Server2003
83#define POLLIN 0x0300
84#define POLLOUT 0x0010
Roger Meier8cd3b1a2011-09-23 14:25:22 +000085#define poll(fds, nfds, timeout) \
86 poll_win32(fds, nfds, timeout)
87
Roger Meier4749b4f2012-01-17 12:25:22 +000088typedef struct pollfd {
Roger Meier64af51b2012-02-06 20:46:06 +000089 SOCKET fd;
90 SHORT events;
91 SHORT revents;
Roger Meier4749b4f2012-01-17 12:25:22 +000092} WSAPOLLFD, *PWSAPOLLFD, FAR *LPWSAPOLLFD;
93
Roger Meier64af51b2012-02-06 20:46:06 +000094inline int poll_win32(LPWSAPOLLFD fdArray, ULONG nfds, INT timeout)
Roger Meier8cd3b1a2011-09-23 14:25:22 +000095{
Roger Meier64af51b2012-02-06 20:46:06 +000096 fd_set read_fds, write_fds;
97 fd_set* read_fds_ptr = NULL;
98 fd_set* write_fds_ptr = NULL;
Roger Meier8cd3b1a2011-09-23 14:25:22 +000099
Roger Meier64af51b2012-02-06 20:46:06 +0000100 FD_ZERO(&read_fds);
101 FD_ZERO(&write_fds);
Roger Meier8cd3b1a2011-09-23 14:25:22 +0000102
Roger Meier64af51b2012-02-06 20:46:06 +0000103 for(ULONG i=0; i<nfds; i++) {
104 //Read (in) socket
105 if((fdArray[i].events & POLLIN) == POLLIN) {
106 read_fds_ptr = &read_fds;
107 FD_SET(fdArray[i].fd, &read_fds);
108 }
109 //Write (out) socket
110 else if((fdArray[i].events & POLLOUT) == POLLOUT) {
111 write_fds_ptr = &write_fds;
112 FD_SET(fdArray[i].fd, &write_fds);
113 }
114 }
Roger Meier8cd3b1a2011-09-23 14:25:22 +0000115
Roger Meier64af51b2012-02-06 20:46:06 +0000116 timeval time_out;
117 timeval* time_out_ptr = NULL;
118 if(timeout >= 0) {
Roger Meier4749b4f2012-01-17 12:25:22 +0000119 timeval time_out = {timeout / 1000, timeout * 1000};
Roger Meier64af51b2012-02-06 20:46:06 +0000120 time_out_ptr = &time_out;
121 }
122 else { //to avoid compiler warnings
123 (void)time_out;
124 (void)timeout;
125 }
126
127 int sktready = select(1, read_fds_ptr, write_fds_ptr, NULL, time_out_ptr);
128 if(sktready > 0) {
129 for(ULONG i=0; i<read_fds.fd_count; i++) {
130 if(FD_ISSET(fdArray[i].fd, &read_fds))
131 fdArray[i].revents = POLLIN;
132 }
133 for(ULONG i=0; i<write_fds.fd_count; i++) {
134 if(FD_ISSET(fdArray[i].fd, &write_fds))
135 fdArray[i].revents = POLLOUT;
136 }
137 }
138 return sktready;
Roger Meier8cd3b1a2011-09-23 14:25:22 +0000139}
Roger Meier64af51b2012-02-06 20:46:06 +0000140#else //Vista, Win7...
141 inline int poll(struct pollfd* fdArray, ULONG fds, INT timeout) {
142 return WSAPoll(fdArray, fds, timeout);
143 }
Roger Meier3faaedf2011-10-02 10:51:45 +0000144#endif // WINVER
Roger Meier84e4a3c2011-09-16 20:58:44 +0000145
Roger Meier8b1adf42012-02-28 20:34:06 +0000146inline int close(SOCKET socket)
Roger Meier84e4a3c2011-09-16 20:58:44 +0000147{
Roger Meier8b1adf42012-02-28 20:34:06 +0000148 return ::closesocket(socket);
Roger Meier84e4a3c2011-09-16 20:58:44 +0000149}
150
151#endif // _THRIFT_WINDOWS_CONFIG_H_