Skip to content

Commit b68931d

Browse files
committed
feat: add mac support
1 parent 8285b8a commit b68931d

5 files changed

Lines changed: 257 additions & 38 deletions

File tree

examples/vs2015/psn_client/psn_client.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@
3030

3131
#include <iostream>
3232

33-
void main( void )
33+
// Platform-specific sleep function
34+
#ifdef _WIN32
35+
#define SLEEP_MS(ms) Sleep(ms)
36+
#else
37+
#include <unistd.h>
38+
#define SLEEP_MS(ms) usleep((ms) * 1000)
39+
#endif
40+
41+
int main( void )
3442
{
3543
wsa_session session ;
3644

@@ -46,9 +54,9 @@ void main( void )
4654

4755
//====================================================
4856
// Main loop
49-
while ( 1 )
57+
while ( 1 )
5058
{
51-
Sleep( 1 ) ;
59+
SLEEP_MS( 1 ) ;
5260

5361
// Update Client
5462
::std::string msg ;
@@ -114,6 +122,6 @@ void main( void )
114122
}
115123
}
116124
}
117-
}
118-
119125

126+
return 0 ;
127+
}

examples/vs2015/psn_glut_client/psn_glut_client.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,24 @@
3939
#include <windows.h>
4040
#endif
4141

42-
#include <GL/gl.h>
43-
#include <GL/glu.h>
44-
#include <GL/glut.h>
42+
// Platform-specific sleep function
43+
#ifdef _WIN32
44+
#define SLEEP_MS(ms) Sleep(ms)
45+
#else
46+
#include <unistd.h>
47+
#define SLEEP_MS(ms) usleep((ms) * 1000)
48+
#endif
49+
50+
// Platform-specific OpenGL headers
51+
#ifdef __APPLE__
52+
#include <OpenGL/gl.h>
53+
#include <OpenGL/glu.h>
54+
#include <GLUT/glut.h>
55+
#else
56+
#include <GL/gl.h>
57+
#include <GL/glu.h>
58+
#include <GL/glut.h>
59+
#endif
4560

4661
#define PI_DEF 3.1415926535897932384626433832795f
4762

@@ -261,7 +276,7 @@ idle( void )
261276
::std::cout << "\n-----------------------------------------------" << ::std::endl ;
262277
}
263278

264-
Sleep( 10 ) ;
279+
SLEEP_MS( 10 ) ;
265280

266281
// schedule GL update
267282
glutPostRedisplay() ;

examples/vs2015/psn_server/psn_server.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,15 @@
3131
#include <list>
3232
#include <string>
3333

34-
void main( void )
34+
// Platform-specific sleep function
35+
#ifdef _WIN32
36+
#define SLEEP_MS(ms) Sleep(ms)
37+
#else
38+
#include <unistd.h>
39+
#define SLEEP_MS(ms) usleep((ms) * 1000)
40+
#endif
41+
42+
int main( void )
3543
{
3644
wsa_session session ;
3745

@@ -120,7 +128,9 @@ void main( void )
120128
::std::cout << "-----------------------------------------------" << ::std::endl ;
121129
}
122130

123-
Sleep( 1 ) ;
124-
timestamp++ ;
131+
SLEEP_MS( 1 ) ;
132+
timestamp++ ;
125133
}
134+
135+
return 0 ;
126136
}

include/utils/udp_socket.h

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@ THE SOFTWARE.
2727
#ifndef UDP_SOCKET_H
2828
#define UDP_SOCKET_H
2929

30+
// Platform detection - include appropriate implementation
31+
#ifdef _WIN32
32+
33+
// Windows implementation
3034
#include <WinSock2.h>
3135
#include <Ws2tcpip.h>
3236

33-
#pragma message("Note: including lib: Ws2_32.lib")
37+
#pragma message("Note: including lib: Ws2_32.lib")
3438
#pragma comment(lib, "Ws2_32.lib")
3539

3640
#include <string>
@@ -49,7 +53,7 @@ class wsa_session
4953
WSACleanup() ;
5054
}
5155

52-
private :
56+
private :
5357

5458
WSAData data_ ;
5559
} ;
@@ -59,7 +63,7 @@ class udp_socket
5963
{
6064
public :
6165

62-
udp_socket( void )
66+
udp_socket( void )
6367
{
6468
// UDP socket
6569
socket_ = socket( AF_INET , SOCK_DGRAM , IPPROTO_UDP ) ;
@@ -69,7 +73,7 @@ class udp_socket
6973
ioctlsocket( socket_ , FIONBIO , &arg ) ;
7074
}
7175

72-
~udp_socket( void )
76+
~udp_socket( void )
7377
{
7478
closesocket( socket_ ) ;
7579
}
@@ -80,7 +84,7 @@ class udp_socket
8084
add.sin_family = AF_INET ;
8185
InetPton( AF_INET , address.c_str() , &add.sin_addr.s_addr ) ;
8286
add.sin_port = htons( port ) ;
83-
87+
8488
if ( sendto( socket_ , message.c_str() , (int)message.length() , 0 , reinterpret_cast<SOCKADDR *>( &add ) , sizeof( add ) ) > 0 )
8589
return true ;
8690

@@ -117,52 +121,59 @@ class udp_socket
117121
return false ;
118122
}
119123

120-
bool enable_send_message_multicast( void )
124+
bool enable_send_message_multicast( void )
121125
{
122126
struct in_addr add ;
123127
add.s_addr = INADDR_ANY ;
124128

125-
int result = setsockopt( socket_ ,
126-
IPPROTO_IP ,
127-
IP_MULTICAST_IF ,
128-
(const char*)&add ,
129+
int result = setsockopt( socket_ ,
130+
IPPROTO_IP ,
131+
IP_MULTICAST_IF ,
132+
(const char*)&add ,
129133
sizeof( add ) ) ;
130-
return ( result != SOCKET_ERROR ) ;
134+
return ( result != SOCKET_ERROR ) ;
131135
}
132136

133-
bool join_multicast_group( const ::std::string & ip_group )
137+
bool join_multicast_group( const ::std::string & ip_group )
134138
{
135-
struct ip_mreq imr;
136-
139+
struct ip_mreq imr;
140+
137141
InetPton( AF_INET , ip_group.c_str() , &imr.imr_multiaddr.s_addr ) ;
138142
imr.imr_interface.s_addr = INADDR_ANY ;
139143

140-
int result = setsockopt( socket_ ,
141-
IPPROTO_IP ,
142-
IP_ADD_MEMBERSHIP ,
143-
(char*) &imr ,
144+
int result = setsockopt( socket_ ,
145+
IPPROTO_IP ,
146+
IP_ADD_MEMBERSHIP ,
147+
(char*) &imr ,
144148
sizeof(struct ip_mreq) ) ;
145-
return ( result != SOCKET_ERROR ) ;
149+
return ( result != SOCKET_ERROR ) ;
146150
}
147151

148-
bool leave_multicast_group( const ::std::string & ip_group )
152+
bool leave_multicast_group( const ::std::string & ip_group )
149153
{
150154
struct ip_mreq imr;
151-
155+
152156
InetPton( AF_INET , ip_group.c_str() , &imr.imr_multiaddr.s_addr ) ;
153157
imr.imr_interface.s_addr = INADDR_ANY ;
154158

155-
int result = setsockopt( socket_ ,
156-
IPPROTO_IP ,
159+
int result = setsockopt( socket_ ,
160+
IPPROTO_IP ,
157161
IP_DROP_MEMBERSHIP ,
158-
(char*) &imr ,
162+
(char*) &imr ,
159163
sizeof(struct ip_mreq) ) ;
160-
return ( result != SOCKET_ERROR ) ;
164+
return ( result != SOCKET_ERROR ) ;
161165
}
162166

163167
private :
164168

165-
SOCKET socket_ ;
169+
SOCKET socket_ ;
166170
} ;
167171

172+
#else
173+
174+
// POSIX implementation (macOS, Linux, etc.)
175+
#include "udp_socket_posix.h"
176+
177+
#endif
178+
168179
#endif

0 commit comments

Comments
 (0)