Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #128 from zussel/feature/networking
Feature/networking
- Loading branch information
Showing
96 changed files
with
8,441 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ADD_SUBDIRECTORY(net) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
SET(NET_LIBRARIES) | ||
|
||
IF(WIN32) | ||
MESSAGE(STATUS "Appending Windows Socket libs: wsock32 ws2_32") | ||
LIST(APPEND NET_LIBRARIES wsock32 ws2_32) | ||
ENDIF() | ||
|
||
# echo server example | ||
|
||
SET (ECHO_SERVER_SOURCES | ||
echo_server_main.cpp | ||
echo_server_connection.cpp | ||
echo_server_connection.hpp | ||
echo_server.cpp echo_server.hpp) | ||
|
||
ADD_EXECUTABLE(echo_server ${ECHO_SERVER_SOURCES}) | ||
|
||
TARGET_LINK_LIBRARIES(echo_server | ||
matador-unit | ||
matador-utils | ||
matador-logger | ||
matador-net | ||
${CMAKE_DL_LIBS} | ||
${NET_LIBRARIES} | ||
) | ||
|
||
# echo client example | ||
|
||
SET (ECHO_CLIENT_SOURCES | ||
echo_client_main.cpp echo_client_connection.cpp echo_client_connection.hpp echo_client.cpp echo_client.hpp) | ||
|
||
ADD_EXECUTABLE(echo_client ${ECHO_CLIENT_SOURCES}) | ||
|
||
TARGET_LINK_LIBRARIES(echo_client | ||
matador-unit | ||
matador-utils | ||
matador-logger | ||
matador-net | ||
${CMAKE_DL_LIBS} | ||
${NET_LIBRARIES} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include "echo_client.hpp" | ||
#include "echo_client_connection.hpp" | ||
|
||
using namespace matador; | ||
|
||
echo_client::echo_client(std::string port) | ||
: connector_(std::make_shared<matador::connector>()) | ||
, port_(std::move(port)) | ||
{ | ||
matador::add_log_sink(matador::create_file_sink("log/client.log")); | ||
matador::add_log_sink(matador::create_stdout_sink()); | ||
|
||
prepare_connect(); | ||
} | ||
|
||
void echo_client::run() | ||
{ | ||
service_.run(); | ||
} | ||
|
||
void echo_client::prepare_connect() | ||
{ | ||
service_.connect(connector_, port_, [](tcp::peer ep, io_stream &stream) { | ||
// create echo server connection | ||
auto conn = std::make_shared<echo_client_connection>(stream, std::move(ep)); | ||
conn->start(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#ifndef MATADOR_ECHO_CLIENT_HPP | ||
#define MATADOR_ECHO_CLIENT_HPP | ||
|
||
#include <string> | ||
#include <matador/net/connector.hpp> | ||
#include <matador/logger/log_manager.hpp> | ||
#include <matador/net/io_service.hpp> | ||
|
||
class echo_client | ||
{ | ||
public: | ||
explicit echo_client(std::string port); | ||
|
||
void run(); | ||
|
||
private: | ||
void prepare_connect(); | ||
private: | ||
matador::io_service service_; | ||
std::shared_ptr<matador::connector> connector_; | ||
std::string port_; | ||
}; | ||
|
||
|
||
#endif //MATADOR_ECHO_CLIENT_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include <matador/logger/log_manager.hpp> | ||
#include <iostream> | ||
#include "echo_client_connection.hpp" | ||
|
||
using namespace matador; | ||
|
||
echo_client_connection::echo_client_connection(matador::io_stream &stream, matador::tcp::peer endpoint) | ||
: log_(create_logger("EchoClientConnection")) | ||
, stream_(stream) | ||
, endpoint_(std::move(endpoint)) | ||
{} | ||
|
||
void echo_client_connection::start() | ||
{ | ||
write(); | ||
} | ||
|
||
void echo_client_connection::read() | ||
{ | ||
auto self(shared_from_this()); | ||
stream_.read(buf_, [this, self](int ec, int nread) { | ||
if (ec == 0) { | ||
std::cout << "Answer (size " << nread << "): " << std::string(buf_.data(), buf_.size()) << "\n"; | ||
buf_.clear(); | ||
write(); | ||
} | ||
}); | ||
} | ||
|
||
void echo_client_connection::write() | ||
{ | ||
std::cout << "Message: "; | ||
std::string message; | ||
std::cin >> message; | ||
if (message == "exit") { | ||
stream_.close_stream(); | ||
return; | ||
} | ||
buf_.append(message.c_str(), message.size()); | ||
auto self(shared_from_this()); | ||
stream_.write(buf_, [this, self](int ec, int nwrite) { | ||
if (ec == 0) { | ||
log_.info("%s sent (bytes: %d)", endpoint_.to_string().c_str(), nwrite); | ||
buf_.clear(); | ||
read(); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#ifndef MATADOR_ECHO_CLIENT_CONNECTION_HPP | ||
#define MATADOR_ECHO_CLIENT_CONNECTION_HPP | ||
|
||
#include <memory> | ||
#include <matador/net/io_stream.hpp> | ||
#include <matador/utils/buffer.hpp> | ||
|
||
class echo_client_connection : public std::enable_shared_from_this<echo_client_connection> | ||
{ | ||
public: | ||
explicit echo_client_connection(matador::io_stream &stream, matador::tcp::peer endpoint); | ||
|
||
void start(); | ||
|
||
void read(); | ||
|
||
void write(); | ||
|
||
private: | ||
matador::logger log_; | ||
matador::buffer buf_; | ||
matador::io_stream &stream_; | ||
matador::tcp::peer endpoint_; | ||
}; | ||
|
||
|
||
#endif //MATADOR_ECHO_CLIENT_CONNECTION_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include <iostream> | ||
#include <matador/net/os.hpp> | ||
#include "echo_client.hpp" | ||
|
||
using namespace matador; | ||
|
||
int main(int argc, char* argv[]) | ||
{ | ||
if (argc < 2) { | ||
std::cout << "usage: echo_client [port]\n"; | ||
return 1; | ||
} else { | ||
|
||
net::init(); | ||
|
||
echo_client client(argv[1]); | ||
|
||
client.run(); | ||
|
||
net::cleanup(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include <matador/net/acceptor.hpp> | ||
#include <matador/logger/log_manager.hpp> | ||
#include <matador/net/io_stream.hpp> | ||
#include "echo_server.hpp" | ||
#include "echo_server_connection.hpp" | ||
|
||
using namespace matador; | ||
|
||
echo_server::echo_server(unsigned short port) | ||
: acceptor_(std::make_shared<acceptor>(tcp::peer(address::v4::any(), port))) | ||
{ | ||
matador::add_log_sink(matador::create_file_sink("log/server.log")); | ||
matador::add_log_sink(matador::create_stdout_sink()); | ||
|
||
prepare_accept(); | ||
} | ||
|
||
void echo_server::run() | ||
{ | ||
service_.run(); | ||
} | ||
|
||
void echo_server::prepare_accept() | ||
{ | ||
service_.accept(acceptor_, [](tcp::peer ep, io_stream &stream) { | ||
// create echo server connection | ||
auto conn = std::make_shared<echo_server_connection>(stream, std::move(ep)); | ||
conn->start(); | ||
}); | ||
} |
Oops, something went wrong.