mirror of
https://gitee.com/acl-dev/acl.git
synced 2024-12-02 03:47:53 +08:00
add tcp communication connections pool.
This commit is contained in:
parent
b48627f168
commit
7bca9425a9
@ -1,6 +1,9 @@
|
||||
修改历史列表:
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
488) 2017.8.8
|
||||
488.1) feature: 增加了 tcp 连接池通信模块
|
||||
|
||||
487) 2017.8.4
|
||||
487.1) feature: polarssl_conf & polarssl_io 两个类支持动态加载 polarssl 动态库
|
||||
|
||||
|
37
lib_acl_cpp/include/acl_cpp/connpool/tcp_client.hpp
Normal file
37
lib_acl_cpp/include/acl_cpp/connpool/tcp_client.hpp
Normal file
@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
#include "../acl_cpp_define.hpp"
|
||||
#include "connect_client.hpp"
|
||||
|
||||
namespace acl
|
||||
{
|
||||
|
||||
class socket_stream;
|
||||
class tcp_sender;
|
||||
class tcp_reader;
|
||||
class string;
|
||||
|
||||
class ACL_CPP_API tcp_client : public connect_client
|
||||
{
|
||||
public:
|
||||
tcp_client(const char* addr, int conn_timeout = 10, int rw_timeout = 10);
|
||||
virtual ~tcp_client(void);
|
||||
|
||||
bool send(const void* data, unsigned int len, string* out = NULL);
|
||||
|
||||
protected:
|
||||
// @override
|
||||
virtual bool open(void);
|
||||
|
||||
private:
|
||||
char* addr_;
|
||||
int conn_timeout_;
|
||||
int rw_timeout_;
|
||||
|
||||
socket_stream* conn_;
|
||||
tcp_sender* sender_;
|
||||
tcp_reader* reader_;
|
||||
|
||||
bool try_open(bool* reuse_conn);
|
||||
};
|
||||
|
||||
} // namespace acl
|
46
lib_acl_cpp/include/acl_cpp/connpool/tcp_ipc.hpp
Normal file
46
lib_acl_cpp/include/acl_cpp/connpool/tcp_ipc.hpp
Normal file
@ -0,0 +1,46 @@
|
||||
/**
|
||||
* Copyright (C) 2017-2018
|
||||
* All rights reserved.
|
||||
*
|
||||
* AUTHOR(S)
|
||||
* Zheng Shuxin
|
||||
* E-mail: zhengshuxin@qiyi.com
|
||||
*
|
||||
* VERSION
|
||||
* Tue 08 Aug 2017 03:09:21 PM CST
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace acl
|
||||
{
|
||||
|
||||
class tcp_manager;
|
||||
class tcp_pool;
|
||||
class string;
|
||||
|
||||
class tcp_ipc
|
||||
{
|
||||
public:
|
||||
tcp_ipc(void);
|
||||
~tcp_ipc(void);
|
||||
|
||||
tcp_ipc& set_limit(int max);
|
||||
tcp_ipc& set_idle(int ttl);
|
||||
tcp_ipc& set_conn_timeout(int conn_timeout);
|
||||
tcp_ipc& set_rw_timeout(int timeout);
|
||||
|
||||
bool send(const char* addr, const void* data, unsigned int len,
|
||||
string* out = NULL);
|
||||
|
||||
private:
|
||||
tcp_manager* manager_;
|
||||
int max_;
|
||||
int ttl_;
|
||||
int conn_timeout_;
|
||||
int rw_timeout_;
|
||||
|
||||
bool send(tcp_pool&, const void*, unsigned int, string*);
|
||||
};
|
||||
|
||||
} // namespace acl
|
21
lib_acl_cpp/include/acl_cpp/connpool/tcp_manager.hpp
Normal file
21
lib_acl_cpp/include/acl_cpp/connpool/tcp_manager.hpp
Normal file
@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
#include "../acl_cpp_define.hpp"
|
||||
#include "connect_manager.hpp"
|
||||
|
||||
namespace acl
|
||||
{
|
||||
|
||||
class connect_pool;
|
||||
|
||||
class ACL_CPP_API tcp_manager : public connect_manager
|
||||
{
|
||||
public:
|
||||
tcp_manager(void);
|
||||
virtual ~tcp_manager(void);
|
||||
|
||||
protected:
|
||||
// @override
|
||||
virtual connect_pool* create_pool(const char*, size_t, size_t);
|
||||
};
|
||||
|
||||
} // namespace acl
|
21
lib_acl_cpp/include/acl_cpp/connpool/tcp_pool.hpp
Normal file
21
lib_acl_cpp/include/acl_cpp/connpool/tcp_pool.hpp
Normal file
@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
#include "../acl_cpp_define.hpp"
|
||||
#include "connect_pool.hpp"
|
||||
|
||||
namespace acl
|
||||
{
|
||||
|
||||
class connect_client;
|
||||
|
||||
class ACL_CPP_API tcp_pool : public connect_pool
|
||||
{
|
||||
public:
|
||||
tcp_pool(const char* addr, size_t count, size_t idx = 0);
|
||||
virtual ~tcp_pool(void);
|
||||
|
||||
protected:
|
||||
// @override
|
||||
virtual connect_client* create_connect(void);
|
||||
};
|
||||
|
||||
} // namespace acl
|
33
lib_acl_cpp/include/acl_cpp/connpool/tcp_reader.hpp
Normal file
33
lib_acl_cpp/include/acl_cpp/connpool/tcp_reader.hpp
Normal file
@ -0,0 +1,33 @@
|
||||
/**
|
||||
* Copyright (C) 2017-2018
|
||||
* All rights reserved.
|
||||
*
|
||||
* AUTHOR(S)
|
||||
* Zheng Shuxin
|
||||
* E-mail: zhengshuxin@qiyi.com
|
||||
*
|
||||
* VERSION
|
||||
* Tue 08 Aug 2017 01:57:39 PM CST
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace acl
|
||||
{
|
||||
|
||||
class socket_stream;
|
||||
class string;
|
||||
|
||||
class tcp_reader
|
||||
{
|
||||
public:
|
||||
tcp_reader(socket_stream& conn);
|
||||
~tcp_reader(void) {}
|
||||
|
||||
bool read(string& out);
|
||||
|
||||
private:
|
||||
socket_stream* conn_;
|
||||
};
|
||||
|
||||
} // namespace acl
|
34
lib_acl_cpp/include/acl_cpp/connpool/tcp_sender.hpp
Normal file
34
lib_acl_cpp/include/acl_cpp/connpool/tcp_sender.hpp
Normal file
@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Copyright (C) 2017-2018
|
||||
* All rights reserved.
|
||||
*
|
||||
* AUTHOR(S)
|
||||
* Zheng Shuxin
|
||||
* E-mail: zhengshuxin@qiyi.com
|
||||
*
|
||||
* VERSION
|
||||
* Tue 08 Aug 2017 02:11:05 PM CST
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "../acl_cpp_define.hpp"
|
||||
|
||||
namespace acl
|
||||
{
|
||||
|
||||
class socket_stream;
|
||||
|
||||
class ACL_CPP_API tcp_sender
|
||||
{
|
||||
public:
|
||||
tcp_sender(socket_stream& conn);
|
||||
~tcp_sender(void) {}
|
||||
|
||||
bool send(const void* data, unsigned int len);
|
||||
|
||||
private:
|
||||
acl::socket_stream* conn_;
|
||||
struct iovec v_[2];
|
||||
};
|
||||
|
||||
} // namespace acl
|
@ -155,6 +155,12 @@
|
||||
#include "connpool/connect_manager.hpp"
|
||||
#include "connpool/connect_monitor.hpp"
|
||||
#include "connpool/check_client.hpp"
|
||||
#include "connpool/tcp_client.hpp"
|
||||
#include "connpool/tcp_pool.hpp"
|
||||
#include "connpool/tcp_manager.hpp"
|
||||
#include "connpool/tcp_ipc.hpp"
|
||||
#include "connpool/tcp_sender.hpp"
|
||||
#include "connpool/tcp_reader.hpp"
|
||||
|
||||
#include "redis/redis_client.hpp"
|
||||
#include "redis/redis_client_pool.hpp"
|
||||
|
@ -902,6 +902,24 @@ copy $(TargetName).pdb ..\dist\lib\win32\$(TargetName).pdb /Y
|
||||
<File
|
||||
RelativePath=".\src\connpool\connect_pool.cpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\connpool\tcp_reader.cpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\connpool\tcp_sender.cpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\connpool\tcp_client.cpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\connpool\tcp_pool.cpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\connpool\tcp_manager.cpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\connpool\tcp_ipc.cpp">
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="event"
|
||||
@ -1483,6 +1501,24 @@ copy $(TargetName).pdb ..\dist\lib\win32\$(TargetName).pdb /Y
|
||||
<File
|
||||
RelativePath=".\include\acl_cpp\connpool\connect_pool.hpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\include\acl_cpp\connpool\tcp_reader.hpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\include\acl_cpp\connpool\tcp_sender.hpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\include\acl_cpp\connpool\tcp_client.hpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\include\acl_cpp\connpool\tcp_pool.hpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\include\acl_cpp\connpool\tcp_manager.hpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\include\acl_cpp\connpool\tcp_ipc.hpp">
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="event"
|
||||
|
@ -401,6 +401,30 @@
|
||||
RelativePath=".\src\connpool\connect_pool.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\connpool\tcp_reader.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\connpool\tcp_sender.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\connpool\tcp_client.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\connpool\tcp_pool.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\connpool\tcp_manager.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\src\connpool\tcp_ipc.cpp"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="db"
|
||||
@ -1247,6 +1271,30 @@
|
||||
RelativePath=".\include\acl_cpp\connpool\connect_pool.hpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\include\acl_cpp\connpool\tcp_reader.hpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\include\acl_cpp\connpool\tcp_sender.hpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\include\acl_cpp\connpool\tcp_client.hpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\include\acl_cpp\connpool\tcp_pool.hpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\include\acl_cpp\connpool\tcp_manager.hpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\include\acl_cpp\connpool\tcp_ipc.hpp"
|
||||
>
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="db"
|
||||
|
@ -210,6 +210,12 @@ copy $(TargetName).pdb ..\dist\lib\win32\$(TargetName).pdb /Y
|
||||
<ClCompile Include="src\connpool\connect_manager.cpp" />
|
||||
<ClCompile Include="src\connpool\connect_monitor.cpp" />
|
||||
<ClCompile Include="src\connpool\connect_pool.cpp" />
|
||||
<ClCompile Include="src\connpool\tcp_reader.cpp" />
|
||||
<ClCompile Include="src\connpool\tcp_sender.cpp" />
|
||||
<ClCompile Include="src\connpool\tcp_client.cpp" />
|
||||
<ClCompile Include="src\connpool\tcp_pool.cpp" />
|
||||
<ClCompile Include="src\connpool\tcp_manager.cpp" />
|
||||
<ClCompile Include="src\connpool\tcp_ipc.cpp" />
|
||||
<ClCompile Include="src\db\db_handle.cpp" />
|
||||
<ClCompile Include="src\db\db_mysql.cpp" />
|
||||
<ClCompile Include="src\db\db_pgsql.cpp" />
|
||||
@ -388,6 +394,12 @@ copy $(TargetName).pdb ..\dist\lib\win32\$(TargetName).pdb /Y
|
||||
<ClInclude Include="include\acl_cpp\connpool\connect_manager.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\connect_monitor.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\connect_pool.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_reader.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_sender.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_client.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_pool.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_manager.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_ipc.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\db\db_handle.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\db\db_mysql.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\db\db_pgsql.hpp" />
|
||||
|
@ -343,6 +343,24 @@
|
||||
<ClCompile Include="src\connpool\connect_pool.cpp">
|
||||
<Filter>src\connpool</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\connpool\tcp_reader.cpp">
|
||||
<Filter>src\connpool</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\connpool\tcp_sender.cpp">
|
||||
<Filter>src\connpool</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\connpool\tcp_client.cpp">
|
||||
<Filter>src\connpool</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\connpool\tcp_pool.cpp">
|
||||
<Filter>src\connpool</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\connpool\tcp_manager.cpp">
|
||||
<Filter>src\connpool</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\connpool\tcp_ipc.cpp">
|
||||
<Filter>src\connpool</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\memcache\memcache.cpp">
|
||||
<Filter>src\memcache</Filter>
|
||||
</ClCompile>
|
||||
@ -912,6 +930,24 @@
|
||||
<ClInclude Include="include\acl_cpp\connpool\connect_client.hpp">
|
||||
<Filter>include\connpool</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_reader.hpp">
|
||||
<Filter>include\connpool</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_sender.hpp">
|
||||
<Filter>include\connpool</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_client.hpp">
|
||||
<Filter>include\connpool</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_pool.hpp">
|
||||
<Filter>include\connpool</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_manager.hpp">
|
||||
<Filter>include\connpool</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_ipc.hpp">
|
||||
<Filter>include\connpool</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\acl_cpp\memcache\memcache_manager.hpp">
|
||||
<Filter>include\memcache</Filter>
|
||||
</ClInclude>
|
||||
|
@ -404,6 +404,12 @@ copy $(TargetName).pdb ..\dist\lib\win64\$(TargetName).pdb /Y
|
||||
<ClCompile Include="src\connpool\connect_manager.cpp" />
|
||||
<ClCompile Include="src\connpool\connect_monitor.cpp" />
|
||||
<ClCompile Include="src\connpool\connect_pool.cpp" />
|
||||
<ClCompile Include="src\connpool\tcp_reader.cpp" />
|
||||
<ClCompile Include="src\connpool\tcp_sender.cpp" />
|
||||
<ClCompile Include="src\connpool\tcp_client.cpp" />
|
||||
<ClCompile Include="src\connpool\tcp_pool.cpp" />
|
||||
<ClCompile Include="src\connpool\tcp_manager.cpp" />
|
||||
<ClCompile Include="src\connpool\tcp_ipc.cpp" />
|
||||
<ClCompile Include="src\db\db_handle.cpp" />
|
||||
<ClCompile Include="src\db\db_mysql.cpp" />
|
||||
<ClCompile Include="src\db\db_pgsql.cpp" />
|
||||
@ -582,6 +588,12 @@ copy $(TargetName).pdb ..\dist\lib\win64\$(TargetName).pdb /Y
|
||||
<ClInclude Include="include\acl_cpp\connpool\connect_manager.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\connect_monitor.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\connect_pool.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_reader.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_sender.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_client.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_pool.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_manager.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_ipc.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\db\db_handle.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\db\db_mysql.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\db\db_pgsql.hpp" />
|
||||
|
@ -346,6 +346,24 @@
|
||||
<ClCompile Include="src\connpool\connect_pool.cpp">
|
||||
<Filter>Source Files\connpool</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\connpool\tcp_reader.cpp">
|
||||
<Filter>Source Files\connpool</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\connpool\tcp_sender.cpp">
|
||||
<Filter>Source Files\connpool</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\connpool\tcp_client.cpp">
|
||||
<Filter>Source Files\connpool</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\connpool\tcp_pool.cpp">
|
||||
<Filter>Source Files\connpool</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\connpool\tcp_manager.cpp">
|
||||
<Filter>Source Files\connpool</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\connpool\tcp_ipc.cpp">
|
||||
<Filter>Source Files\connpool</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\http\http_request_pool.cpp">
|
||||
<Filter>Source Files\http</Filter>
|
||||
</ClCompile>
|
||||
@ -900,6 +918,24 @@
|
||||
<ClInclude Include="include\acl_cpp\connpool\connect_pool.hpp">
|
||||
<Filter>Header Files\connpool</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_readder.hpp">
|
||||
<Filter>Header Files\connpool</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_sender.hpp">
|
||||
<Filter>Header Files\connpool</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_client.hpp">
|
||||
<Filter>Header Files\connpool</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_pool.hpp">
|
||||
<Filter>Header Files\connpool</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_manager.hpp">
|
||||
<Filter>Header Files\connpool</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_ipc.hpp">
|
||||
<Filter>Header Files\connpool</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\acl_cpp\http\http_request_pool.hpp">
|
||||
<Filter>Header Files\http</Filter>
|
||||
</ClInclude>
|
||||
|
@ -404,6 +404,12 @@ copy $(TargetName).pdb ..\dist\lib\win64\$(TargetName).pdb /Y
|
||||
<ClCompile Include="src\connpool\connect_manager.cpp" />
|
||||
<ClCompile Include="src\connpool\connect_monitor.cpp" />
|
||||
<ClCompile Include="src\connpool\connect_pool.cpp" />
|
||||
<ClCompile Include="src\connpool\tcp_reader.cpp" />
|
||||
<ClCompile Include="src\connpool\tcp_sender.cpp" />
|
||||
<ClCompile Include="src\connpool\tcp_client.cpp" />
|
||||
<ClCompile Include="src\connpool\tcp_pool.cpp" />
|
||||
<ClCompile Include="src\connpool\tcp_manager.cpp" />
|
||||
<ClCompile Include="src\connpool\tcp_ipc.cpp" />
|
||||
<ClCompile Include="src\db\db_handle.cpp" />
|
||||
<ClCompile Include="src\db\db_mysql.cpp" />
|
||||
<ClCompile Include="src\db\db_pgsql.cpp" />
|
||||
@ -582,6 +588,12 @@ copy $(TargetName).pdb ..\dist\lib\win64\$(TargetName).pdb /Y
|
||||
<ClInclude Include="include\acl_cpp\connpool\connect_manager.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\connect_monitor.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\connect_pool.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_reader.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_sender.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_client.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_pool.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_manager.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_ipc.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\db\db_handle.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\db\db_mysql.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\db\db_pgsql.hpp" />
|
||||
|
@ -346,6 +346,24 @@
|
||||
<ClCompile Include="src\connpool\connect_pool.cpp">
|
||||
<Filter>Source Files\connpool</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\connpool\tcp_reader.cpp">
|
||||
<Filter>Source Files\connpool</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\connpool\tcp_sender.cpp">
|
||||
<Filter>Source Files\connpool</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\connpool\tcp_client.cpp">
|
||||
<Filter>Source Files\connpool</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\connpool\tcp_pool.cpp">
|
||||
<Filter>Source Files\connpool</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\connpool\tcp_manager.cpp">
|
||||
<Filter>Source Files\connpool</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\connpool\tcp_ipc.cpp">
|
||||
<Filter>Source Files\connpool</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\http\http_request_pool.cpp">
|
||||
<Filter>Source Files\http</Filter>
|
||||
</ClCompile>
|
||||
@ -900,6 +918,24 @@
|
||||
<ClInclude Include="include\acl_cpp\connpool\connect_pool.hpp">
|
||||
<Filter>Header Files\connpool</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_reader.hpp">
|
||||
<Filter>Header Files\connpool</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_sender.hpp">
|
||||
<Filter>Header Files\connpool</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_client.hpp">
|
||||
<Filter>Header Files\connpool</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_pool.hpp">
|
||||
<Filter>Header Files\connpool</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_manager.hpp">
|
||||
<Filter>Header Files\connpool</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_ipc.hpp">
|
||||
<Filter>Header Files\connpool</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\acl_cpp\http\http_request_pool.hpp">
|
||||
<Filter>Header Files\http</Filter>
|
||||
</ClInclude>
|
||||
|
@ -404,6 +404,12 @@ copy $(TargetName).pdb ..\dist\lib\win64\$(TargetName).pdb /Y
|
||||
<ClCompile Include="src\connpool\connect_manager.cpp" />
|
||||
<ClCompile Include="src\connpool\connect_monitor.cpp" />
|
||||
<ClCompile Include="src\connpool\connect_pool.cpp" />
|
||||
<ClCompile Include="src\connpool\tcp_reader.cpp" />
|
||||
<ClCompile Include="src\connpool\tcp_sender.cpp" />
|
||||
<ClCompile Include="src\connpool\tcp_client.cpp" />
|
||||
<ClCompile Include="src\connpool\tcp_pool.cpp" />
|
||||
<ClCompile Include="src\connpool\tcp_manager.cpp" />
|
||||
<ClCompile Include="src\connpool\tcp_ipc.cpp" />
|
||||
<ClCompile Include="src\db\db_handle.cpp" />
|
||||
<ClCompile Include="src\db\db_mysql.cpp" />
|
||||
<ClCompile Include="src\db\db_pgsql.cpp" />
|
||||
@ -582,6 +588,12 @@ copy $(TargetName).pdb ..\dist\lib\win64\$(TargetName).pdb /Y
|
||||
<ClInclude Include="include\acl_cpp\connpool\connect_manager.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\connect_monitor.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\connect_pool.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_reader.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_sender.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_client.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_pool.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_manager.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_ipc.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\db\db_handle.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\db\db_mysql.hpp" />
|
||||
<ClInclude Include="include\acl_cpp\db\db_pgsql.hpp" />
|
||||
|
@ -346,6 +346,24 @@
|
||||
<ClCompile Include="src\connpool\connect_pool.cpp">
|
||||
<Filter>Source Files\connpool</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\connpool\tcp_reader.cpp">
|
||||
<Filter>Source Files\connpool</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\connpool\tcp_sender.cpp">
|
||||
<Filter>Source Files\connpool</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\connpool\tcp_client.cpp">
|
||||
<Filter>Source Files\connpool</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\connpool\tcp_pool.cpp">
|
||||
<Filter>Source Files\connpool</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\connpool\tcp_manager.cpp">
|
||||
<Filter>Source Files\connpool</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\connpool\tcp_ipc.cpp">
|
||||
<Filter>Source Files\connpool</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\http\http_request_pool.cpp">
|
||||
<Filter>Source Files\http</Filter>
|
||||
</ClCompile>
|
||||
@ -900,6 +918,24 @@
|
||||
<ClInclude Include="include\acl_cpp\connpool\connect_pool.hpp">
|
||||
<Filter>Header Files\connpool</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_reader.hpp">
|
||||
<Filter>Header Files\connpool</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_sender.hpp">
|
||||
<Filter>Header Files\connpool</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_client.hpp">
|
||||
<Filter>Header Files\connpool</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_pool.hpp">
|
||||
<Filter>Header Files\connpool</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_manager.hpp">
|
||||
<Filter>Header Files\connpool</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\acl_cpp\connpool\tcp_ipc.hpp">
|
||||
<Filter>Header Files\connpool</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\acl_cpp\http\http_request_pool.hpp">
|
||||
<Filter>Header Files\http</Filter>
|
||||
</ClInclude>
|
||||
|
103
lib_acl_cpp/src/connpool/tcp_client.cpp
Normal file
103
lib_acl_cpp/src/connpool/tcp_client.cpp
Normal file
@ -0,0 +1,103 @@
|
||||
#include "acl_stdafx.hpp"
|
||||
#ifndef ACL_PREPARE_COMPILE
|
||||
#include "acl_cpp/stdlib/log.hpp"
|
||||
#include "acl_cpp/stdlib/util.hpp"
|
||||
#include "acl_cpp/stdlib/string.hpp"
|
||||
#include "acl_cpp/stream/socket_stream.hpp"
|
||||
#include "acl_cpp/connpool/tcp_sender.hpp"
|
||||
#include "acl_cpp/connpool/tcp_reader.hpp"
|
||||
#include "acl_cpp/connpool/tcp_client.hpp"
|
||||
#endif
|
||||
|
||||
namespace acl
|
||||
{
|
||||
|
||||
tcp_client::tcp_client(const char* addr, int conn_timeout, int rw_timeout)
|
||||
: conn_timeout_(conn_timeout)
|
||||
, rw_timeout_(rw_timeout)
|
||||
{
|
||||
addr_ = acl_mystrdup(addr);
|
||||
conn_ = new socket_stream;
|
||||
sender_ = new tcp_sender(*conn_);
|
||||
reader_ = NULL;
|
||||
}
|
||||
|
||||
tcp_client::~tcp_client(void)
|
||||
{
|
||||
delete reader_;
|
||||
delete sender_;
|
||||
delete conn_;
|
||||
acl_myfree(addr_);
|
||||
}
|
||||
|
||||
bool tcp_client::open(void)
|
||||
{
|
||||
if (conn_->open(addr_, conn_timeout_, rw_timeout_) == false)
|
||||
{
|
||||
logger_error("connnect %s error %s", addr_, last_serror());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool tcp_client::try_open(bool* reuse_conn)
|
||||
{
|
||||
if (conn_->opened())
|
||||
{
|
||||
*reuse_conn = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
*reuse_conn = false;
|
||||
if (conn_->open(addr_, conn_timeout_, rw_timeout_) == false)
|
||||
{
|
||||
logger_error("connect %s error %s", addr_, last_serror());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool tcp_client::send(const void* data, unsigned int len, string* out /* = NULL */)
|
||||
{
|
||||
bool have_retried = false, reuse_conn;
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (try_open(&reuse_conn) == false)
|
||||
{
|
||||
logger_error("connect server error");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (sender_->send(data, len) == false)
|
||||
{
|
||||
if (have_retried || !reuse_conn)
|
||||
{
|
||||
logger_error("send head error");
|
||||
return false;
|
||||
}
|
||||
|
||||
have_retried = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (out == NULL)
|
||||
return true;
|
||||
|
||||
if (reader_ == NULL)
|
||||
reader_ = new tcp_reader(*conn_);
|
||||
if (reader_->read(*out))
|
||||
return true;
|
||||
if (have_retried || !reuse_conn)
|
||||
{
|
||||
logger_error("read data error");
|
||||
return false;
|
||||
}
|
||||
|
||||
have_retried = true;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace acl
|
95
lib_acl_cpp/src/connpool/tcp_ipc.cpp
Normal file
95
lib_acl_cpp/src/connpool/tcp_ipc.cpp
Normal file
@ -0,0 +1,95 @@
|
||||
/**
|
||||
* Copyright (C) 2017-2018
|
||||
* All rights reserved.
|
||||
*
|
||||
* AUTHOR(S)
|
||||
* Zheng Shuxin
|
||||
* E-mail: zhengshuxin@qiyi.com
|
||||
*
|
||||
* VERSION
|
||||
* Tue 08 Aug 2017 03:10:44 PM CST
|
||||
*/
|
||||
|
||||
#include "acl_stdafx.hpp"
|
||||
#ifndef ACL_PREPARE_COMPILE
|
||||
#include "acl_cpp/stdlib/string.hpp"
|
||||
#include "acl_cpp/stdlib/log.hpp"
|
||||
#include "acl_cpp/connpool/tcp_manager.hpp"
|
||||
#include "acl_cpp/connpool/tcp_pool.hpp"
|
||||
#include "acl_cpp/connpool/tcp_client.hpp"
|
||||
#include "acl_cpp/connpool/tcp_ipc.hpp"
|
||||
#endif
|
||||
|
||||
namespace acl
|
||||
{
|
||||
|
||||
tcp_ipc::tcp_ipc(void)
|
||||
: max_(0)
|
||||
, ttl_(60)
|
||||
, conn_timeout_(10)
|
||||
, rw_timeout_(10)
|
||||
{
|
||||
manager_ = new tcp_manager;
|
||||
}
|
||||
|
||||
tcp_ipc::~tcp_ipc(void)
|
||||
{
|
||||
delete manager_;
|
||||
}
|
||||
|
||||
tcp_ipc& tcp_ipc::set_limit(int max)
|
||||
{
|
||||
max_ = max;
|
||||
return *this;
|
||||
}
|
||||
|
||||
tcp_ipc& tcp_ipc::set_idle(int ttl)
|
||||
{
|
||||
ttl_ = ttl;
|
||||
return *this;
|
||||
}
|
||||
|
||||
tcp_ipc& tcp_ipc::set_conn_timeout(int timeout)
|
||||
{
|
||||
conn_timeout_ = timeout;
|
||||
return *this;
|
||||
}
|
||||
|
||||
tcp_ipc& tcp_ipc::set_rw_timeout(int timeout)
|
||||
{
|
||||
rw_timeout_ = timeout;
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool tcp_ipc::send(const char* addr, const void* data, unsigned int len,
|
||||
string* out /* = NULL */)
|
||||
{
|
||||
tcp_pool* pool = (tcp_pool*) manager_->peek(addr);
|
||||
if (pool == NULL)
|
||||
pool = &(tcp_pool&) manager_->set(
|
||||
addr, max_, conn_timeout_, rw_timeout_);
|
||||
return send(*pool, data, len, out);
|
||||
}
|
||||
|
||||
bool tcp_ipc::send(tcp_pool& pool, const void* data, unsigned int len,
|
||||
string* out)
|
||||
{
|
||||
tcp_client* conn = (tcp_client*) pool.peek();
|
||||
if (conn == NULL)
|
||||
{
|
||||
logger_error("no connection available, addr=%s",
|
||||
pool.get_addr());
|
||||
return false;
|
||||
}
|
||||
|
||||
if (conn->send(data, len, out) == false)
|
||||
{
|
||||
pool.put(conn, false);
|
||||
return false;
|
||||
}
|
||||
|
||||
pool.put(conn);
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace acl
|
25
lib_acl_cpp/src/connpool/tcp_manager.cpp
Normal file
25
lib_acl_cpp/src/connpool/tcp_manager.cpp
Normal file
@ -0,0 +1,25 @@
|
||||
#include "acl_stdafx.hpp"
|
||||
#ifndef ACL_PREPARE_COMPILE
|
||||
#include "acl_cpp/connpool/tcp_pool.hpp"
|
||||
#include "acl_cpp/connpool/tcp_manager.hpp"
|
||||
#endif
|
||||
|
||||
namespace acl
|
||||
{
|
||||
|
||||
tcp_manager::tcp_manager(void)
|
||||
{
|
||||
}
|
||||
|
||||
tcp_manager::~tcp_manager(void)
|
||||
{
|
||||
}
|
||||
|
||||
connect_pool* tcp_manager::create_pool(const char* addr, size_t count,
|
||||
size_t idx)
|
||||
{
|
||||
tcp_pool* pool = new tcp_pool(addr, count, idx);
|
||||
return pool;
|
||||
}
|
||||
|
||||
} // namespace acl
|
25
lib_acl_cpp/src/connpool/tcp_pool.cpp
Normal file
25
lib_acl_cpp/src/connpool/tcp_pool.cpp
Normal file
@ -0,0 +1,25 @@
|
||||
#include "acl_stdafx.hpp"
|
||||
#ifndef ACL_PREPARE_COMPILE
|
||||
#include "acl_cpp/connpool/tcp_client.hpp"
|
||||
#include "acl_cpp/connpool/tcp_pool.hpp"
|
||||
#endif
|
||||
|
||||
namespace acl
|
||||
{
|
||||
|
||||
tcp_pool::tcp_pool(const char* addr, size_t count, size_t idx /* = 0 */)
|
||||
: connect_pool(addr, count, idx)
|
||||
{
|
||||
}
|
||||
|
||||
tcp_pool::~tcp_pool(void)
|
||||
{
|
||||
}
|
||||
|
||||
connect_client* tcp_pool::create_connect(void)
|
||||
{
|
||||
tcp_client* conn = new tcp_client(addr_, conn_timeout_, rw_timeout_);
|
||||
return conn;
|
||||
}
|
||||
|
||||
} // namespace acl
|
55
lib_acl_cpp/src/connpool/tcp_reader.cpp
Normal file
55
lib_acl_cpp/src/connpool/tcp_reader.cpp
Normal file
@ -0,0 +1,55 @@
|
||||
/**
|
||||
* Copyright (C) 2017-2018
|
||||
* All rights reserved.
|
||||
*
|
||||
* AUTHOR(S)
|
||||
* Zheng Shuxin
|
||||
* E-mail: zhengshuxin@qiyi.com
|
||||
*
|
||||
* VERSION
|
||||
* Tue 08 Aug 2017 01:59:35 PM CST
|
||||
*/
|
||||
|
||||
#include "acl_stdafx.hpp"
|
||||
#ifndef ACL_PREPARE_COMPILE
|
||||
#include "acl_cpp/stdlib/log.hpp"
|
||||
#include "acl_cpp/stdlib/util.hpp"
|
||||
#include "acl_cpp/stdlib/string.hpp"
|
||||
#include "acl_cpp/stream/socket_stream.hpp"
|
||||
#include "acl_cpp/connpool/tcp_reader.hpp"
|
||||
#endif
|
||||
|
||||
namespace acl
|
||||
{
|
||||
|
||||
tcp_reader::tcp_reader(socket_stream& conn)
|
||||
: conn_(&conn)
|
||||
{
|
||||
}
|
||||
|
||||
bool tcp_reader::read(string& out)
|
||||
{
|
||||
int len;
|
||||
if (conn_->read(len) == false)
|
||||
{
|
||||
logger_error("read head error!");
|
||||
return false;
|
||||
}
|
||||
|
||||
len = ntohl(len);
|
||||
if (len <= 0)
|
||||
{
|
||||
logger_error("invalid len=%d", len);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (conn_->read(out, (size_t) len) == false)
|
||||
{
|
||||
logger_error("read body error %s", last_serror());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace acl
|
39
lib_acl_cpp/src/connpool/tcp_sender.cpp
Normal file
39
lib_acl_cpp/src/connpool/tcp_sender.cpp
Normal file
@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Copyright (C) 2017-2018
|
||||
* All rights reserved.
|
||||
*
|
||||
* AUTHOR(S)
|
||||
* Zheng Shuxin
|
||||
* E-mail: zhengshuxin@qiyi.com
|
||||
*
|
||||
* VERSION
|
||||
* Tue 08 Aug 2017 02:13:31 PM CST
|
||||
*/
|
||||
|
||||
#include "acl_stdafx.hpp"
|
||||
#ifndef ACL_PREPARE_COMPILE
|
||||
#include "acl_cpp/stream/socket_stream.hpp"
|
||||
#include "acl_cpp/connpool/tcp_sender.hpp"
|
||||
#endif
|
||||
|
||||
namespace acl
|
||||
{
|
||||
|
||||
tcp_sender::tcp_sender(socket_stream& conn)
|
||||
: conn_(&conn)
|
||||
{
|
||||
}
|
||||
|
||||
bool tcp_sender::send(const void* data, unsigned int len)
|
||||
{
|
||||
unsigned int n = htonl(len);
|
||||
|
||||
v_[0].iov_base = &n;
|
||||
v_[0].iov_len = sizeof(n);
|
||||
v_[1].iov_base = (void*) data;
|
||||
v_[1].iov_len = len;
|
||||
|
||||
return conn_->writev(v_, 2) > 0;
|
||||
}
|
||||
|
||||
} // namespace acl
|
Loading…
Reference in New Issue
Block a user