Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yuesong-feng/pine
A C++ network library for study.
https://github.com/yuesong-feng/pine
concurrency cpp cpp-library epoll network server socket
Last synced: 4 days ago
JSON representation
A C++ network library for study.
- Host: GitHub
- URL: https://github.com/yuesong-feng/pine
- Owner: yuesong-feng
- Created: 2022-01-04T11:20:28.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-12-20T04:15:14.000Z (about 2 years ago)
- Last Synced: 2023-03-05T17:33:58.476Z (almost 2 years ago)
- Topics: concurrency, cpp, cpp-library, epoll, network, server, socket
- Language: C++
- Homepage:
- Size: 192 KB
- Stars: 85
- Watchers: 4
- Forks: 20
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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:
```cpp
#include
#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:
```cpp
#include "pine.h"
#includeint 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
```bash
mkdir build && cd build
cmake .. # for debug, add -DCMAKE_BUILD_TYPE=DEBUGmake format # optional
make cpplint # optional
make clang-tidy # optional
make
```build tests files, at `pine/build`
```bash
make build-tests
```Need to run your own tests? Write your program in "test/" directory, eg. server.cpp
```bash
# at pine/build
make server
./bin/server
```run the echo server
```bash
./bin/echo_server &
./bin/echo_client
```