enet/test/main-server.cpp

68 lines
1.8 KiB
C++
Raw Normal View History

2016-05-02 22:14:07 +02:00
/** @file
* @author Edouard DUPIN
* @copyright 2014, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file)
2016-05-02 22:14:07 +02:00
*/
2016-09-30 22:28:36 +02:00
#include <test-debug/debug.hpp>
#include <enet/enet.hpp>
2016-09-30 22:28:36 +02:00
#include <enet/Tcp.hpp>
#include <enet/Http.hpp>
#include <etk/etk.hpp>
#include <enet/TcpServer.hpp>
2016-05-02 22:14:07 +02:00
2016-09-30 22:28:36 +02:00
#include <etk/stdTools.hpp>
2016-05-02 22:14:07 +02:00
int main(int _argc, const char *_argv[]) {
etk::init(_argc, _argv);
enet::init(_argc, _argv);
2016-05-02 22:14:07 +02:00
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 TCP server ==");
TEST_INFO("==================================");
2016-08-09 21:17:44 +02:00
//Wait on TCP connection:
enet::TcpServer interface;
// Configure server interface:
interface.setHostNane("127.0.0.1");
interface.setPort(12345);
2016-08-09 21:17:44 +02:00
// Start listening ...
interface.link();
// Wait a new connection ..
enet::Tcp tcpConnection = std::move(interface.waitNext());
// Free Connected port
interface.unlink();
2016-05-02 22:14:07 +02:00
int32_t iii = 0;
2016-09-08 21:35:02 +02:00
while (tcpConnection.getConnectionStatus() == enet::Tcp::status::link) {
2016-08-09 21:17:44 +02:00
int32_t len = tcpConnection.write("plop" + etk::to_string(iii));
2016-05-02 22:14:07 +02:00
TEST_INFO("write len=" << len);
2016-05-12 21:01:11 +02:00
char data[1024];
2016-08-09 21:17:44 +02:00
len = tcpConnection.read(data, 1024);
2016-05-12 21:01:11 +02:00
if (len > 0) {
TEST_INFO("read len=" << len << " data='" << data << "'");
}
2016-05-02 22:14:07 +02:00
iii++;
}
if (iii>=1000000) {
TEST_INFO("auto disconnected");
2016-09-08 21:35:02 +02:00
} else if (tcpConnection.getConnectionStatus() != enet::Tcp::status::link) {
2016-05-02 22:14:07 +02:00
TEST_INFO("server disconnected");
} else {
TEST_INFO("ERROR disconnected");
}
2016-08-09 21:17:44 +02:00
if (tcpConnection.unlink() == false) {
2016-05-02 22:14:07 +02:00
TEST_ERROR("can not unlink to the socket...");
return -1;
}
return 0;
}