Sha256: b957fec4e0c46127e19d66b90fd8395e2322e7db045014baa7d0677738cee4f8

Contents?: true

Size: 1.5 KB

Versions: 8

Compression:

Stored size: 1.5 KB

Contents

#ifndef GOSUIMPL_SOCKETS_HPP
#define GOSUIMPL_SOCKETS_HPP

#include <Gosu/Platform.hpp>

#ifdef GOSU_IS_WIN
    #include "winsock2.h"
    #define GOSU_SOCK_ERR(code) WSA##code
    namespace Gosu { typedef SOCKET SocketHandle; }
    typedef int socklen_t;
#else
    #include <sys/errno.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <sys/ioctl.h> 
    #define GOSU_SOCK_ERR(code) code
    #define INVALID_SOCKET -1
    #define SOCKET_ERROR -1
    #define closesocket close
    #define ioctlsocket ioctl
    namespace Gosu { typedef int SocketHandle; }
#endif

namespace Gosu
{
    // Owns a socket and manages library initialization.
    class Socket    {
        Socket(const Socket&);
        Socket& operator=(const Socket&);
        
        SocketHandle handle_;
    public:
        Socket();
        ~Socket();

        SocketHandle handle() const;
        void setHandle(SocketHandle value);
        void setBlocking(bool value);

        SocketAddress address() const;
        SocketPort port() const;

        void swap(Socket& other);
    };

    int lastSocketError();
    
    GOSU_NORETURN void throwLastSocketError();
    
    template<typename T>
    T socketCheck(T retVal)
    {
        if (retVal == SOCKET_ERROR &&
            lastSocketError() != GOSU_SOCK_ERR(EWOULDBLOCK))
        {
            throwLastSocketError();
        }
        
        return retVal;
    }
}

#endif

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
gosu-0.7.43 GosuImpl/Sockets/Sockets.hpp
gosu-0.7.41 GosuImpl/Sockets/Sockets.hpp
gosu-0.7.40 GosuImpl/Sockets/Sockets.hpp
gosu-0.7.39 GosuImpl/Sockets/Sockets.hpp
gosu-0.7.38 GosuImpl/Sockets/Sockets.hpp
gosu-0.7.37 GosuImpl/Sockets/Sockets.hpp
gosu-0.7.36.2 GosuImpl/Sockets/Sockets.hpp
gosu-0.7.35 GosuImpl/Sockets/Sockets.hpp