Skip to content
This repository was archived by the owner on Apr 16, 2018. It is now read-only.
Merged
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
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ int main()
Socket s(Domain::IPv4, Type::TCP);
s.start("127.0.0.1", 24000);
while (true) {
auto peer = s.accept();
auto msg = peer.recv(15);
std::cout << msg << "\n";
const auto peer = s.accept();
const auto msg = peer.recv(15);
std::cout << msg << '\n';
}
} catch (std::exception &e) {
std::cerr << e.what() << "\n";
std::cerr << e.what() << '\n';
}
}
```
10 changes: 7 additions & 3 deletions examples/meson.build
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
progs = [['socket_tcp_server', ['socket_tcp_server.cpp']],
['socket_tcp_client', ['socket_tcp_client.cpp']],
['socket_unix_udp_server',['socket_unix_udp_server.cpp']],
['socket_unix_udp_client',['socket_unix_udp_client.cpp']]]
['socket_unix_udp_server',['socket_unix_udp_server.cpp']],
['socket_unix_udp_client',['socket_unix_udp_client.cpp']],
['socket_tcp_mt_server', ['socket_tcp_mt_server.cpp']],
['socket_tcp_mt_client', ['socket_tcp_mt_client.cpp']],
['socket_tcp_fork_server', ['socket_tcp_fork_server.cpp']]]

foreach p : progs
executable(p[0], p[1], include_directories : inc, link_with : netlib)
executable(p[0], p[1], include_directories : inc,
link_with : netlib, dependencies: [thread_dep])
endforeach
2 changes: 1 addition & 1 deletion examples/socket_tcp_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ int main()
s.send("Hello World!");

} catch (std::exception &e) {
std::cerr << e.what() << "\n";
std::cerr << e.what() << '\n';
}
}
22 changes: 22 additions & 0 deletions examples/socket_tcp_fork_server.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "socket.hpp"
#include <iostream>

using namespace net;

int main()
{
try {
Socket s(Domain::IPv4, Type::TCP);
s.start("127.0.0.1", 24001);

while (true) {
const auto peer = s.accept();
if (!fork()) {
std::cout << peer.recv(10) << '\n';
return 0;
}
}
} catch (std::exception &e) {
std::cerr << e.what() << '\n';
}
}
32 changes: 32 additions & 0 deletions examples/socket_tcp_mt_client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "socket.hpp"
#include <iostream>
#include <thread>
#include <vector>

using namespace net;
using namespace std::chrono_literals;


void connect_send()
{
try {
Socket s(Domain::IPv4, Type::TCP);
std::this_thread::sleep_for(2s);
s.connect("127.0.0.1", 24001);
s.send("123456789");
} catch (std::exception &e) {
std::cerr << e.what() << '\n';
}
}


int main()
{
std::vector<std::thread> threads;
for (int i = 0; i < 100; ++i) {
threads.push_back(std::thread(connect_send));
}
for (auto &t : threads) {
t.join();
}
}
44 changes: 44 additions & 0 deletions examples/socket_tcp_mt_server.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "socket.hpp"
#include <iostream>
#include <thread>
#include <functional>
#include <condition_variable>

using namespace net;

std::mutex m;
std::condition_variable cv;
bool accepted = false;


void worker_thread(Socket &s)
{
try {
const auto peer = s.accept();
std::unique_lock<std::mutex> lock(m);
accepted = true;
lock.unlock();
cv.notify_one();
std::cout << peer.recv(10) << '\n';
} catch (std::exception &e) {
std::cerr << e.what() << '\n';
}
}


int main()
{
try {
Socket s(Domain::IPv4, Type::TCP);
s.start("127.0.0.1", 24001);

while (true) {
std::thread(worker_thread, std::ref(s)).detach();
std::unique_lock<std::mutex> lock(m);
cv.wait(lock, [] { return accepted; });
accepted = false;
}
} catch (std::exception &e) {
std::cerr << e.what() << '\n';
}
}
5 changes: 2 additions & 3 deletions examples/socket_tcp_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ int main()
while (1) {
const auto peer = s.accept();
const auto msg = peer.recv(15);
std::cout << msg << "\n";
std::cout << msg << '\n';
}

} catch (std::exception &e) {
std::cerr << e.what() << "\n";
std::cerr << e.what() << '\n';
}
}
6 changes: 2 additions & 4 deletions examples/socket_unix_udp_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ int main()

unixClient.write("hello server");
} catch (std::exception &e) {
std::cout << " Something unexpected happened: " << e.what() << '\n';
std::cout << e.what() << '\n';
}

return 0;
}
}
6 changes: 2 additions & 4 deletions examples/socket_unix_udp_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ int main()
std::cout << "Some client sent: " << res << '\n';

} catch (std::exception &e) {
std::cout << " Something unexpected happened: " << e.what() << '\n';
std::cout << e.what() << '\n';
}

return 0;
}
}
22 changes: 12 additions & 10 deletions include/socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Socket final {
int sockfd;
Domain sock_domain;
Type sock_type;
bool isClosed = false;


/**
Expand Down Expand Up @@ -850,11 +851,11 @@ class Socket final {
* @access public
* Unlinks the unix socket path.
*/
void unlink() const noexcept
bool unlink() const noexcept
{
if (sock_domain == Domain::UNIX) {
::unlink(unix.sun_path);
}
return (sock_domain == Domain::UNIX && !isClosed)
? ::unlink(unix.sun_path) == 0
: false;
}


Expand All @@ -863,15 +864,16 @@ class Socket final {
* @access public
* Closes the Socket for terminating connection.
*/
void close() const noexcept { ::close(sockfd); }
bool close() const noexcept
{
return (!isClosed) ? ::close(sockfd) == 0 : false;
}


~Socket()
~Socket() noexcept
{
if (sock_domain == Domain::UNIX) {
::unlink(unix.sun_path);
}
::close(sockfd);
unlink();
close();
}
};
}
Expand Down
9 changes: 5 additions & 4 deletions test/socket_bind_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,17 @@ TEST(Socket, Bind)
}),
std::invalid_argument);


Socket unixSocket(Domain::UNIX, Type::TCP);
std::string unixSocketPath("/tmp/unixSocketFile");
unixSocket.bind([&](AddrUnix &s) {
return methods::construct(s, unixSocketPath.c_str());
});
AddrUnix actualUnixSocket;
socklen_t actualUnixSocketSize = sizeof(actualUnixSocket);
ASSERT_NE(getsockname(unixSocket.getSocket(),
(sockaddr *) &actualUnixSocket,
&actualUnixSocketSize),
-1);
ASSERT_NE(
getsockname(unixSocket.getSocket(), (sockaddr *) &actualUnixSocket,
&actualUnixSocketSize),
-1);
EXPECT_EQ(actualUnixSocket.sun_path, unixSocketPath);
}