@@ -28,40 +28,39 @@ NDN_LOG_INIT(repo.TcpHandle);
2828namespace ip = boost::asio::ip;
2929
3030namespace repo {
31- namespace detail {
31+ namespace {
3232
3333class TcpBulkInsertClient : noncopyable
3434{
3535public:
36- TcpBulkInsertClient (TcpBulkInsertHandle& writer,std::shared_ptr< ip::tcp::socket> socket)
36+ TcpBulkInsertClient (TcpBulkInsertHandle& writer, ip::tcp::socket socket)
3737 : m_writer(writer)
3838 , m_socket(std::move(socket))
3939 {
4040 }
4141
4242static void
43- startReceive (TcpBulkInsertHandle& writer,std::shared_ptr< ip::tcp::socket> socket)
43+ startReceive (TcpBulkInsertHandle& writer, ip::tcp::socket socket)
4444 {
4545auto client = std::make_shared<TcpBulkInsertClient>(writer,std::move (socket));
46- client->m_socket -> async_receive (
46+ client->m_socket . async_receive (
4747boost::asio::buffer (client->m_inputBuffer , ndn::MAX_NDN_PACKET_SIZE),0 ,
4848std::bind (&TcpBulkInsertClient::handleReceive, client, _1, _2, client));
4949 }
5050
5151private:
5252void
53- handleReceive (const boost::system::error_code& error,
54- std::size_t nBytesReceived,
53+ handleReceive (const boost::system::error_code& error, std::size_t nBytesReceived,
5554const std::shared_ptr<TcpBulkInsertClient>& client);
5655
5756private:
5857 TcpBulkInsertHandle& m_writer;
59- std::shared_ptr< ip::tcp::socket> m_socket;
58+ ip::tcp::socket m_socket;
6059uint8_t m_inputBuffer[ndn::MAX_NDN_PACKET_SIZE];
6160 std::size_t m_inputBufferSize =0 ;
6261};
6362
64- }// namespace detail
63+ }// namespace
6564
6665TcpBulkInsertHandle::TcpBulkInsertHandle (boost::asio::io_context& io,
6766 RepoStorage& storageHandle)
7473TcpBulkInsertHandle::listen (const std::string& host,const std::string& port)
7574{
7675 ip::tcp::resolverresolver (m_acceptor.get_executor ());
77- ip::tcp::resolver::queryquery (host, port);
76+ boost::system::error_code ec;
77+ auto results = resolver.resolve (host, port, ec);
78+ if (ec)
79+ NDN_THROW (Error (" Cannot resolve" + host +" :" + port +" (" + ec.message () +" )" ));
7880
79- ip::tcp::resolver::iterator endpoint = resolver.resolve (query);
80- ip::tcp::resolver::iterator end;
81- if (endpoint == end)
82- NDN_THROW (Error (" Cannot listen on" + host +" port" + port));
83-
84- m_localEndpoint = *endpoint;
81+ m_localEndpoint = *results.begin ();
8582NDN_LOG_DEBUG (" Start listening on" << m_localEndpoint);
8683
8784 m_acceptor.open (m_localEndpoint.protocol ());
@@ -105,39 +102,31 @@ TcpBulkInsertHandle::stop()
105102void
106103TcpBulkInsertHandle::asyncAccept ()
107104{
108- auto clientSocket = std::make_shared<ip::tcp::socket>(m_acceptor.get_executor ());
109- m_acceptor.async_accept (*clientSocket,
110- std::bind (&TcpBulkInsertHandle::handleAccept,this , _1, clientSocket));
111- }
112-
113- void
114- TcpBulkInsertHandle::handleAccept (const boost::system::error_code& error,
115- const std::shared_ptr<ip::tcp::socket>& socket)
116- {
117- if (error) {
118- return ;
119- }
120-
121- NDN_LOG_DEBUG (" New connection from" << socket->remote_endpoint ());
105+ m_acceptor.async_accept ([this ] (const auto & error, ip::tcp::socket socket) {
106+ if (error) {
107+ return ;
108+ }
122109
123- detail::TcpBulkInsertClient::startReceive (*this , socket);
110+ NDN_LOG_DEBUG (" New connection from" << socket.remote_endpoint ());
111+ TcpBulkInsertClient::startReceive (*this ,std::move (socket));
124112
125- // prepare accepting the next connection
126- asyncAccept ();
113+ // prepare accepting the next connection
114+ asyncAccept ();
115+ });
127116}
128117
129118void
130- detail:: TcpBulkInsertClient::handleReceive (const boost::system::error_code& error,
131- std::size_t nBytesReceived,
132- const std::shared_ptr<detail:: TcpBulkInsertClient>& client)
119+ TcpBulkInsertClient::handleReceive (const boost::system::error_code& error,
120+ std::size_t nBytesReceived,
121+ const std::shared_ptr<TcpBulkInsertClient>& client)
133122{
134123if (error) {
135- if (error == boost::system::errc::operation_canceled )// when socket is closed by someone
124+ if (error == boost::asio::error::operation_aborted )// when socket is closed by someone
136125return ;
137126
138127 boost::system::error_code ec;
139- m_socket-> shutdown (ip::tcp::socket::shutdown_both, ec);
140- m_socket-> close (ec);
128+ m_socket. shutdown (ip::tcp::socket::shutdown_both, ec);
129+ m_socket. close (ec);
141130return ;
142131 }
143132
@@ -175,8 +164,8 @@ detail::TcpBulkInsertClient::handleReceive(const boost::system::error_code& erro
175164
176165if (!isOk && m_inputBufferSize == ndn::MAX_NDN_PACKET_SIZE && offset ==0 ) {
177166 boost::system::error_code ec;
178- m_socket-> shutdown (ip::tcp::socket::shutdown_both, ec);
179- m_socket-> close (ec);
167+ m_socket. shutdown (ip::tcp::socket::shutdown_both, ec);
168+ m_socket. close (ec);
180169return ;
181170 }
182171
@@ -190,9 +179,9 @@ detail::TcpBulkInsertClient::handleReceive(const boost::system::error_code& erro
190179 }
191180 }
192181
193- m_socket-> async_receive (boost::asio::buffer (m_inputBuffer + m_inputBufferSize,
194- ndn::MAX_NDN_PACKET_SIZE - m_inputBufferSize),0 ,
195- std::bind (&TcpBulkInsertClient::handleReceive,this , _1, _2, client));
182+ m_socket. async_receive (boost::asio::buffer (m_inputBuffer + m_inputBufferSize,
183+ ndn::MAX_NDN_PACKET_SIZE - m_inputBufferSize),0 ,
184+ std::bind (&TcpBulkInsertClient::handleReceive,this , _1, _2, client));
196185}
197186
198187}// namespace repo