Database Stream Class
Overview
Functions
Changes in 6.3 and 6.3.1
Overview
The gxStream class is derived from the gxSocket class and is used to send and receive database blocks over a TCP connection.
Description: The 32/64-bit gxDatabase engine is used to construct multi-platform database files. Since its inception additional communication components have been added to the library to support TCP/IP communications via TCP and UDP sockets. The additional communication components are part of sub-library named the gxSocket Library. By adding gxSocket components to existing gxDatabase applications you can read and write database blocks over a local or wide are network in real-time. gxSocket applications use the network to allow database application to communicate with each. Database blocks are contiguous bytes of binary data marked with a block header. The block header contain a block check word, the size of the block, the block status, and a pointer to the next deleted block (applicable to disk files only). When a database block is allocated a new block header is constructed and written to the database file directly before the space allocated for the block data. Using the gxSocket library extensions the same block header written to a disked-based database can be used to transmit a database block to a remote host. By design, gxDatabase files use an internal format for byte ordering allowing block headers to work transparently in a multi-platform environment.
Protocol Number: TCP & UDP
Message Formats: Block check-word, block length, and status code
Message Types: Request, reply, acknowledge, shutdown
Message opcodes: Add block, change block, delete remote block, client request failed, close client/server connection, shutdown client/server, request block, send raw data, acknowledge block
Message Sequences: When a client sends a block of raw data a block header precedes the data and informs the server of the block size and the block status. After reception of a device header the server waits for the block data and processes the data or signals/handles an event according to the status of the block.
Protocol functions: Communication via database block headers allows gxDatabase applications to share database blocks in a client/server relationship.
6.4: the block header goes on the wire in one canonical 32-byte form (the bytes 64-bit Linux always sent; DataReel 7.4 sends the same), on every platform, and so do gxDatagram and gxSerialCommServer. A sender that uses 4-byte words (a Windows or 32-bit build before 6.4) is refused, and PeerOldHeader() returns nonzero. A 6.4 peer does not talk to such a 6.3 or older peer: upgrade both ends together.
Functions
gxStream::gxStream()
gxStream::~gxStream()
gxStream::AddBlock()
gxStream::ChangeBlock()
gxStream::CloseConnection()
gxStream::DeleteBlock()
gxStream::HostName()
gxStream::PortNumber()
gxStream::ReadAckBlock()
gxStream::ReadBlock()
gxStream::ReadClientHeader()
gxStream::ReadHeader()
gxStream::ReadRemoteAckBlock()
gxStream::ReadRemoteBlock()
gxStream::RemoteHostName()
gxStream::RemotePortNumber()
gxStream::RequestBlock()
gxStream::SetBlockStatus()
gxStream::StreamClient()
gxStream::StreamServer()
gxStream::TerminateConnection()
gxStream::WriteAckBlock()
gxStream::WriteBlock()
gxStream::WriteHeader()
gxStream::WriteRemoteAckBlock()
gxStream::WriteRemoteBlock()
gxStream::gxStream()Default class constructor.
gxStream::~gxStream()Class destructor.
int gxStream::AddBlock(const void *block, gxBlockHeader &block_header)Public member function used to request that the block pointed to by the "block" variable be added by a local or remote device. The block header must account for the total length of the data pointed to by the "block" variable. Returns zero if no errors occur or a non-zero value to indicate a failure.
int gxStream::AddBlock(gxsSocket_t s, const void *block, gxBlockHeader &block_header)Public member function used to request that the block pointed to by the "block" variable be added by the specified socket descriptor. The block header must account for the total length of the data pointed to by the "block" variable. Returns zero if no errors occur or a non-zero value to indicate a failure.
int gxStream::ChangeBlock(const void *request, const void *block, gxBlockHeader &request_header, gxBlockHeader &block_header)Public member function used to request that the block pointed to by the "block" variable be changed by a local or remote device. The "request" variable points to the new information that will be inserted in place of the block pointed to by the "block" variable. The block header must account for the total length of the data pointed to by the "block" variable and the "request_header" must account for the total length of the data pointed to by the "request" variable. Returns zero if no errors occur or a non-zero value to indicate a failure.
int gxStream::ChangeBlock(gxsSocket_t s, const void *request, const void *block, gxBlockHeader &request_header,gxBlockHeader &block_header)Public member function used to request that the block pointed to by the "block" variable be changed by the specified socket descriptor. The "request" variable points to the new information that will be inserted in place of the block pointed to by the "block" variable. The block header must account for the total length of the data pointed to by the "block" variable and the "request_header" must account for the total length of the data pointed to by the "request" variable. Returns zero if no errors occur or a non-zero value to indicate a failure.
int gxStream::CloseConnection()Public member function that sends a block command used to close a persistent client or server connection. Returns zero if no errors occur or a non-zero value to indicate a failure.
int gxStream::CloseConnection(gxsSocket_t s)Public member function that sends a block command to the specified socket descriptor used to close a persistent client or server connection. Returns zero if no errors occur or a non-zero value to indicate a failure.
int gxStream::DeleteBlock(const void *request, gxBlockHeader &request_header)Public member function used to request that the a block pointed to by the "request" variable be deleted. The "request_header" must account for the total length of the data pointed to by the "request" variable. Returns zero if no errors occur or a non-zero value to indicate a failure.
int gxStream::DeleteBlock(gxsSocket_t s, const void *request,gxBlockHeader &request_header)Public member function used to request that the a block pointed to by the "request" variable be deleted by the specified socket descriptor. The "request_header" must account for the total length of the data pointed to by the "request" variable. Returns zero if no errors occur or a non-zero value to indicate a failure.
int gxStream::HostName(char *hs)Public member function that passes back the host name of this machine in the "hs" variable. NOTE: A memory buffer equal to gxsMAX_NAME_LEN must be pre-allocated prior to using this function. Returns zero if no errors occur or a non-zero value to indicate a failure.
int gxStream::PortNumber(int &port)Public member function that passes back the port number actually set by the system in the "port" variable. Returns zero if no errors occur or a non-zero value to indicate a failure.
int gxStream::ReadAckBlock()Public member function used to read an acknowledgment block. Returns zero if no errors occur or a non-zero value to indicate a failure.
int gxStream::ReadAckBlock(gxsSocket_t s)Public member function used to read an acknowledgment block from the specified socket descriptor. Returns zero if no errors occurs or a non-zero value to indicate a failure.
int gxStream::ReadBlock(gxsSocket_t s, void *buf, gxBlockHeader &gx)Public member function used to read a variable block from the specified socket descriptor. The block header must account for the total length of the data pointed to by the "buf" variable. Returns zero if no errors occur or a non-zero value to indicate a failure.
int gxStream::ReadClientHeader(gxBlockHeader &gxs)Public member function used to read a client header. Returns zero if no errors occur or a non-zero value to indicate a failure.
int gxStream::ReadHeader(gxsSocket_t s, gxBlockHeader &gx)Public member function used to read a device block header from the specified socket descriptor. Returns zero if no errors occur or a non-zero value to indicate a failure.
int gxStream::ReadRemoteAckBlock()Public member function used to read a client acknowledgment block. Returns zero if no errors occur or a non-zero value to indicate a failure.
int gxStream::ReadRemoteBlock(void *buf, gxBlockHeader &gx)Public member function used to read a remote block. The block header must account for the total length of the data pointed to by the "buf" variable. Returns zero if no errors occur or a non-zero value to indicate a failure.
int gxStream::RemoteHostName(char *hs)Public member function that passes back the host name of the client in the "hs" variable. NOTE: A memory buffer for "hs" equal to gxsMAX_NAME_LEN must be pre-allocated prior to using this function. Returns zero if no errors occur or a non-zero value to indicate a failure.
int gxStream::RemotePortNumber(int &port)Public member function that passes back the client's port number in the "port" variable. Returns zero if no errors occur or a non-zero value to indicate a failure.
void *gxStream::RequestBlock(const void *request, gxBlockHeader &request_header, gxBlockHeader &requested_block_header)Public member function used to request a block from the remote end. The "request" variable points to the request information. The "request_header" must account for the total length of the data pointed to by the "request" variable. Returns the requested block or a null value if an error occurs. The block returned by this function must be typecast to the correct type by the calling function.
void *gxStream::RequestBlock(gxsSocket_t s, const void *request, gxBlockHeader &request_header,gxBlockHeader &requested_block_header)Public member function used to request a block from the specified socket descriptor. The "request" variable points to the request information. The "request_header" must account for the total length of the data pointed to by the "request" variable. Returns the requested block or a null value if an error occurs. The block returned by this function must be typecast to the correct type by the calling function.
void gxStream::SetBlockStatus(gxBlockHeader &gx, __SBYTE__ net_status,__SBYTE__ file_status = gxRemoteDeviceBlock)Public member function used to set the variable block header status member control byte. NOTE: The block's next deleted variable must be set, if needed, after a call to this function. The block length will not be set here.
int gxStream::StreamClient(int port, char *host)Public member function used to connect a stream client to the specified host. Returns zero if no errors occur during initialization or a non-zero value to indicate a failure.
int gxStream::StreamServer(int port, int max_connections = SOMAXCONN)Public member function used to initialize a stream server. Returns zero if no errors occur during initialization or a non-zero value to indicate a failure.
int gxStream::TerminateConnection()Public member function used to send a block command that will shutdown a server or client. Returns zero if no errors occur or a non-zero value to indicate a failure.
int gxStream::TerminateConnection(gxsSocket_t s)Public member function used to send a block command to the specified socket descriptor that will shutdown a server or client. Returns zero if no errors occur or a non-zero value to indicate a failure.
int gxStream::WriteAckBlock()Public member function used to write an acknowledgment block. Returns zero if no errors occur or a non-zero value to indicate a failure.
int gxStream::WriteAckBlock(gxsSocket_t s)Public member function used to write an acknowledgment block to the specified socket descriptor. Returns zero if no errors occur or a non-zero value to indicate a failure.
int gxStream::WriteBlock(const void *buf, int bytes)Public member function used to write a block of raw data. Returns zero if no errors occur or a non-zero value to indicate a failure.
int gxStream::WriteBlock(gxsSocket_t s, const void *buf, int bytes)Public member function used to write a block of raw data to the specified socket descriptor. Returns zero if no errors occur or a non-zero value to indicate a failure.
int gxStream::WriteBlock(gxsSocket_t s, const void *buf, gxBlockHeader &gx)Public member function used to write a variable block to the specified socket descriptor. The block header must account for the total length of the data pointed to by the "buf" variable. Returns zero if no errors occur or a non-zero value to indicate a failure.
int gxStream::WriteHeader(gxsSocket_t s, gxBlockHeader &gx)Public member function used to write a block header to the specified socket descriptor. Returns zero if no errors occur or a non-zero value to indicate a failure.
int gxStream::WriteRemoteAckBlock()Public member function used to write an acknowledgment block to the remote end. Returns zero if no errors occur or a non-zero value to indicate a failure.
int gxStream::WriteRemoteBlock(void *buf, int bytes)Public member function used to write a block of raw data to the remote end. Returns zero if no errors occur or a non-zero value to indicate a failure.
Changes in 6.3 and 6.3.1
Behaviour change in 6.3.1: the networked object database protocol had no authentication. A gxStream server now needs TLS with client certificates: StreamServer(), Accept() and AcceptClient() return gxSOCKET_SSL_ERROR unless the server has a server certificate, a client CA file and at least one allow list entry, or the program calls AllowAnonymousClients(true) (an open server: plain TCP as in 6.2, or TLS without client certificates). Clients are unchanged unless they call UseTLS(). In TLS mode a failed handshake or certificate check closes the socket, and the block functions refuse a socket without a TLS connection, so nothing is sent in cleartext. Block lengths sent by the peer are checked before memory is allocated or data is read, and negative or over-INT_MAX writes are refused. See the socket_cache example (--cert/--key, --client-ca, --allow-fingerprint, --allow-cn, --open, --allow-remote-shutdown; client --ca, --insecure, --cert/--key).
int gxStream::SetServerCert(const char *cert_file, const char *key_file, const char *passphrase = 0)Server certificate and private key (PEM; with Schannel also a .pfx/.p12 file with an empty key file name).
void gxStream::SetClientCAFile(const char *fname)PEM file of the CAs that issue client certificates. Every client must send a certificate that chains to one of them.
int gxStream::AllowClientFingerprint(const char *fp) / int gxStream::AllowClientCN(const char *cn) / void gxStream::ClearClientAllowList()Allow list: a client is accepted if its certificate SHA-256 fingerprint (32 hex bytes, colons optional, the openssl -fingerprint -sha256 output works) or its subject CN (exact) is listed.
void gxStream::AllowAnonymousClients(bool allow = true) / int gxStream::AnonymousClientsAllowed()The explicit opt-out: accept clients without certificates. A CA file or allow list that is set is still enforced.
gxsSocket_t gxStream::Accept() / int gxStream::AcceptClient(gxStream &client)Accept a client, run the TLS handshake and the client checks; AcceptClient() gives each client its own gxStream (one per thread).
const char *gxStream::PeerCN() / const char *gxStream::PeerFingerprint() / int gxStream::ClientAuthenticated() / const char *gxStream::TLSError()The accepted client's certificate CN and fingerprint, whether it was authenticated, and why the last TLS or client check failed.
void gxStream::UseTLS(int on = 1) / int gxStream::SetClientCert(const char *cert_file, const char *key_file, const char *passphrase = 0)Client side: use TLS, and send a client certificate.
void gxStream::SetServerCAFile(const char *fname) / void gxStream::SetServerName(const char *name) / void gxStream::SetVerifyServer(int on = 1)Client side: CA for the server certificate (the system store without it), the expected host name, and turning the server check off (testing only).
int gxStream::IsTLS() / int gxStream::IsSecure() / int gxStream::IsRemoteSecure() / void gxStream::SetHandshakeTimeout(int seconds)TLS mode set, this connection or the accepted client on TLS, and the handshake time limit.
void gxStream::SetMaxBlockSize(gxUINT32 bytes) / gxUINT32 gxStream::MaxBlockSize()Largest block length accepted from the peer (gxSTREAM_DEF_MAX_BLOCK, 64 MB, by default; at most INT_MAX).
int gxStream::ReadBlock(gxsSocket_t s, void *buf, int buf_size, gxBlockHeader &gx, int seconds, int useconds) / int gxStream::ReadRemoteBlock(void *buf, int buf_size, gxBlockHeader &gx)Read a block only if it fits in buf_size bytes; otherwise nothing is read and gxSOCKET_BLOCKSIZE_ERROR is returned. Use these instead of the forms without a size.
int gxStream::WriteResultBlock64(FAU_t result_code) / int gxStream::ReadResultBlock64(FAU_t &result_code)Result blocks that keep a 64-bit file address (the int forms truncate). Remote forms: WriteRemoteResultBlock64(), ReadRemoteResultBlock64().
void gxStream::Close() / void gxStream::CloseRemoteSocket()Send close_notify, half-close and drain briefly before closing (TLS).