Into

Modules

Documentation

classPiiNetworkServer

#include <PiiNetworkServer.h>

An implementation of a threaded network server.

Inherits QObject, PiiNetworkServerThread::Controller

Inherited by PiiLocalServer, PiiTcpServer

Description

This class provides a framework for server processes that handle incoming connections by creating a worker thread for each. PiiNetworkServer is designed to work with any native socket type. Subclasses provide implementations for TCP servers and local socket servers.

See also

Properties

The message sent to the client when the server cannot handle more connections.

int

The number of pending connection attempts to keep.

int

The maximum number of worker threads ever running concurrently.

int

Minimum number of worker threads always available.

The address the server binds to.

int

The time (in milliseconds) a worker thread is allowed to be idle before dying.

Constructors and destructor

Initialize the server with a communication protocol.

Interrupts all open connections and destroys the server.

Public member functions

int
int
int
( )

Get the communication protocol.

virtual QString
( )  = 0
void
( )
void
(
  • int maxPendingConnections
)
void
(
  • int maxWorkers
)
void
(
  • int minWorkers
)
virtual bool
( )  = 0

Set the server's bind address.

void
(
  • int workerMaxIdleTime
)
virtual bool
( )

Starts the server.

virtual void
(
  • PiiNetwork::StopMode mode = PiiNetwork::WaitClients
)

Sends a stop signal to the server.

virtual void

Called by the thread when the protocol has done its work.

virtual void

Called by the thread when it is about to finish.

int

Protected member functions

Create a new worker thread.

void

Handle an incoming connection.

Initialize the server with a communication protocol.

virtual void
( )

Called by the server when maxWorkers threads are running, and maxPendingConnections connection attempts have been already queued when a new connection attempt comes in.

Property details

  • QString busyMessage

    [read, write]

    The message sent to the client when the server cannot handle more connections.

    The default value is "Server busy\ ".

  • int maxPendingConnections

    [read, write]

    The number of pending connection attempts to keep.

    If the server cannot create new worker threads due to maxWorkers limitation, it will place the connection into a list of pending connections. Keep this value low to avoid client timeouts without any response. The default value is 0.

  • int maxWorkers

    [read, write]

    The maximum number of worker threads ever running concurrently.

    If the number of concurrent connections exceeds this value, subsequent connection attempts will be stored as pending connections. The upper bound for this value is 1000. The default value is 10.

    maxPendingConnections

  • int minWorkers

    [read, write]

    Minimum number of worker threads always available.

    Keeping a reasonable number of idle threads available increases the server's performance under load. The default value is 0.

  • QString serverAddress

    [read, write]

    The address the server binds to.

    The format of the address depends on the server type. PiiTcpServer uses a combination of IP address and port number, PiiLocalServer users the name of a socket or a pipe.

  • int workerMaxIdleTime

    [read, write]

    The time (in milliseconds) a worker thread is allowed to be idle before dying.

    If a worker thread has not been activated within this time, and more than minWorkers worker threads are alive, the thread will be destroyed. The default value is 10000.

Function details

  • PiiNetworkServer

    ()
    [protected]

    Initialize the server with a communication protocol.

    Parameters
    protocol

    The protocol used for communication. If the protocol is stateless, it will be shared among all worker threads. A stateful protocol will be cloned for each worker. The protocol must be non-zero and it must remain valid during the lifetime of the server.

  • ~PiiNetworkServer

    ()

    Interrupts all open connections and destroys the server.

  • QString busyMessage

    ()
  • int maxPendingConnections

    ()
  • int maxWorkers

    ()
  • int minWorkers

    ()
  • PiiNetworkProtocol * protocol

    ()

    Get the communication protocol.

  • virtual QString serverAddress

    ()
    [pure virtual]
  • void setBusyMessage

    ()
  • void setMaxPendingConnections

    (
    • int maxPendingConnections
    )
  • void setMaxWorkers

    (
    • int maxWorkers
    )
  • void setMinWorkers

    (
    • int minWorkers
    )
  • virtual bool setServerAddress

    ()
    [pure virtual]

    Set the server's bind address.

    Parameters
    serverAddress

    the address in an implementation-dependent format.

    Returns

    true if the address was successfully set, false otherwise.

  • void setWorkerMaxIdleTime

    (
    • int workerMaxIdleTime
    )
  • virtual bool start

    ()
    [virtual]

    Starts the server.

    This function initializes the server's state for execution. A typical overridden implementation makes the low-level server implementation to start listening to a socket after calling this function.

    Returns

    true if the server was successfully started, false otherwise. The default implementation always returns true.

    See also
    • QTcpServer::listen()

  • virtual void stop

    (
    • PiiNetwork::StopMode mode = PiiNetwork::WaitClients
    )
    [virtual]

    Sends a stop signal to the server.

    This function stops all worker threads and waits until they are done. Subclasses may implement any additional functionality related to stopping the server. Typically, the overridden first calls this function and then closes the low-level server interface.

    See also
    • QTcpServer::close()

  • virtual void threadAvailable

    ()
    [virtual]

    Called by the thread when the protocol has done its work.

    Reimplemented from PiiNetworkServerThread::Controller.

  • virtual void threadFinished

    ()
    [virtual]

    Called by the thread when it is about to finish.

    The thread finishes if it has not been assigned new clients for a while, or stop() has been called. Use the #setTimeOut() function to change the maximum wait time.

    Reimplemented from PiiNetworkServerThread::Controller.

  • int workerMaxIdleTime

    ()
  • virtual PiiNetworkServerThread * createWorker

    ()
    [protected, virtual]

    Create a new worker thread.

    Subclasses may override this function to set thread priorities etc. The default implementation creates a new PiiNetworkServerThread.

    Parameters
    protocol

    the protocol used for communication. If the protocol is stateless, PiiNetworkServer retains its ownership. If it is stateful, the passed pointer is a clone that must be deleted by whoever will finally take the pointer. Usually, the pointer is just passed to the constructor of PiiNetworkServerThread, which automatically takes care of deleting the pointer.

  • void incomingConnection

    ()
    [protected]

    Handle an incoming connection.

    This function tries the following, in the order of precedence:

  • Pick an idle worker thread to handle the connection.

  • Create a new thread with createWorker().

  • Put the connection to the list of pending connections.

  • Call serverBusy().

  • The function is called by subclasses that handle the conversion from their native socket descriptor format to PiiGenericSocketDescriptor.

  • virtual void serverBusy

    ()
    [protected, virtual]

    Called by the server when maxWorkers threads are running, and maxPendingConnections connection attempts have been already queued when a new connection attempt comes in.

    Subclasses implement this function to provide a protocol-specific response.

    The default implementation sends a user-specified message (UTF-8 encoded) to the device returned by createSocket().

    See also
    • #setBusyMessage()

  • Notes (0)

    Add a note

    Not a single note added yet. Be the first, add yours.