Skip to content

Commit

Permalink
libdeng2: LegacyNetwork can relinquish control of a socket
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Jan 27, 2013
1 parent 51c3264 commit b5d8dcc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
11 changes: 11 additions & 0 deletions doomsday/libdeng2/include/de/legacy/legacynetwork.h
Expand Up @@ -28,6 +28,7 @@ namespace de {
class Address;
class Block;
class IByteArray;
class Socket;

/**
* Network communications for the legacy engine implementation. Implements
Expand Down Expand Up @@ -66,6 +67,16 @@ class LegacyNetwork
*/
bool incomingForSocket(int socket);

/**
* Relinquishes ownership of a socket. The socket is removed from any
* socket sets, and loses its LegacyNetwork-specific id.
*
* @param socket Socket id.
*
* @return de::Socket instance. Caller gets ownership.
*/
Socket *takeSocket(int socket);

private:
struct Instance;
Instance *d;
Expand Down
33 changes: 29 additions & 4 deletions doomsday/libdeng2/src/legacy/legacynetwork.cpp
Expand Up @@ -46,21 +46,41 @@ struct LegacyNetwork::Instance
SocketSets sets;

Instance() : idGen(0) {}
~Instance() {

~Instance()
{
// Cleanup time! Delete all existing sockets.
foreach(Socket *s, sockets.values()) {
foreach(Socket *s, sockets.values())
{
delete s;
}
foreach(ListenSocket *s, serverSockets.values()) {
foreach(ListenSocket *s, serverSockets.values())
{
delete s;
}
}
int nextId() {

int nextId()
{
/** @todo This will fail after 2.1 billion sockets have been opened.
* That might take a while, though...
*/
return ++idGen;
}

Socket *take(int id)
{
Socket *sock = sockets[id];
sockets.remove(id);

// Remove from any sets.
DENG2_FOR_EACH(SocketSets, i, sets)
{
i->members.removeAll(sock);
}

return sock;
}
};

LegacyNetwork::LegacyNetwork()
Expand Down Expand Up @@ -240,4 +260,9 @@ bool LegacyNetwork::incomingForSocket(int socket)
return d->sockets[socket]->hasIncoming();
}

Socket *LegacyNetwork::takeSocket(int socket)
{
return d->take(socket);
}

} // namespace de

0 comments on commit b5d8dcc

Please sign in to comment.