-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
non blocking semantics for receiving soeckts and the tcp acceptor
tests updated
- Loading branch information
Foo
committed
Mar 23, 2024
1 parent
4964820
commit 21742dd
Showing
28 changed files
with
1,188 additions
and
656 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#include <MinimalSocket/tcp/TcpClient.h> | ||
#include <MinimalSocket/tcp/TcpServer.h> | ||
|
||
#include <atomic> | ||
#include <iostream> | ||
#include <mutex> | ||
#include <sstream> | ||
#include <thread> | ||
#include <vector> | ||
using namespace std; | ||
|
||
using namespace MinimalSocket; | ||
|
||
const std::uint16_t PORT = 44553; | ||
const string MESSAGE = "Hello from the sender"; | ||
|
||
struct Logger { | ||
Logger(const string &name) : name_{name} {} | ||
|
||
template <typename... Args> void log(const Args &...args) { | ||
std::stringstream buff; | ||
(pack(buff, args), ...); | ||
std::scoped_lock lock{getMtx()}; | ||
std::cout << '|' << name_ << "|: " << buff.str() << std::endl; | ||
} | ||
|
||
private: | ||
template <typename T> static void pack(std::stringstream &recipient, T val) { | ||
recipient << val; | ||
} | ||
|
||
static std::mutex &getMtx() { | ||
static std::mutex res = std::mutex{}; | ||
return res; | ||
} | ||
|
||
string name_; | ||
}; | ||
|
||
void server_loop(std::atomic_bool &done) { | ||
Logger logger{"Server"}; | ||
|
||
std::optional<tcp::TcpConnectionBlocking> connection; | ||
{ | ||
tcp::TcpServer<true> server{PORT, MinimalSocket::AddressFamily::IP_V4}; | ||
if (!server.open()) | ||
throw std::runtime_error{"unable to open the server"}; | ||
done = true; | ||
logger.log("listening"); | ||
connection.emplace(server.acceptNewClient()); | ||
} | ||
logger.log("connected"); | ||
while (true) { | ||
logger.log("sending"); | ||
connection->send(MESSAGE); | ||
logger.log("sleeping ... "); | ||
std::this_thread::sleep_for(std::chrono::milliseconds{1500}); | ||
} | ||
} | ||
|
||
void client_loop() { | ||
Logger logger{"Client"}; | ||
|
||
tcp::TcpClient<false> connection{Address{PORT}}; | ||
|
||
logger.log("connecting"); | ||
connection.open(); | ||
logger.log("connected"); | ||
|
||
while (true) { | ||
auto res = connection.receive(MESSAGE.size()); | ||
if (res.empty()) | ||
logger.log("nothing"); | ||
else | ||
logger.log("received `", res, '`'); | ||
std::this_thread::sleep_for(std::chrono::milliseconds{100}); | ||
} | ||
} | ||
|
||
int main() { | ||
std::atomic_bool done = false; | ||
std::thread server([&done]() { server_loop(done); }); | ||
while (!done.load()) { | ||
} | ||
|
||
client_loop(); | ||
|
||
server.join(); | ||
|
||
return EXIT_SUCCESS; | ||
} |
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
Oops, something went wrong.