Skip to content
This repository was archived by the owner on Apr 6, 2019. It is now read-only.

Perfect examples #47

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 54 additions & 32 deletions examples/tcp_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,54 +29,76 @@

#ifdef _WIN32
#include <Winsock2.h>

//! Windows netword DLL init
struct _INIT_WSA_ {

_INIT_WSA_() {
WORD version = MAKEWORD(2, 2);
WSADATA data;

if (WSAStartup(version, &data) != 0) {
std::cerr << "WSAStartup() failure" << std::endl;
}
};

~_INIT_WSA_() {
WSACleanup();
};

} __INIT_WSA__;


#endif /* _WIN32 */


std::condition_variable cv;

void
signint_handler(int) {
cv.notify_all();
cv.notify_all();
}

void
on_new_message(tacopie::tcp_client& client, const tacopie::tcp_client::read_result& res) {
if (res.success) {
std::cout << "Client recv data" << std::endl;
client.async_write({res.buffer, nullptr});
client.async_read({1024, std::bind(&on_new_message, std::ref(client), std::placeholders::_1)});
}
else {
std::cout << "Client disconnected" << std::endl;
client.disconnect();
}
if (res.success) {
std::string str;
str.insert(str.begin(), res.buffer.begin(), res.buffer.end());
std::cout << "Client recv data:" << str << std::endl;
//client.async_write({ res.buffer, nullptr });
client.async_read({ 1024, std::bind(&on_new_message, std::ref(client), std::placeholders::_1) });
}
else {
std::cout << "Client disconnected" << std::endl;
client.disconnect();
}
}

int
main(void) {
#ifdef _WIN32
//! Windows netword DLL init
WORD version = MAKEWORD(2, 2);
WSADATA data;

if (WSAStartup(version, &data) != 0) {
std::cerr << "WSAStartup() failure" << std::endl;
return -1;
}
#endif /* _WIN32 */
tacopie::tcp_client client;
try
{
client.connect("localhost", 3001);
}
catch (const std::exception& e)
{
std::cout << "connect error:" << e.what() << std::endl;
return 0;
}

client.async_read({ 1024, std::bind(&on_new_message, std::ref(client), std::placeholders::_1) });

tacopie::tcp_client client;
client.connect("127.0.0.1", 3001);
client.async_read({1024, std::bind(&on_new_message, std::ref(client), std::placeholders::_1)});
std::string str = "hello world!";
std::vector<char> buff;
buff.insert(buff.begin(), str.begin(), str.end());

signal(SIGINT, &signint_handler);
client.async_write({ buff, nullptr });
signal(SIGINT, &signint_handler);

std::mutex mtx;
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock);

#ifdef _WIN32
WSACleanup();
#endif /* _WIN32 */
std::mutex mtx;
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock);

return 0;
return 0;
}
80 changes: 45 additions & 35 deletions examples/tcp_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,57 +29,67 @@

#ifdef _WIN32
#include <Winsock2.h>

//! Windows netword DLL init
struct _INIT_WSA_ {

_INIT_WSA_() {
WORD version = MAKEWORD(2, 2);
WSADATA data;

if (WSAStartup(version, &data) != 0) {
std::cerr << "WSAStartup() failure" << std::endl;
}
};

~_INIT_WSA_() {
WSACleanup();
};

} __INIT_WSA__;

#endif /* _WIN32 */

std::condition_variable cv;

void
signint_handler(int) {
cv.notify_all();
cv.notify_all();
}

void
on_new_message(const std::shared_ptr<tacopie::tcp_client>& client, const tacopie::tcp_client::read_result& res) {
if (res.success) {
std::cout << "Client recv data" << std::endl;
client->async_write({res.buffer, nullptr});
client->async_read({1024, std::bind(&on_new_message, client, std::placeholders::_1)});
}
else {
std::cout << "Client disconnected" << std::endl;
client->disconnect();
}
if (res.success) {
std::string str;
str.insert(str.begin(), res.buffer.begin(), res.buffer.end());

std::cout << "Client recv data" << "[" << client->get_host() << ":" << client->get_port() << "]:"
<< str << std::endl;
client->async_write({ res.buffer, nullptr });
client->async_read({ 1024, std::bind(&on_new_message, client, std::placeholders::_1) });
}
else {
std::cout << "Client disconnected" << "[" << client->get_host() << ":" << client->get_port() << "]" << std::endl;
client->disconnect();
}
}

int
main(void) {
#ifdef _WIN32
//! Windows netword DLL init
WORD version = MAKEWORD(2, 2);
WSADATA data;

if (WSAStartup(version, &data) != 0) {
std::cerr << "WSAStartup() failure" << std::endl;
return -1;
}
#endif /* _WIN32 */

tacopie::tcp_server s;
s.start("127.0.0.1", 3001, [](const std::shared_ptr<tacopie::tcp_client>& client) -> bool {
std::cout << "New client" << std::endl;
client->async_read({1024, std::bind(&on_new_message, client, std::placeholders::_1)});
return true;
});
tacopie::tcp_server s;
s.start("localhost", 3001, [](const std::shared_ptr<tacopie::tcp_client>& client) -> bool {
std::cout <<
"New client:" << "[" <<client->get_host() << ":" << client->get_port() << "]" << std::endl;
client->async_read({ 1024, std::bind(&on_new_message, client, std::placeholders::_1) });
return true;
});

signal(SIGINT, &signint_handler);
signal(SIGINT, &signint_handler);

std::mutex mtx;
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock);

#ifdef _WIN32
WSACleanup();
#endif /* _WIN32 */
std::mutex mtx;
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock);

return 0;
return 0;
}