Skip to content

Commit

Permalink
Added internal storage of clients
Browse files Browse the repository at this point in the history
  • Loading branch information
bausshf committed May 16, 2017
1 parent 9335275 commit 9646fd4
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/socketclient.d
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ class SocketClient(T) {
/// The address string.
string _addressString;

/// The client id.
size_t _clientId;

public:
/**
* Creates a new tcp client.
Expand Down Expand Up @@ -113,6 +116,16 @@ class SocketClient(T) {

/// Gets the address string.
auto address() pure @safe { return _addressString; }

package(cheetah) {
/// Sets the client id.
void clientId(size_t newClientId) {
_clientId = newClientId;
}
}

/// Gets the client id.
size_t clientId() { return _clientId; }
}

/**
Expand Down Expand Up @@ -195,6 +208,8 @@ class SocketClient(T) {
_disconnected = true;

try {
_server.removeClient(_clientId);

if (_connection.connected) {
_connection.close();
}
Expand Down
63 changes: 60 additions & 3 deletions src/socketserver.d
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ class SocketServer(T) {
/// Boolean determining whether the server is running or not.
bool _running;

/// Boolean determining whether clients should be stored or not.
bool _storeClients;

/// A collection of clients.
(SocketClient!T)[size_t] _clients;

/// The next client id.
size_t _nextClientId;

public:
/**
* Creates a new tcp socket server.
Expand Down Expand Up @@ -128,6 +137,18 @@ class SocketServer(T) {
void copyErrorEvents(bool shouldCopy) {
_copyErrorEvents = shouldCopy;
}

/// Gets a boolean determining whether clients should be stored or not.
bool storeClients() { return _storeClients; }

/**
* Sets a boolean determining whether clients should be stored or not.
* Params:
* shouldStore = Boolean determining whether clients should be stored or not.
*/
void storeClients(bool shouldStore) {
_storeClients = shouldStore;
}
}

/**
Expand Down Expand Up @@ -248,7 +269,7 @@ class SocketServer(T) {
}
catch (Throwable e) {
report(new Exception(e.toString()), this);

throw e;
}

Expand All @@ -273,13 +294,29 @@ class SocketServer(T) {
}
catch (Throwable e) {
report(new Exception(e.toString()), this);

throw e;
}

_running = false;
}

/**
* Gets a client by its client id.
* Params:
* clientId = The id of the client to retrieve.
* Returns:
* The client associated with the client id or null if no client is found by the id.
*/
auto getClient(size_t clientId) {
return _clients.get(clientId, null);
}

/// Gets all clients.
auto getClients() {
return _clients.values;
}

package(cheetah) {
/**
* Fires a socket event.
Expand Down Expand Up @@ -326,6 +363,19 @@ class SocketServer(T) {
void report(Exception error, SocketServer!T server = null, SocketClient!T client = null) {
fireEvent(SocketEventType.error, new SocketEventArgs!T(server, client, error));
}

/**
* Removes a client from the internal client storage.
* Params:
* clientId = The client id to remove.
*/
void removeClient(size_t clientId) {
if (!_storeClients) {
return;
}

_clients.remove(clientId);
}
}

private:
Expand All @@ -344,7 +394,7 @@ class SocketServer(T) {
}
catch (Throwable e) {
report(new Exception(e.toString()), this);

throw e;
}
}
Expand All @@ -363,6 +413,13 @@ class SocketServer(T) {
_copyErrorEvents ? _errorEvents : null
);

if (_storeClients) {
client.clientId = _nextClientId;
_nextClientId++;

_clients[client.clientId] = client;
}

client.fireEvent(SocketEventType.connect, client.eventArgs);

client.process();
Expand Down

0 comments on commit 9646fd4

Please sign in to comment.