Sha256: a6bc96db16d97dd4bcc44d80a63399d2bc824ddc5df26a259d66ace18c06d7ac

Contents?: true

Size: 1.43 KB

Versions: 27

Compression:

Stored size: 1.43 KB

Contents

#include <Gosu/Sockets.hpp>
#include "Socket.hpp"
#include <cassert>
#include <cstring>

struct Gosu::ListenerSocket::Impl
{
    Socket socket;
};

Gosu::ListenerSocket::ListenerSocket(SocketPort port)
: pimpl(new Impl)
{
    pimpl->socket.setHandle(socketCheck(::socket(AF_INET, SOCK_STREAM, 0)));
    pimpl->socket.setBlocking(false);

    int enable = 1;
    socketCheck(::setsockopt(pimpl->socket.handle(), SOL_SOCKET, SO_REUSEADDR,
        reinterpret_cast<char*>(&enable), sizeof enable));

    sockaddr_in addr;
    std::memset(&addr, 0, sizeof addr);
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    addr.sin_port = htons(port);
    socketCheck(::bind(pimpl->socket.handle(),
        reinterpret_cast<sockaddr*>(&addr), sizeof addr));
    socketCheck(::listen(pimpl->socket.handle(), 10));
}

Gosu::ListenerSocket::~ListenerSocket()
{
}

Gosu::SocketAddress Gosu::ListenerSocket::address() const
{
    return pimpl->socket.address();
}

Gosu::SocketPort Gosu::ListenerSocket::port() const
{
    return pimpl->socket.port();
}

void Gosu::ListenerSocket::update()
{
    while (onConnection)
    {
        SocketHandle newHandle =
            socketCheck(::accept(pimpl->socket.handle(), 0, 0));

        if (newHandle == INVALID_SOCKET)
            break;

        Socket newSocket;
        newSocket.setHandle(newHandle);
        onConnection(newSocket);
    }
}

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
gosu-0.9.2 src/Sockets/ListenerSocket.cpp
gosu-0.9.2.pre1 src/Sockets/ListenerSocket.cpp
gosu-0.9.1 src/Sockets/ListenerSocket.cpp
gosu-0.9.0 src/Sockets/ListenerSocket.cpp
gosu-0.9.0.pre1 src/Sockets/ListenerSocket.cpp
gosu-0.8.7.2 src/Sockets/ListenerSocket.cpp
gosu-0.8.7.1 src/Sockets/ListenerSocket.cpp
gosu-0.8.7 src/Sockets/ListenerSocket.cpp
gosu-0.8.6 src/Sockets/ListenerSocket.cpp
gosu-0.8.6.pre1 src/Sockets/ListenerSocket.cpp
gosu-0.8.5 src/Sockets/ListenerSocket.cpp
gosu-0.8.5.pre1 src/Sockets/ListenerSocket.cpp
gosu-0.8.4 src/Sockets/ListenerSocket.cpp
gosu-0.8.3 src/Sockets/ListenerSocket.cpp
gosu-0.8.2 src/Sockets/ListenerSocket.cpp
gosu-0.8.1 src/Sockets/ListenerSocket.cpp
gosu-0.8.0 src/Sockets/ListenerSocket.cpp
gosu-0.8.0.pre7 src/Sockets/ListenerSocket.cpp
gosu-0.8.0.pre6 src/Sockets/ListenerSocket.cpp
gosu-0.8.0.pre5 src/Sockets/ListenerSocket.cpp