Giter Club home page Giter Club logo

fdstreambuf's Introduction

fdstreambuf

Provides C++ stream compatiliby for POSIX file descriptors

Important: Stream doesn't own file descriptor, so it won't close the descriptor or flush data implicitly when beeing destructed. User must ensure that before close buffer is synced, as shown in examples.

Usage

File I/O:

#include <algorithm>
#include <cassert>
#include <iterator>
#include <fcntl.h>

#define Fdstreambuf_Implementation
#include <fdstreambuf.hh>

void copy(char const* source_file, char const* destination_file)
{
  auto const ensure = [](int fd) { if (fd < 0) { perror("copy: "); exit(1); } return fd; };

  fdstreambuf out = ensure(open(destination_file, O_WRONLY | O_CREAT, 0644));
  fdstreambuf in = ensure(open(source_file, O_RDONLY));

  std::copy(
    std::istreambuf_iterator<char>(&in), {},
    std::ostreambuf_iterator<char>(&out));

  out.sync();

  close(out.fd);
  close(in.fd);
}

int main()
{
  copy(__FILE__, __FILE__ ".1");
}

Simple HTTP server:

#include <ostream>

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define Fdstreambuf_Implementation
#include <fdstreambuf.hh>

std::string_view Http_Header =
  "HTTP/1.1 200 OK\r\n"
  "Server: fdstreambuf example HTTP server\r\n"
  "Content-Type: text/html\r\n"
  "Connection: Closed\r\n"
  "\r\n";

void http()
{
  auto const ensure = [](int v) { if (v < 0) { perror("http: "); exit(1); } return v; };

  auto const sock = ensure(socket(AF_INET, SOCK_STREAM, 0));

  struct sockaddr_in sa;
  sa.sin_family = AF_INET;
  sa.sin_addr.s_addr = inet_addr("127.0.0.1");
  sa.sin_port = htons(8080);

  ensure(bind(sock, (struct sockaddr*)&sa, sizeof(sa)));
  ensure(listen(sock, 5));

  struct sockaddr incoming;
  socklen_t incoming_len = sizeof(incoming);
  for (size_t connections = 0;;) {
    fdstreambuf client = ensure(accept(sock, &incoming, &incoming_len));
    std::ostream response(&client);

    std::copy(Http_Header.begin(), Http_Header.end(), std::ostreambuf_iterator(&client));
    response << "<h1> Hello world! </h1>\n";
    response << "<p> This message has been send " << connections++ << " times </p>\n";

    auto status = client.sync();
    assert(status == 0);
    close(client.fd);
  }

  close(sock);
}

fdstreambuf's People

Contributors

robertbendun avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.