Skip to content

Commit

Permalink
Microsoft sucks.
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown committed Dec 5, 2010
1 parent cfce901 commit 739bd10
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 12 deletions.
12 changes: 12 additions & 0 deletions src/ClockSocket.cpp
Expand Up @@ -5,6 +5,7 @@
ClockSocket::ClockSocket( ) {
char enable_loopback = 1;

#ifndef WINDOWS
if (
setsockopt(
socket_fd, IPPROTO_IP, IP_MULTICAST_LOOP,
Expand All @@ -13,6 +14,7 @@ ClockSocket::ClockSocket( ) {
) {
throw std::runtime_error("Failed to set multicast loopback");
}
#endif

/* initialize destination address */
destination.sin_family = AF_INET;
Expand All @@ -26,8 +28,18 @@ ClockSocket::~ClockSocket( ) {
}

void ClockSocket::send(void *thing_to_send, size_t size) {
#ifdef WINDOWS
/*
* Dear Microsoft: if you were trying to make your interface incompatible,
* you couldn't have been more subtle about it by accident.
* (And stop indenting my code for me.
*/
if (sendto(socket_fd, (char *)thing_to_send, size, 0,
(struct sockaddr *)&destination, sizeof(destination)) == -1) {
#else
if (sendto(socket_fd, thing_to_send, size, 0,
(struct sockaddr *)&destination, sizeof(destination)) == -1) {
#endif
fprintf(stderr, "warning: failed to send out clock update");
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/HockeyData.h
@@ -1,11 +1,14 @@
#ifndef _hockeydata_h_
#define _hockeydata_h_

/* needs to come first so winsock2.h comes first... stupid MSFT */
#include "SyncSocket.h"

#include "FreeType.h"
#include "HockeyRoster.h"
#include "Mclock.h"
#include "serialsync.h"
#include "SyncSocket.h"

#include <string>
#include <sstream>

Expand Down
5 changes: 4 additions & 1 deletion src/HockeyDraw.h
@@ -1,6 +1,9 @@
#ifndef _hockeydraw_h_
#define _hockeydraw_h_

/* must go here so winsock2.h is included before windows.h... ugh */
#include "ClockSocket.h"

#ifdef WINDOWS
#include <windows.h>
#endif
Expand All @@ -9,7 +12,7 @@
#include <GL/glu.h>
#include "HockeyData.h"
#include "HockeyDrop.h"
#include "ClockSocket.h"


class HockeyDraw {
public:
Expand Down
3 changes: 2 additions & 1 deletion src/HockeyDrop.h
Expand Up @@ -2,8 +2,9 @@
#define _hockeydrop_h_
#include <string>
//#include "GLBitmap.h"
#include "GLCairoTextSurface.h"

#include "HockeyData.h"
#include "GLCairoTextSurface.h"
#include "FreeType.h"
#include "Mclock.h"

Expand Down
24 changes: 21 additions & 3 deletions src/SyncSocket.cpp
Expand Up @@ -9,17 +9,27 @@ SyncSocket::SyncSocket( ) {
struct ip_mreq mreq;

inet_aton("239.160.181.93", &mreq.imr_multiaddr);
inet_aton("0.0.0.0", &mreq.imr_interface);

#ifdef WINDOWS
mreq.imr_interface.s_addr = INADDR_ANY;
#else
inet_aton("0.0.0.0", &mreq.imr_interface);
#endif
memset(&listen_addr, 0, sizeof(listen_addr));
listen_addr.sin_family = AF_INET;
listen_addr.sin_port = htons(30004);

if (bind(socket_fd, (struct sockaddr *)&listen_addr, sizeof(listen_addr)) == -1) {
print_error("bind");
throw std::runtime_error("failed to bind to port 30004");
}

if (setsockopt(socket_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) != 0) {
#ifdef WINDOWS
/* Dear Microsoft: This is *exactly* what "void *" pointers were meant for! */
if (setsockopt(socket_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq)) != 0) {
#else
if (setsockopt(socket_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) != 0) {
#endif
print_error("setsockopt");
throw std::runtime_error("failed to bind to multicast address 239.160.181.93");
}
}
Expand Down Expand Up @@ -50,7 +60,15 @@ bool SyncSocket::can_receive( ) {
}

size_t SyncSocket::receive(void *buf, size_t size) {
#ifdef WINDOWS
/*
* Dear Microsoft: RTFMan Pages on Sockets.
* Hint: Look for "void *". It's everywhere.
*/
ssize_t ret = recvfrom(socket_fd, (char *)buf, size, 0, NULL, NULL);
#else
ssize_t ret = recvfrom(socket_fd, buf, size, 0, NULL, NULL);
#endif
if (ret < 0) {
throw std::runtime_error("recvfrom( ) failed");
} else {
Expand Down
28 changes: 27 additions & 1 deletion src/UDPSocket.cpp
@@ -1,11 +1,37 @@
#include "UDPSocket.h"
#include <stdexcept>

#ifdef WINDOWS

void inet_aton(const char *a, IN_ADDR *n) {
n->s_addr = inet_addr(a);
}

void print_error(const char *msg) {
/* Dear Microsoft: This is *so* much easier on Unix... */
TCHAR buf[128];
memset(buf, 0, sizeof(buf));

FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError( ),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
buf, sizeof(buf) - 1, NULL
);

fprintf(stderr, "%s: %s\n", msg, buf);

}

#endif

UDPSocket::UDPSocket( ) {
#ifdef WINDOWS
WSADATA wsad;
if (winsock_started == 0) {
if (WSAStartup(0x0101, &wsad) != 0) {
if (WSAStartup(0x0002, &wsad) != 0) {
throw std::runtime_error("Failed to initialize Windows sockets API\n");
}
}
Expand Down
14 changes: 13 additions & 1 deletion src/UDPSocket.h
Expand Up @@ -2,13 +2,25 @@
#define _udpsocket_h_

#ifdef WINDOWS
#include <winsock.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>

void inet_aton(const char *a, IN_ADDR *n);
void print_error(const char *msg);

/* Visual Studio apparently lacks a <stdint.h> */
typedef unsigned __int32 uint32_t;
typedef signed __int32 ssize_t;

#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

#define print_error perror
#endif


Expand Down
3 changes: 2 additions & 1 deletion src/main.cpp
Expand Up @@ -240,7 +240,8 @@ int main( int argc, char* argv[] ) {
if ( p.y >= 0 && p.y < 100 ) {
SetCursor(NULL);
}
HINSTANCE hInstance = (HINSTANCE) glutGetHINSTANCE();

HINSTANCE hInstance = (HINSTANCE) GetModuleHandle(NULL);
curN = LoadCursor(NULL, IDC_ARROW);
curG = LoadCursor(hInstance, MAKEINTRESOURCE(IDC_GREEN));
curB = LoadCursor(hInstance, MAKEINTRESOURCE(IDC_BLUE));
Expand Down
10 changes: 7 additions & 3 deletions src/main.h
Expand Up @@ -5,8 +5,9 @@
// required libraries
#pragma comment(lib, "opengl32")
#pragma comment(lib, "glu32")
#pragma comment(lib, "glaux")

#pragma comment(lib, "ws2_32")
//#pragma comment(lib, "glaux")
#include <winsock2.h>
#include <windows.h> // Header File For Windows
#endif

Expand All @@ -18,7 +19,10 @@
#include <GL/glu.h>
#include <GL/glut.h>

//#include "..\vc7\resource.h"
#ifdef WINDOWS
#include "..\vc7\resource.h"
#endif

#include "FreeType.h"
#include "HockeyData.h"
#include "HockeyDrop.h"
Expand Down

0 comments on commit 739bd10

Please sign in to comment.