frannecki / ladder

A light-weight cross-platform event-driven web server
BSD 3-Clause "New" or "Revised" License
1 stars 1 forks source link
cpp epoll https iocp kqueue network reactor server

Ladder: A Light-weight Event-driven Server

Ladder is a light-weight web server framework developed for studying purposes. It conforms to the Reactor design patterns and leverages Linux epoll / BSD kqueue / Windows IOCP for I/O multiplexing or asynchronous IO.

Required Dependencies

Supported Platforms

Usage

Building

Buiding scripts with cmake and bazel are provided for Linux/FreeBSD. You can refer to msvc for its usage on Windows.

Usage

Start a ladder server

#include <functional>
#include <string>

#ifdef _MSC_VER
#pragma comment(lib, "ws2_32.lib")
#endif

#include <ladder/Buffer.h>
#include <ladder/Connection.h>
#include <ladder/TcpServer.h>
#include <ladder/Logging.h>

using namespace ladder;
using namespace std::placeholders;

void OnMessage(const ConnectionPtr& conn, Buffer* buffer) {
  std::string message = buffer->ReadAll();
  LOG_INFO("Received: " + message);
  conn->Send("Echoback~ " + message);
}

void OnConnection(const ConnectionPtr& conn) {
  conn->Send("Hello -- This is a ladder server.");
}

int main(int argc, char** argv) {
#ifdef _MSC_VER
  WSADATA wsa_data;
  if (WSAStartup(MAKEWORD(2, 2), &wsa_data) != NO_ERROR) {
    return -1;
  }
#endif
  SocketAddr addr("0.0.0.0", 8070, false);
  TcpServer server(addr, false);
  server.SetConnectionCallback(std::bind(OnConnection, _1));
  server.SetReadCallback(std::bind(OnMessage, _1, _2));
  server.Start();
#ifdef _MSC_VER
  WSACleanup();
#endif
  return 0;
}

You can refer to the test demo for more details.