Loading...
Searching...
No Matches
async_acceptor.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#pragma once
21#include <boost/asio.hpp>
22#include <memory>
23
24namespace resdb {
25
27 public:
28 typedef std::function<void(const char* buffer, size_t len)> CallBack;
29
30 AsyncAcceptor(const std::string& ip, int thread_num, int port,
31 CallBack call_back_func);
32 virtual ~AsyncAcceptor();
33
34 void StartAccept();
35
36 private:
37 class Session {
38 public:
39 Session(boost::asio::io_service* io_service, CallBack call_back_func);
40 ~Session();
41
42 boost::asio::ip::tcp::socket* GetSocket();
43
44 void StartRead();
45 void Close();
46
47 private:
48 void ReadDone();
49 void OnRead();
50
51 private:
52 boost::asio::io_service* io_service_ = nullptr;
53 boost::asio::ip::tcp::socket client_socket_;
54 size_t data_size_ = 0;
55 size_t need_size_ = 0;
56 size_t current_idx_ = 0;
57 char* recv_buffer_ = nullptr;
58 bool status_ = 0;
59 CallBack call_back_func_;
60 };
61
62 private:
63 void OnAccept(boost::shared_ptr<Session> client_socket,
64 const boost::system::error_code ec);
65
66 private:
67 boost::asio::io_service io_service_;
68 boost::asio::ip::tcp::endpoint endpoint_;
69 boost::asio::ip::tcp::acceptor acceptor_;
70 std::unique_ptr<boost::asio::io_service::work> worker_;
71 CallBack call_back_func_;
72 std::vector<std::thread> worker_thread_;
73 std::vector<boost::shared_ptr<Session>> sessions_;
74};
75
76} // namespace resdb
std::function< void(const char *buffer, size_t len)> CallBack