C++ IP API
9.5.7
TCP & UDP for virtual user scripts
|
Manage IP connections and endpoints and send and receive data using the IP family of protocols from an eggPlant test script. Two protocols are supported: UDP and TCP.
Facilita::IpVirtualUser (inherits from VirtualUser) and Facilita::IpScript (inherits from VirtualUserScript) provide base classes for developing IP test scripts. Facilita::UdpLink and Facilita::TcpConnection instances provide peer to peer communications using the underlying network protocol. Links and Connections are created by factory objects within the Facilita::IpVirtualUser instances that are created as part of a test. There are distinct factory objects for UDP and TCP per IpVirtualUser instance, call the script's udp() or tcp() methods to access the relevant factory. Within test scripts (that inherit from Facilita::IpScript) the factory objects are used to create Links and Connections.
To send data to and receive data from a remote TCP peer at IP address 192.168.0.1 on IP port 60000:
TCP is a streaming protocol where the current position in the read data stream is maintained. As bytes are consumed methods the read position advances. No backtracking is allowed. Bytes received from the network but not yet read from the API are buffered. The Facilita::TcpConnection methods that consume data fall into three groups:
The property Buffersize specifies the maximum data that can be read in a single receive call. The discard* methods are not affected by Buffersize.
TCP connection classes come in two flavours Facilita::TcpConnection and a sub-class Facilita::TcpClientConnection. These classes have almost the same signature, Facilita::TcpClientConnection has the additional method connect. A TcpClientConnection is created in the above example. To create a server:
Making multiple simultaneous waitForConnection calls from the same engine process is supported. This enables a number of Virtual Users executing the same server style script to emulate a multi-threaded service. A single server thread per engine process listens for connection requests. This thread passes completed connections to the server-side Virtual User threads. Attempts by multiple engine processes (executing on the same machine) to make simultaneous Facilita::TcpConnectionFactory::waitForConnection() calls for the same address will result in "address in use" errors.
To create a directed UDP link and then exchange data:
The UDP classes support a maximum datagram size up to the limit of the underlying network, the current limit is given by the property Facilita::UdpLink::maxUdpSize. The UDP peer to peer links over which datagrams are received and sent are either directed or non-directed. Directed links reference the same peers throughout their life and are rather like TCP connections. Non-directed links represent a local endpoint that can handle incoming calls from variable remote peers.
UDP clients are created by calls on the IpVirtualUser UDP factory.
To avoid loss of data, each link has it's own thread dedicated to reading data from the associated remote peer.
The difference between a UDP "client" and "server" is that a server waits for the first message whilst the client initiates the conversation. A pool of Virtual Users running a server script can implement a multi-threaded service accessed via UDP. Here is how a link is created in a server script:
Server scripts wait for a link to be created and script instances, like the example above, are in a race to handle the next incoming message. The script instances for a server endpoint share a reader thread. This thread handles incoming messages and depending on the source address of the incoming message passes them to either: