Barracuda Application Server C/C++ Reference
Native APIs, integration guides, and platform interfaces
HttpServCon Struct Reference

Detailed Description

Create a server listen object.

The object binds itself to the SoDisp object and makes the web-server listen for new connections on the port specified. The default port is 80. You can create several of these objects and bind to the dispatcher if you want the web-server to listen to more than one port.

#include <HttpServCon.h>

Public Member Functions

 HttpServCon (HttpServer *server, SoDisp *dispatcher, U16 port=80, BaBool setIP6=FALSE, const void *interfaceName=0, HttpServCon_AcceptNewCon userDefinedAccept=0)
 Create a Server Connection. More...
 
BaBool isValid ()
 
int setPort (U16 portNumber, bool setIp6=false, const void *interfaceName=0)
 Open a replacement listening endpoint before closing the old one. More...
 
 ~HttpServCon ()
 Close and unregister the listener; accepted connections are separate. More...
 
 HttpServCon ()
 Uninitialized storage; initialize before use or destruction. More...
 

Constructor & Destructor Documentation

◆ HttpServCon() [1/2]

HttpServCon::HttpServCon ( HttpServer *  server,
SoDisp *  dispatcher,
U16  port = 80,
BaBool  setIP6 = FALSE,
const void *  interfaceName = 0,
HttpServCon_AcceptNewCon  userDefinedAccept = 0 
)

Create a Server Connection.

Parameters
serverBorrowed server, valid throughout listener use. Required for the default HTTP handler and for setPort.
dispatcherRequired borrowed dispatcher, valid throughout use.
portTCP port in host byte order, default 80. Zero requests an OS-assigned port if supported by the socket port.
setIP6TRUE selects IPv6, FALSE IPv4 (default). This parameter is ignored unless the underlying TCP/IP stack is a dual IP V4 and IP V6 stack.
interfaceNameBorrowed platform binding address/interface, used during construction; normally a NUL-terminated textual address. NULL binds the wildcard address. Accepted representation is port-specific.
userDefinedAcceptThe default (argument is NULL) is to accept connections for the web-server. A custom callback is required in NO_BA_SERVER builds; omitting it calls baFatalE.

A ServerConnection object is normally used for accepting new connections for the web-server. It is possible to redirect new connections to the "userDefinedAccept" callback function. This makes it possible to use the socket dispatcher logic in the web-server for implementing other services such as a telnet server.

The "user defined accept" callback function is called when a new connection is established. The HttpConnection object passed in as the argument to the callback function is a temporary object that will be destroyed as soon as the callback function returns. You must, therefore, move the connection into an initialized application object.

Example C code:

typedef struct { HttpConnection con; } MyHttpCon;
static void MyHttpCon_dispatchData(SoDispCon* socket)
{
MyHttpCon* o = (MyHttpCon*)socket;
char buf[512];
int len = HttpConnection_readData(&o->con, buf, sizeof(buf));
if(len < 0)
{
HttpConnection_destructor(&o->con); // Unregister and close.
baFree(o);
return;
}
if(len > 0)
{
// Consume exactly len bytes here; buf is not NUL-terminated.
}
}
static void MyHttpCon_myAccept(HttpServCon* listener,
HttpConnection* temporary)
{
MyHttpCon* o = (MyHttpCon*)baMalloc(sizeof(MyHttpCon));
(void)listener;
if(!o) return; // The listener closes the unclaimed socket.
HttpConnection_getServer(temporary), disp,
MyHttpCon_dispatchData);
HttpConnection_moveCon(temporary, &o->con);
SoDisp_addConnection(disp, (SoDispCon*)&o->con);
SoDisp_activateRec(disp, (SoDispCon*)&o->con);
}
#define HttpConnection_getServer(o)
Definition: HttpConnection.h:127
#define HttpConnection_getDispatcher(o)
Definition: HttpConnection.h:130
BA_API int HttpConnection_readData(HttpConnection *con, void *data, int len)
Read pending pushback bytes first, otherwise perform an event-oriented read.
BA_API int HttpConnection_moveCon(HttpConnection *o, HttpConnection *newCon)
Move the socket, transport state and pending input to another object.
BA_API void HttpConnection_constructor(HttpConnection *o, struct HttpServer *server, struct SoDisp *dispatcher, SoDispCon_DispRecEv e)
Initialize connection storage without opening a socket.
BA_API void HttpConnection_destructor(HttpConnection *o)
Release pushback storage, unregister events, and close the connection.
BA_API void SoDisp_activateRec(SoDisp *o, struct SoDispCon *con)
Enable receive events.
BA_API void SoDisp_addConnection(SoDisp *o, struct SoDispCon *con)
Register a connection without enabling events.
void * baMalloc(size_t size)
Allocate uninitialized storage using the target's configured allocator.
void baFree(void *p)
Release storage using the target's configured allocator.
Contains information about the physical socket connection.
Definition: HttpConnection.h:80
Create a server listen object.
Definition: HttpServCon.h:70
Contains information about the physical socket connection.
Definition: SoDispCon.h:120
The SoDisp dispatches any socket connection that contains data by calling the SoDispCon::execute memb...
Definition: SoDisp.h:91

◆ ~HttpServCon()

HttpServCon::~HttpServCon ( )

Close and unregister the listener; accepted connections are separate.

Does not destroy the borrowed server or dispatcher.

◆ HttpServCon() [2/2]

HttpServCon::HttpServCon ( )

Uninitialized storage; initialize before use or destruction.

Member Function Documentation

◆ isValid()

BaBool HttpServCon::isValid ( )
Returns
TRUE if the listen socket is currently valid, FALSE otherwise. Check after construction, which returns no status. Error messages are printed to HttpTrace.

◆ setPort()

int HttpServCon::setPort ( U16  portNumber,
bool  setIp6 = false,
const void *  interfaceName = 0 
)

Open a replacement listening endpoint before closing the old one.

Parameters
[in]portNumberNew TCP port, in host byte order.
[in]setIp6True selects IPv6, false IPv4 (default).
[in]interfaceNamePlatform binding address/interface, or NULL for wildcard; borrowed for this call only.
Returns
Zero on success, -1 if creating/binding/listening fails. The old listener remains on failure. Requires a non-NULL server and uses that server's dispatcher. Existing accepted connections are unaffected. Perform listener changes while holding the dispatcher mutex.