Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/poisonousjohn/net
Simple network protocols wrapper. Supported protocols: http (via curl)
https://github.com/poisonousjohn/net
Last synced: 5 days ago
JSON representation
Simple network protocols wrapper. Supported protocols: http (via curl)
- Host: GitHub
- URL: https://github.com/poisonousjohn/net
- Owner: PoisonousJohn
- License: mit
- Created: 2014-08-30T12:16:07.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2015-05-02T19:55:57.000Z (over 9 years ago)
- Last Synced: 2023-03-22T20:42:37.758Z (over 1 year ago)
- Language: C++
- Homepage:
- Size: 230 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: COPYING
Awesome Lists containing this project
README
net
===Simple network protocols wrapper. Supported protocols: http (via curl)
## Thread-safety
Transport is thread safe##Requirements
- C++11
- cURL (current implentation works via libcurl transport for now, but it's easy to implement other transports)##Simple example
```c++
#include
#include "net/http/curl/Curl.h"
#include "net/http/Listener.h"using namespace poison::net::http;
class HTTPListener : public Listener {
public:void onRequestComplete(const Response &resp) override {
std::cout << "from listener\n";
std::cout << "got response\ncode: " << resp.code << "\nerror: " << int(resp.error) << "\nerrorText: " << resp.errorText
<< "data: " << resp.data
<< std::endl;}
};
int main(int argc, const char * argv[])
{
Curl http;// transport supports listeners
HTTPListener listener;
http.addListener(&listener);Request::RequestData get;
Request::RequestData post;post["test"] = "test";
bool stop = false;
auto respHandler = [&]( const Response& resp ){
std::cout << "got response\ncode: " << resp.code << "\nerror: " << int(resp.error) << "\nerrorText: " << resp.errorText
<< "data: " << resp.data
<< std::endl;stop = true;
};// do request asynchronous (every request will be called on a separate thread)
// also you can provide callback
Request req("http://google.com", get, post);
// run synchronously
auto resp = http.send(req);
respHandler(resp);
stop = false;
// run asynchronously
http.send(req, respHandler);// you should call update in your event loop
// update will invoke callbacks and notify listeners
while( !stop ) {
http.update();
}return 0;
}
```