unilink  0.4.3
A simple C++ library for unified async communication
boost_tcp_socket.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2025 Jinwoo Sung
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
20 #include <sys/socket.h>
21 #include <sys/types.h>
22 #endif
23 
24 namespace unilink {
25 namespace transport {
26 
27 namespace net = boost::asio;
28 using tcp = net::ip::tcp;
29 
30 BoostTcpSocket::BoostTcpSocket(tcp::socket sock) : socket_(std::move(sock)) {
31 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
32  int yes = 1;
33  (void)::setsockopt(socket_.native_handle(), SOL_SOCKET, SO_NOSIGPIPE, &yes, sizeof(yes));
34 #endif
35 }
36 
37 void BoostTcpSocket::async_read_some(const net::mutable_buffer& buffer,
38  std::function<void(const boost::system::error_code&, std::size_t)> handler) {
39  socket_.async_read_some(buffer, std::move(handler));
40 }
41 
42 void BoostTcpSocket::async_write(const net::const_buffer& buffer,
43  std::function<void(const boost::system::error_code&, std::size_t)> handler) {
44  net::async_write(socket_, buffer, std::move(handler));
45 }
46 
47 void BoostTcpSocket::shutdown(tcp::socket::shutdown_type what, boost::system::error_code& ec) {
48  socket_.shutdown(what, ec);
49 }
50 
51 void BoostTcpSocket::close(boost::system::error_code& ec) { socket_.close(ec); }
52 
53 tcp::endpoint BoostTcpSocket::remote_endpoint(boost::system::error_code& ec) const {
54  return socket_.remote_endpoint(ec);
55 }
56 
57 } // namespace transport
58 } // namespace unilink