Making SDL_net C++ compilable

Hi there!
Attached is a patch that contains the changes to SDL_net to get the sources
compiling with a C++ compiler(g++).
It mostly gets around syntactic differences between C and C++ via some #ifdefs.
Further some casts are removed that are dubious to a C++ compiler(e.g. in
SDLnetTCP.c) in favor of a declaration of sock_len with the proper type from
the beginning. But I’m not sure that my #ifdef logic captures the behaviour of
the accept() system call on all systems supported by SDL_net.
Happy Sunday, Flo.
-------------- next part --------------
Index: SDLnetTCP.c===================================================================
— SDLnetTCP.c (revision 5119)
+++ SDLnetTCP.c (working copy)
@@ -817,8 +817,16 @@
{
TCPsocket sock;
struct sockaddr_in sock_addr;

  • int sock_alen;
    +#ifdef USE_GUSI_SOCKETS
  • unsigned int sock_alen;
    +#endif
    +#ifdef WIN32

  • int sock_alen;
    +#else

  • socklen_t sock_alen; /default to some POSIX behaviour/
    +#endif

  • /* Only server sockets can accept */
    if ( ! server->sflag ) {
    SDLNet_SetError(“Only server sockets can accept()”);
    @@ -835,12 +843,7 @@

    /* Accept a new TCP connection on a server socket */
    sock_alen = sizeof(sock_addr);

  • sock->channel = accept(server->channel, (struct sockaddr *)&sock_addr,
    -#ifdef USE_GUSI_SOCKETS
  •   				(unsigned int *)&sock_alen);
    

-#else

  •   						&sock_alen);
    

-#endif

  • sock->channel = accept(server->channel, (struct sockaddr *)&sock_addr, &sock_alen);
    if ( sock->channel == INVALID_SOCKET ) {
    SDLNet_SetError(“accept() failed”);
    goto error_return;
    Index: SDLnetUDP.c
    ===================================================================
    — SDLnetUDP.c (revision 5119)
    +++ SDLnetUDP.c (working copy)
    @@ -443,7 +443,11 @@
    */
    int SDLNet_UDP_Bind(UDPsocket sock, int channel, IPaddress *address)
    {
  • struct UDP_channel *binding;
    +#ifdef __cplusplus
  • struct _UDPsocket::UDP_channel *binding;
    +#else

  • struct UDP_channel *binding;
    +#endif

    if ( channel == -1 ) {
    for ( channel=0; channel < SDLNET_MAX_UDPCHANNELS; ++channel ) {
    @@ -509,7 +513,11 @@
    int SDLNet_UDP_SendV(UDPsocket sock, UDPpacket **packets, int npackets)
    {
    int numsent, i, j;

  • struct UDP_channel *binding;
    +#ifdef __cplusplus
  • struct _UDPsocket::UDP_channel *binding;
    +#else
  • struct UDP_channel *binding;
    +#endif
    int status;
    #ifndef MACOS_OPENTRANSPORT
    int sock_len;
    @@ -690,13 +698,24 @@
    extern int SDLNet_UDP_RecvV(UDPsocket sock, UDPpacket **packets)
    {
    int numrecv, i, j;
  • struct UDP_channel *binding;
    +#ifdef __cplusplus
  • struct _UDPsocket::UDP_channel *binding;
    +#else
  • struct UDP_channel *binding;
    +#endif
    #ifdef MACOS_OPENTRANSPORT
    TUnitData OTpacket;
    OTFlags flags;
    InetAddress address;
    #else
  • int sock_len;
    +#ifdef USE_GUSI_SOCKETS
  • unsigned int sock_len;
    +#endif
    +#ifdef WIN32
  • int sock_len;
    +#else
  • socklen_t sock_len; /default to some POSIX behaviour/
    +#endif
    struct sockaddr_in sock_addr;
    #endif

@@ -732,12 +751,7 @@
sock_len = sizeof(sock_addr);
packet->status = recvfrom(sock->channel,
packet->data, packet->maxlen, 0,

  •   		(struct sockaddr *)&sock_addr,
    

-#ifdef USE_GUSI_SOCKETS

  •   		(unsigned int *)&sock_len);
    

-#else

  •   				&sock_len);
    

-#endif

  •   		(struct sockaddr *)&sock_addr, &sock_len);
      if ( packet->status >= 0 ) {
      	packet->len = packet->status;
      	packet->address.host = sock_addr.sin_addr.s_addr;
    

Index: SDL_net.h

— SDL_net.h (revision 5119)
+++ SDL_net.h (working copy)
@@ -269,7 +269,7 @@
typedef struct _SDLNet_SocketSet *SDLNet_SocketSet;

/* Any network socket can be safely cast to this socket type */
-typedef struct {
+typedef struct SDLNet_GenericSocket_{
int ready;
} *SDLNet_GenericSocket;

This sounded like an interesting patch, is it possible to have it merged?
VittorioOn Sun, Oct 18, 2009 at 5:04 PM, Florian Goth wrote:

Hi there!
Attached is a patch that contains the changes to SDL_net to get the sources
compiling with a C++ compiler(g++).
It mostly gets around syntactic differences between C and C++ via some
#ifdefs.
Further some casts are removed that are dubious to a C++ compiler(e.g. in
SDLnetTCP.c) in favor of a declaration of sock_len with the proper type
from
the beginning. But I’m not sure that my #ifdef logic captures the behaviour
of
the accept() system call on all systems supported by SDL_net.
Happy Sunday, Flo.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Charles de Gaullehttp://www.brainyquote.com/quotes/authors/c/charles_de_gaulle.html

  • “The better I get to know men, the more I find myself loving dogs.”
  • int sock_alen;
    +#ifdef USE_GUSI_SOCKETS
  • unsigned int sock_alen;
    +#endif
    +#ifdef WIN32
  • int sock_alen;
    +#else
  • socklen_t sock_alen; /default to some POSIX behaviour/
    +#endif

If USE_GUSI_SOCKETS is defined, there will have been two declarations
of sock_alen. You need to use another #else, and move the first #endif
to the bottom of this block where the other one is. Or something like
that.–
http://codebad.com/

Also, about the, the _UDPsocket::UDP_channel stuff, couldn’t you just
do this instead near the beginning:

#ifdef __cplusplus
using UDPsocket::UDP_channel
#endif
http://codebad.com/

Hi there!
finally I took the time to update the patch and incorporate some of your
suggestions. the “using” idea didn’t work out, so I put UDPsocket into the
global namespace.
Best, Flo.On Thursday 24 December 2009 18:10:39 Donny Viszneki wrote:

Also, about the, the _UDPsocket::UDP_channel stuff, couldn’t you just
do this instead near the beginning:

#ifdef __cplusplus
using UDPsocket::UDP_channel
#endif

-------------- next part --------------
Index: SDLnetTCP.c

— SDLnetTCP.c (revision 5549)
+++ SDLnetTCP.c (working copy)
@@ -817,8 +817,15 @@
{
TCPsocket sock;
struct sockaddr_in sock_addr;

  • int sock_alen;
    +#ifdef USE_GUSI_SOCKETS
  •    unsigned int sock_alen;
    

+#elif defined(WIN32)

  •    int sock_alen;
    

+#else

  •    socklen_t sock_alen; /*default to some POSIX behaviour*/
    

+#endif

  • /* Only server sockets can accept */
    if ( ! server->sflag ) {
    SDLNet_SetError(“Only server sockets can accept()”);
    @@ -835,12 +842,7 @@

    /* Accept a new TCP connection on a server socket */
    sock_alen = sizeof(sock_addr);

  • sock->channel = accept(server->channel, (struct sockaddr *)&sock_addr,
    -#ifdef USE_GUSI_SOCKETS
  •   				(unsigned int *)&sock_alen);
    

-#else

  •   						&sock_alen);
    

-#endif

  • sock->channel = accept(server->channel, (struct sockaddr *)&sock_addr, &sock_alen);
    if ( sock->channel == INVALID_SOCKET ) {
    SDLNet_SetError(“accept() failed”);
    goto error_return;
    Index: SDLnetUDP.c
    ===================================================================
    — SDLnetUDP.c (revision 5549)
    +++ SDLnetUDP.c (working copy)
    @@ -28,6 +28,11 @@
    #include <Events.h>
    #endif

+struct UDP_channel {

  • int numbound;
  • IPaddress address[SDLNET_MAX_UDPADDRESSES];
    +};

struct _UDPsocket {
int ready;
SOCKET channel;
@@ -47,10 +52,7 @@
OTConfigurationRef config; // Master configuration. you can clone this.
#endif /* MACOS_OPENTRANSPORT */

  • struct UDP_channel {
  •   int numbound;
    
  •   IPaddress address[SDLNET_MAX_UDPADDRESSES];
    
  • } binding[SDLNET_MAX_UDPCHANNELS];
  • UDP_channel binding[SDLNET_MAX_UDPCHANNELS];
    };

#ifdef MACOS_OPENTRANSPORT
@@ -696,7 +698,13 @@
OTFlags flags;
InetAddress address;
#else

  • int sock_len;
    +#ifdef USE_GUSI_SOCKETS
  • unsigned int sock_len;
    +#elif defined(WIN32)
  • int sock_len;
    +#else
  • socklen_t sock_len; /default to some POSIX behaviour/
    +#endif
    struct sockaddr_in sock_addr;
    #endif

@@ -732,12 +740,7 @@
sock_len = sizeof(sock_addr);
packet->status = recvfrom(sock->channel,
packet->data, packet->maxlen, 0,

  •   		(struct sockaddr *)&sock_addr,
    

-#ifdef USE_GUSI_SOCKETS

  •   		(unsigned int *)&sock_len);
    

-#else

  •   				&sock_len);
    

-#endif

  •   		(struct sockaddr *)&sock_addr, &sock_len);
      if ( packet->status >= 0 ) {
      	packet->len = packet->status;
      	packet->address.host = sock_addr.sin_addr.s_addr;
    

Index: SDL_net.h

— SDL_net.h (revision 5549)
+++ SDL_net.h (working copy)
@@ -269,7 +269,7 @@
typedef struct _SDLNet_SocketSet *SDLNet_SocketSet;

/* Any network socket can be safely cast to this socket type */
-typedef struct {
+typedef struct SDLNet_GenericSocket_{
int ready;
} *SDLNet_GenericSocket;