Manages all low-level networking required in maintaining a network server and keeps track of all currently established connections. More...
#include <NetworkServer.h>


Public Member Functions | |
| ~NetworkServer () | |
| When destroyed, the NetworkServer closes all the server connections it has. | |
| void | RunModalServer () |
| Enters a stand-alone processing loop that manages incoming connections until server is shut down. | |
| void | SetAcceptNewConnections (bool acceptNewConnections) |
| Enables or disables whether new connection attempts are allowed. | |
| void | SetStealthMode (bool stealthModeEnabled) |
| Enables or disables whether rejected connection attempts are messages back to the client (UDP only). | |
| void | ReadUDPSocketData (Socket *listenSocket) |
| If the server is running in UDP mode, the listenSocket is the socket that receives all application data. | |
| void | Process () |
| Handles all new connection attempts and pulls in new messages from all existing connections. | |
| void | BroadcastMessage (const NetworkMessage &msg, MessageConnection *exclude=0) |
| Broadcasts the given message to all currently active connections, except for the single 'exclude' connection. | |
| void | BroadcastMessage (unsigned long id, bool reliable, bool inOrder, unsigned long priority, unsigned long contentID, const char *data, size_t numBytes, MessageConnection *exclude=0) |
| Creates a NetworkMessage structure with the given data and broadcasts it to all currently active connections, except to the given excluded connection. | |
| void | SendMessage (const NetworkMessage &msg, MessageConnection &destination) |
| Sends the given message to the given destination. | |
| void | DisconnectAllClients () |
| Starts a benign disconnection procedure for all clients (write-closes each connection). | |
| void | Close (int disconnectWaitMilliseconds) |
| Forcibly closes down the server. | |
| void | ConnectionClosed (MessageConnection *connection) |
| Forcibly erases the given connection from the active connection list. The client will be left to time out. | |
| std::vector< Socket * > & | ListenSockets () |
| Returns all the sockets this server is listening on. | |
| ConnectionMap | GetConnections () |
| Returns all the currently tracked connections. | |
| std::string | ToString () const |
| Returns a one-liner textual summary of this server. | |
Manages all low-level networking required in maintaining a network server and keeps track of all currently established connections.
NetworkServer has the
| void kNet::NetworkServer::RunModalServer | ( | ) |
Enters a stand-alone processing loop that manages incoming connections until server is shut down.
References Process(), and kNet::Clock::Sleep().
| void kNet::NetworkServer::SetStealthMode | ( | bool | stealthModeEnabled | ) |
Enables or disables whether rejected connection attempts are messages back to the client (UDP only).
i.e. whether to message "Connection rejected" back to the peer.
| void kNet::NetworkServer::ReadUDPSocketData | ( | Socket * | listenSocket | ) |
If the server is running in UDP mode, the listenSocket is the socket that receives all application data.
This function pulls all new data from the socket and sends it to MessageConnection instances for deserialization and processing.
References kNet::Socket::BeginReceive(), kNet::Socket::EndReceive(), kNet::EndPoint::FromSockAddrIn(), LOG, and kNet::Socket::ToString().
| void kNet::NetworkServer::Process | ( | ) |
Handles all new connection attempts and pulls in new messages from all existing connections.
Periodically call this function to update the NetworkServer object.
References kNet::Network::AssignConnectionToWorkerThread(), kNet::Socket::Connected(), kNet::ConnectionOK, kNet::EndPoint::FromSockAddrIn(), kNet::WaitFreeQueue< T >::Front(), LOG, kNet::INetworkServerListener::NewConnectionEstablished(), kNet::WaitFreeQueue< T >::PopFront(), Ptr, kNet::Socket::ToString(), and kNet::Socket::TransportLayer().
Referenced by RunModalServer().
| void kNet::NetworkServer::BroadcastMessage | ( | const NetworkMessage & | msg, | |
| MessageConnection * | exclude = 0 | |||
| ) |
Broadcasts the given message to all currently active connections, except for the single 'exclude' connection.
If exclude is 0, all clients will receive the message.
| msg | The message to send. | |
| exclude | The network connection to exclude from the recipient list. All other clients connected to this server will get the message. This parameter is useful when you are implementing a server that relays messages between clients and want to avoid "echoing" the original message back to the client who sent it. |
References LOG, and SendMessage().
| void kNet::NetworkServer::BroadcastMessage | ( | unsigned long | id, | |
| bool | reliable, | |||
| bool | inOrder, | |||
| unsigned long | priority, | |||
| unsigned long | contentID, | |||
| const char * | data, | |||
| size_t | numBytes, | |||
| MessageConnection * | exclude = 0 | |||
| ) |
Creates a NetworkMessage structure with the given data and broadcasts it to all currently active connections, except to the given excluded connection.
References kNet::NetworkMessage::contentID, kNet::NetworkMessage::data, kNet::MessageConnection::EndAndQueueMessage(), kNet::NetworkMessage::inOrder, kNet::MessageConnection::IsWriteOpen(), LOG, kNet::NetworkMessage::priority, kNet::NetworkMessage::reliable, and kNet::MessageConnection::StartNewMessage().
| void kNet::NetworkServer::DisconnectAllClients | ( | ) |
Starts a benign disconnection procedure for all clients (write-closes each connection).
Also calls SetAcceptNewConnections(false), since this function is intended to be used when the server is going down. It can take an indefinite time for the connections to bidirectionally close, since it is up to the individual clients to write-close their end of the connections. This function returns immediately.
References LOG, and SetAcceptNewConnections().
Referenced by Close().
| void kNet::NetworkServer::Close | ( | int | disconnectWaitMilliseconds | ) |
Forcibly closes down the server.
Calls SetAcceptNewConnections(false) so that no new connections are accepted.
| disconnectWaitMilliseconds | If >0, this function calls DisconnectAllClients() and waits for the given amount of time until calling Close() on each client connection. If 0, MessageConnection::Close(0) will be immediately called on each client. In that case, this function returns immediately and all active clients will be left to time out at their end. Calling this function does not guarantee that all outbound data will be received by the peers. |
References DisconnectAllClients(), GetConnections(), LOG, and kNet::Clock::Sleep().
| std::string kNet::NetworkServer::ToString | ( | ) | const |
Returns a one-liner textual summary of this server.
1.7.1