server.h reference

Module <cweb/server.h> — TCP server and libevent

Starts the HTTP server on libevent, manages the global event_base, and pending responses (async flow).

Macros / types

extern struct event_base *g_event_base

  • Purpose: Global libevent main loop object after successful initialization in cweb_run_server.

AUTOFREE_EVENT

  • Purpose: cleanup attribute for struct event* via cweb_cleanup_free_event.

typedef enum { FILESYSTEM, MEMORY, HYBRID } Mode

  • Note: Semantically overlaps with FileServerMode in fileserver.h; for new code prefer consistent use of FileServerMode from the file server header.

Server lifecycle

void cweb_run_server(const char *port)

  • Purpose: Creates event_base, initializes pending responses and speedbench, binds listener on IPv4 all interfaces, port from string (e.g. "8080"), runs event_base_dispatch (blocking until loop ends).
  • Parameters: port — decimal string.
  • Notes: Returns when the event loop ends; listener and base are then freed internally.

void cweb_cleanup_server(void)

  • Purpose: Cleanup after the main loop ends: pending responses, routes, file server, session store (no second event_base_free — that happens after dispatch in cweb_run_server).
  • Recommendation: Call before process exit after cweb_run_server.

Pending responses (async)

void cweb_add_pending_response(Request *req, Response *res, struct bufferevent *bev)

  • Purpose: Registers a response that cannot be sent yet (e.g. waiting on fetch); the framework retries later.

void cweb_cancel_pending_responses(struct bufferevent *bev)

  • Purpose: Removes pending entries for a connection (e.g. on disconnect).

void cweb_init_pending_responses(struct event_base *base) / void cweb_cleanup_pending_responses(void)

  • Purpose: Init/shutdown of pending system (cweb_run_server calls init/cleanup).

void cweb_send_response(struct bufferevent *bev, Request *req, Response *res)

  • Purpose: Serializes and writes the response to the connection.

Helpers

struct event_base *cweb_get_event_base(void)

  • Purpose: Access global base (for fetch_client_create, custom timers, …).

void cweb_cleanup_free_event(struct event **ev)

  • Purpose: For AUTOFREE_EVENT — calls event_free when pointer is set.

See also