medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 53d99b9442f363ded9934019d85a87f87a41ba23
parent b129beaaf3ab4f91ba2271e35d803a94fd4b93a2
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sat Nov 19 18:46:07 +0200

Make http module work on Windows

Diffstat:
http.cc | 15++++++---------
http.h | 4+---
log.cc | 5-----
platform_io.h | 10++++++++--
platform_net.h | 19+++++++++++++++++++
5 files changed, 34 insertions(+), 19 deletions(-)
diff --git a/http.cc b/http.cc @@ -1,24 +1,21 @@ -#include <sys/types.h> -#include <sys/select.h> -#include <sys/socket.h> - #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <netdb.h> -#include <unistd.h> #include <errno.h> #include <string> #include "intrinsics.h" +#include "platform_io.h" +#include "platform_net.h" #include "http.h" bool dns_first( const char * host, struct sockaddr_storage * address ) { struct addrinfo hints; memset( &hints, 0, sizeof( struct addrinfo ) ); hints.ai_family = AF_UNSPEC; - hints.ai_socktype = SOCK_DGRAM; + hints.ai_socktype = SOCK_STREAM; + hints.ai_protocol = IPPROTO_TCP; struct addrinfo * addresses; int ok = getaddrinfo( host, "http", &hints, &addresses ); @@ -37,7 +34,7 @@ GetResult http_get( const struct sockaddr_storage & address, const char * host, if( sock == -1 ) { return GET_ERROR_SOCKET; } - SCOPE_EXIT( close( sock ) ); + SCOPE_EXIT( closesocket( sock ) ); if( connect( sock, ( const sockaddr * ) &address, sizeof( address ) ) == -1 ) { return GET_ERROR_CONNECT; @@ -67,7 +64,7 @@ GetResult http_get( const struct sockaddr_storage & address, const char * host, { fd_set select_fds; FD_ZERO( &select_fds ); - FD_SET( sock, &select_fds ); + FD_SET( checked_cast< unsigned int >( sock ), &select_fds ); struct timeval timeout; timeout.tv_sec = 10; diff --git a/http.h b/http.h @@ -1,12 +1,10 @@ #ifndef _HTTP_H_ #define _HTTP_H_ -#include <sys/types.h> -#include <sys/socket.h> - #include <string> #include "intrinsics.h" +#include "platform_net.h" enum GetResult { GET_OK, diff --git a/log.cc b/log.cc @@ -16,11 +16,6 @@ static const char * names[ LOGLEVEL_COUNT + 1 ] = { "all", }; -// TODO: -#if defined( _WIN32 ) -#define snprintf _snprintf -#endif - // this might lead to trouble with threads static void log_init() { if( initialised ) return; diff --git a/platform_io.h b/platform_io.h @@ -1,13 +1,19 @@ #ifndef _PLATFORM_IO_H_ #define _PLATFORM_IO_H_ -#if defined( _WIN32 ) +#include "platform.h" + +#if PLATFORM_WINDOWS #include <windows.h> +#include <io.h> +#define ssize_t int +#define snprintf _snprintf #define mkdir( path, mode ) ( CreateDirectory( path, NULL ) ? 0 : -1 ) -#elif defined( __linux__ ) || defined( __APPLE__ ) +#elif PLATFORM_UNIX #include <sys/stat.h> #include <sys/types.h> +#include <unistd.h> #else #error new platform #endif diff --git a/platform_net.h b/platform_net.h @@ -0,0 +1,19 @@ +#ifndef _PLATFORM_NET_H_ +#define _PLATFORM_NET_H_ + +#include "platform.h" + +#if PLATFORM_WINDOWS +#include <winsock2.h> +#include <ws2tcpip.h> +#elif PLATFORM_UNIX +#include <sys/types.h> +#include <sys/select.h> +#include <sys/socket.h> +#include <netdb.h> +#define closesocket close +#else +#error new platform +#endif + +#endif // _PLATFORM_NET_H_