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

Commit

Permalink
tcp_client example
Browse files Browse the repository at this point in the history
  • Loading branch information
Cylix committed Dec 11, 2016
1 parent 6db406c commit 1b037a4
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ s.start("127.0.0.1", 3001, [] (tacopie::tcp_client& client) -> bool {
```
`tacopie::tcp_server` [full documentation](https://github.com/Cylix/tacopie/wiki/TCP-Server) and [detailed example](https://github.com/Cylix/tacopie/wiki/Examples#tcp-server).

`tacopie::tcp_client`:
```cpp
tacopie::tcp_client client;
client.connect("127.0.0.1", 3001);
client.async_read({ 1024, [] (tacopie::tcp_client& client, const tacopie::tcp_client::read_result& res) {
client.async_write({ res.buffer, nullptr });
} });
```
`tacopie::tcp_client` [full documentation](https://github.com/Cylix/tacopie/wiki/TCP-Client) and [detailed example](https://github.com/Cylix/tacopie/wiki/Examples#tcp-client).

## Wiki
A [Wiki](https://github.com/Cylix/tacopie/wiki) is available and provides full documentation for the library as well as [installation explanations](https://github.com/Cylix/tacopie/wiki/Installation).

Expand Down
12 changes: 9 additions & 3 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ include_directories(${PROJECT_SOURCE_DIR}/includes
###
# executable
###
add_executable(tacopie_example tacopie.cpp)
target_link_libraries(tacopie_example tacopie)
add_executable(tcp_server tcp_server.cpp)
target_link_libraries(tcp_server tacopie)
IF (LOGGING_ENABLED)
set_target_properties(tacopie_example PROPERTIES COMPILE_DEFINITIONS "__TACOPIE_LOGGING_ENABLED=${LOGGING_ENABLED}")
set_target_properties(tcp_server PROPERTIES COMPILE_DEFINITIONS "__TACOPIE_LOGGING_ENABLED=${LOGGING_ENABLED}")
ENDIF (LOGGING_ENABLED)

add_executable(tcp_client tcp_client.cpp)
target_link_libraries(tcp_client tacopie)
IF (LOGGING_ENABLED)
set_target_properties(tcp_client PROPERTIES COMPILE_DEFINITIONS "__TACOPIE_LOGGING_ENABLED=${LOGGING_ENABLED}")
ENDIF (LOGGING_ENABLED)

add_executable(logger logger.cpp)
Expand Down
41 changes: 41 additions & 0 deletions examples/tcp_client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <tacopie/tacopie>

#include <iostream>
#include <mutex>
#include <condition_variable>
#include <signal.h>

std::condition_variable cv;

void
signint_handler(int) {
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, &on_new_message });
}
else {
std::cout << "Client disconnected" << std::endl;
client.disconnect();
}
}

int
main(void) {
tacopie::tcp_client client;
client.connect("127.0.0.1", 3001);
client.async_read({ 1024, &on_new_message });

signal(SIGINT, &signint_handler);

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

return 0;
}
File renamed without changes.
2 changes: 1 addition & 1 deletion includes/tacopie/network/tcp_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class tcp_client {
//! returns whether the client is currently connected or not
bool is_connected(void) const;

public:
private:
//! Call disconnection handler
void call_disconnection_handler(void);

Expand Down

0 comments on commit 1b037a4

Please sign in to comment.