Git Product home page Git Product logo

pine-1's Introduction

pine

A high-performance and easy-to-use C++ network library for study.

Now this is just a toy library for education purpose, do not use in production.

example

An echo server:

#include <iostream>
#include "pine.h"

int main() {
  TcpServer *server = new TcpServer();

  Signal::signal(SIGINT, [&] {
    delete server;
    std::cout << "\nServer exit!" << std::endl;
    exit(0);
  });

  server->onConnect([](Connection *conn) { std::cout << "New connection fd: " << conn->socket()->fd() << std::endl; });

  server->onRecv([](Connection *conn) {
    std::cout << "Message from client " << conn->read_buf()->c_str() << std::endl;
    conn->Send(conn->read_buf()->c_str());
  });

  server->Start();

  delete server;
  return 0;
}

An echo client:

#include "pine.h"
#include <iostream>

int main() {
  Socket *sock = new Socket();
  sock->Create();
  sock->Connect("127.0.0.1", 1234);

  Connection *conn = new Connection(sock->fd(), nullptr);

  while (true) {
    std::string input;
    std::getline(std::cin, input);
    conn->set_send_buf(input.c_str());
    conn->Write();
    if (conn->state() == Connection::State::Closed) {
      conn->Close();
      break;
    }
    conn->Read();
    std::cout << "Message from server: " << conn->read_buf()->c_str() << std::endl;
  }

  delete conn;
  delete sock;
  return 0;
}

An HTTP web server:

build & run

build the pine library

mkdir build && cd build
cmake .. # for debug, add -DCMAKE_BUILD_TYPE=DEBUG

make format      # optional
make cpplint      # optional
make clang-tidy  # optional
make

build tests files, at pine/build

make build-tests

Need to run your own tests? Write your program in "test/" directory, eg. server.cpp

# at pine/build
make server
./bin/server

run the echo server

./bin/echo_server &
./bin/echo_client

pine-1's People

Contributors

chan1919 avatar loryhndol avatar yuesong-feng 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.