[DEV] rework ENET
This commit is contained in:
@@ -6,10 +6,19 @@
|
||||
|
||||
#include <test-debug/debug.h>
|
||||
#include <enet/Tcp.h>
|
||||
#include <enet/TcpClient.h>
|
||||
#include <enet/Http.h>
|
||||
#include <etk/etk.h>
|
||||
|
||||
#include <etk/stdTools.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace appl {
|
||||
void onReceiveData(enet::Http& _interface, std::vector<uint8_t>& _data) {
|
||||
TEST_INFO("Receive Datas : " << _data.size() << " bytes");
|
||||
TEST_INFO("data:" << (char*)&_data[0] << "");
|
||||
}
|
||||
}
|
||||
|
||||
int main(int _argc, const char *_argv[]) {
|
||||
etk::init(_argc, _argv);
|
||||
@@ -27,35 +36,25 @@ int main(int _argc, const char *_argv[]) {
|
||||
TEST_INFO("== Test HTTP client ==");
|
||||
TEST_INFO("==================================");
|
||||
#ifndef __TARGET_OS__Windows
|
||||
// client mode ...
|
||||
enet::Http connection;
|
||||
connection.setServer("127.0.0.1");
|
||||
// connect on TCP server:
|
||||
enet::Tcp tcpConnection = std::move(enet::connectTcpClient("127.0.0.1", 12345));
|
||||
// TODO : Check if connection is valid ...
|
||||
|
||||
// Create a HTTP connection in Client mode
|
||||
enet::Http connection(std::move(tcpConnection), false);
|
||||
connection.setKeepAlive(true);
|
||||
TEST_INFO("----------------------------");
|
||||
TEST_INFO("GET data : ");
|
||||
if (connection.get("") == false) {
|
||||
TEST_ERROR("can not GET data...");
|
||||
return -1;
|
||||
}
|
||||
TEST_INFO("data : " << connection.dataString());
|
||||
// Set callbacks:
|
||||
connection.connect(appl::onReceiveData);
|
||||
|
||||
TEST_INFO("----------------------------");
|
||||
TEST_INFO("POST data : ");
|
||||
std::map<std::string, std::string> values;
|
||||
values.insert(std::make_pair<std::string, std::string>("plop", "valuePlop"));
|
||||
if (connection.post("", values) == false) {
|
||||
TEST_ERROR("can not POST data...");
|
||||
return -1;
|
||||
}
|
||||
TEST_INFO("data : " << connection.dataString());
|
||||
// start http connection (the actual state is just TCP start ...)
|
||||
connection.start();
|
||||
connection.get("plop.txt");
|
||||
|
||||
TEST_INFO("----------------------------");
|
||||
TEST_INFO("POST xml : ");
|
||||
if (connection.post("", /*"application/xml"*/ "text/xml; charset=utf-8", "<plop><string>value1</string></plop>") == false) {
|
||||
TEST_ERROR("can not POST XML data...");
|
||||
return -1;
|
||||
|
||||
while (connection.isAlive() == true) {
|
||||
usleep(100000);
|
||||
}
|
||||
TEST_INFO("data : " << connection.dataString());
|
||||
|
||||
#else
|
||||
TEST_CRITICAL("not implemented");
|
||||
#endif
|
||||
|
111
test/main-server-http.cpp
Normal file
111
test/main-server-http.cpp
Normal file
@@ -0,0 +1,111 @@
|
||||
/** @file
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2014, Edouard DUPIN, all right reserved
|
||||
* @license APACHE v2.0 (see license file)
|
||||
*/
|
||||
|
||||
#include <test-debug/debug.h>
|
||||
#include <enet/Tcp.h>
|
||||
#include <enet/Http.h>
|
||||
#include <enet/TcpServer.h>
|
||||
#include <etk/etk.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <etk/stdTools.h>
|
||||
namespace appl {
|
||||
void onReceiveData(enet::Http& _interface, std::vector<uint8_t>& _data) {
|
||||
TEST_INFO("Receive Datas : " << _data.size() << " bytes");
|
||||
}
|
||||
void onReceiveHeader(enet::Http& _interface, const enet::HttpHeader& _data) {
|
||||
TEST_INFO("Receive Header data:");
|
||||
_data.display();
|
||||
if (_data.m_req == "GET") {
|
||||
if (_data.m_what == "http://127.0.0.1:12345/plop.txt") {
|
||||
_interface.writeAnswerHeader(enet::HTTPAnswerCode::c200_ok);
|
||||
std::string data = "<html><head></head></body>coucou</body></html>";
|
||||
_interface.write(data);
|
||||
_interface.stop();
|
||||
return;
|
||||
}
|
||||
}
|
||||
_interface.writeAnswerHeader(enet::HTTPAnswerCode::c200_ok);
|
||||
_interface.stop();
|
||||
}
|
||||
}
|
||||
|
||||
int main(int _argc, const char *_argv[]) {
|
||||
etk::init(_argc, _argv);
|
||||
for (int32_t iii=0; iii<_argc ; ++iii) {
|
||||
std::string data = _argv[iii];
|
||||
if ( data == "-h"
|
||||
|| data == "--help") {
|
||||
TEST_PRINT(etk::getApplicationName() << " - help : ");
|
||||
TEST_PRINT(" " << _argv[0] << " [options]");
|
||||
TEST_PRINT(" No options ...");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
TEST_INFO("==================================");
|
||||
TEST_INFO("== Test HTTP server ==");
|
||||
TEST_INFO("==================================");
|
||||
#ifndef __TARGET_OS__Windows
|
||||
//Wait on TCP connection:
|
||||
enet::TcpServer interface;
|
||||
// Configure server interface:
|
||||
interface.setHostNane("127.0.0.1");
|
||||
interface.setPort(12345);
|
||||
// Start listening ...
|
||||
interface.link();
|
||||
// Wait a new connection ..
|
||||
enet::Tcp tcpConnection = std::move(interface.waitNext());
|
||||
// Free Connected port
|
||||
interface.unlink();
|
||||
// TODO : Check if connection is valid ...
|
||||
|
||||
// Create a HTTP connection in Server mode
|
||||
enet::Http connection(std::move(tcpConnection), true);
|
||||
connection.setKeepAlive(true);
|
||||
// Set callbacks:
|
||||
connection.connect(appl::onReceiveData);
|
||||
connection.connectHeader(appl::onReceiveHeader);
|
||||
|
||||
// start http connection (the actual state is just TCP start ...)
|
||||
connection.start();
|
||||
|
||||
while (connection.isAlive() == true) {
|
||||
usleep(100000);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
TEST_INFO("----------------------------");
|
||||
TEST_INFO("GET data : ");
|
||||
if (connection.get("") == false) {
|
||||
TEST_ERROR("can not GET data...");
|
||||
return -1;
|
||||
}
|
||||
TEST_INFO("data : " << connection.dataString());
|
||||
|
||||
TEST_INFO("----------------------------");
|
||||
TEST_INFO("POST data : ");
|
||||
std::map<std::string, std::string> values;
|
||||
values.insert(std::make_pair<std::string, std::string>("plop", "valuePlop"));
|
||||
if (connection.post("", values) == false) {
|
||||
TEST_ERROR("can not POST data...");
|
||||
return -1;
|
||||
}
|
||||
TEST_INFO("data : " << connection.dataString());
|
||||
|
||||
TEST_INFO("----------------------------");
|
||||
TEST_INFO("POST xml : ");
|
||||
if (connection.post("", "text/xml; charset=utf-8", "<plop><string>value1</string></plop>") == false) {
|
||||
TEST_ERROR("can not POST XML data...");
|
||||
return -1;
|
||||
}
|
||||
TEST_INFO("data : " << connection.dataString());
|
||||
*/
|
||||
#else
|
||||
TEST_CRITICAL("not implemented");
|
||||
#endif
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user