Skip to content

Commit

Permalink
optimize pool persistance
Browse files Browse the repository at this point in the history
  • Loading branch information
nmq committed Mar 8, 2018
1 parent da2ea4c commit a1d8933
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 24 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ Parameter explanation:
-f json config file
--lcapIp=[ip] Internet IP. Can omit -d if this parameter sepcified.
--unPath Local unix domain socket. Not available now.
-p, --ports=[...] tcp/udp port list for rsock server. e.g.10001,10010(2 ports); 10001-10010(11 ports); 80,443,10001-10010(12个). NO white spaces allowed.
--duration=[timeSec] Time for app connection to persist if no data is transfered in the app connection. unit: seconds.
-p, --ports=[...] tcp/udp port list for rsock server. e.g.10001,10010(2 ports); 10001-10010(11 ports); 80,443,10001-10010(12 ports). NO white spaces allowed.
--duration=[timeSec] Time for app connection to persist if no data is transfered in the app connection. unit: seconds. defalt 30s
--hash=[hashKey] Not for encryption. Only for judgement if data belong to rsock. REPEAT: rsock don't encrypt data. Encryption is done by kcptun.
--type=[tcp|udp|all] type of communication. One of tcp, udp or all. Default is tcp.
--daemon=[1|0] Run as daemon. 1 yes. 0 no. default 1.
Expand All @@ -106,7 +106,7 @@ Parameter explanation:
2. Client connects to all of server ports.
3. For each of communications, client send data by libnet to one of server ports, which should have been connected.
4. Server receive data by libpcap. It's same for server to send data to client. Now they make communication.
5. For application, local_ip:app_udp_port and server_ip:app_udp_port make a connection。If no data flows thourgh this connection, it will be closed.
5. For application, local_ip:app_udp_port and server_ip:app_udp_port make a connection。If no data flows thourgh this connection for a period of time(default 30s), it will be closed.
6. When client receive rst or fin, it will close that real network connection and reconnect to server.

#### Disadvantage
Expand Down Expand Up @@ -222,7 +222,7 @@ If servers run normally, try to restart shadowsocks client.

1. windows support

1. Add other ways to communicate, e.g. icmp, dns.
1. Add idle mode. Stop repeatedly connect to server if no data for a period.

1. Try to introduce reliable data transfer. Listen tcp directly and remove kcptun.

Expand Down
2 changes: 1 addition & 1 deletion bean/RConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ int RConfig::Parse(bool is_server, int argc, const char *const *argv) {
"(e.g.3000,3001,4000-4050. No blank spaces or other characters allowed)", {'p', "ports"});
ValueFlag<uint32_t> duration(opt, "",
"Interval(sec) to invalid connection. Client need to set to same value with server. "
"(default 20s. min: 10s, max: 60s.)", {"duration"});
"(default 30s. min: 10s, max: 60s.)", {"duration"});
ValueFlag<std::string> key(opt, "HashKey", "Key to check validation of packet. (default hello1235)", {"hash"});
ValueFlag<std::string> type(opt, "tcp|udp|all",
"Type used to communicate with server. tcp for tcp only mode, udp for udp only mode. "
Expand Down
2 changes: 1 addition & 1 deletion conn/IAppGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class IAppGroup : public IGroup, public ITcpObserver {
private:
const int MAX_RETRY = 3;
const uint32_t FLUSH_INTERVAL = 5000; // every 5sec
const uint32_t FIRST_FLUSH_DELAY = 30000; // on app start
const uint32_t FIRST_FLUSH_DELAY = 5000; // on app start
IAppGroup *mAppGroup = nullptr;
uv_timer_t *mFlushTimer = nullptr;
std::map<IntKeyType, int> mReqMap;
Expand Down
4 changes: 2 additions & 2 deletions doc/README.zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ I
--lcapIp=[ip] 外网ip。如果指定了这个参数,可以不指定 -d.
--unPath 本地监听的unix域套接字地址。暂时不可用
-p, --ports=[...] 服务器端使用的tcp/udp端口。如:10001,10010(2个) ; 10001-10010(11个); 80,443,10001-10010(12个)。中间无空格。
--duration=[timeSec] 一段duration时间内,app连接如果无数据通信,会被关闭。单位秒。
--duration=[timeSec] 一段duration时间内,app连接如果无数据通信,会被关闭。单位秒。默认30s
--hash=[hashKey] 不是用来加密的。只是用来判断是否属于本程序的特征。再重复一次, 数据的加密由kcptun进行)
--type=[tcp|udp|all] 通信的方式。可以选择tcp,udp或者all。默认是tcp。
--daemon=[1|0] 是否以后台进程运行。1是,0否。默认是1。
Expand Down Expand Up @@ -213,7 +213,7 @@ kcptun的下载速度. 速度在2M左右。

1. windows 支持

1. 尝试增加其他通信方式,比如icmp,dns
1. 增加闲置模式。当没有数据通过的时候,不要一直重连服务器。

1. 尝试引入类似kcp的可靠数据传输。直接监听tcp,取消kcptun中转。

Expand Down
2 changes: 1 addition & 1 deletion net/INetManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "NetUtil.h"
#include "../conn/BtmUdpConn.h"

INetManager::INetManager(uv_loop_t *loop, TcpAckPool *ackPool) {
INetManager::INetManager(uv_loop_t *loop, TcpAckPool *ackPool) : POOL_PERSIST_MS(ackPool->PersistMs()) {
mLoop = loop;
mTcpAckPool = ackPool;
}
Expand Down
5 changes: 1 addition & 4 deletions net/INetManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ class TcpAckPool;

struct ConnInfo;

// todo: INetManager 到底需要哪些功能,注意无论是server还是client,都需要绑定udp.
// server 监听tcp, client连接server,抽象冲出
// Dial方法的命名,是否需要更改
class INetManager {
public:
explicit INetManager(uv_loop_t *loop, TcpAckPool *ackPool);
Expand Down Expand Up @@ -72,7 +69,7 @@ class INetManager {
const std::chrono::milliseconds BLOCK_WAIT_MS = std::chrono::milliseconds(500);

const uint64_t FLUSH_INTERVAL = 1000; // 1s
const uint64_t POOL_PERSIST_MS = 30 * 1000;
const uint64_t POOL_PERSIST_MS = 30000; // 30s
std::map<std::string, ConnHelper> mPool;
uv_timer_t *mFlushTimer = nullptr;

Expand Down
3 changes: 1 addition & 2 deletions net/TcpAckPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <plog/Log.h>
#include "TcpAckPool.h"

TcpAckPool::TcpAckPool(uv_loop_t *loop) {
TcpAckPool::TcpAckPool(uv_loop_t *loop, uint64_t expireMs): EXPIRE_INTERVAL(expireMs) {
mLoop = loop;
}

Expand Down Expand Up @@ -49,7 +49,6 @@ bool TcpAckPool::Wait2Info(TcpInfo &info, const std::chrono::milliseconds milliS
return ok;
}

// todo
void TcpAckPool::Flush(uint64_t now) {
for (auto it = mInfoPool.begin(); it != mInfoPool.end();) {
if (it->second <= now) {
Expand Down
8 changes: 5 additions & 3 deletions net/TcpAckPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// store ack information of incomming connection
class TcpAckPool {
public:
explicit TcpAckPool(uv_loop_t *loop);
explicit TcpAckPool(uv_loop_t *loop, uint64_t expireMs);

// sp or dp == 0 is not valid
bool AddInfoFromPeer(const TcpInfo &infoFromPeer, uint8_t flags);
Expand All @@ -31,9 +31,11 @@ class TcpAckPool {
void Flush(uint64_t now);

void Close() {}

std::string Dump();

uint64_t PersistMs() const { return EXPIRE_INTERVAL; }

protected:
// no lock protection
bool getInfoIfExists(TcpInfo &info);
Expand All @@ -44,7 +46,7 @@ class TcpAckPool {
};

private:
const uint64_t EXPIRE_INTERVAL = 10000;
const uint64_t EXPIRE_INTERVAL = 30000; // by default.
std::map<TcpInfo, uint64_t, TcpCmpFn> mInfoPool;
std::mutex mMutex;
std::condition_variable mCondVar;
Expand Down
2 changes: 1 addition & 1 deletion src/ISockApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ int ISockApp::doInit() {
}
LOGD << "conf: " << mConf.to_json().dump();

mAckPool = new TcpAckPool(mLoop);
mAckPool = new TcpAckPool(mLoop, mConf.param.conn_duration_sec * 1000);

mNetManager = CreateNetManager(mConf, mLoop, mAckPool);
if (mNetManager->Init()) {
Expand Down
5 changes: 1 addition & 4 deletions xbuild/cmake/base.cmake
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
set(RSOCK_LINKER_FLAGS "${RSOCK_LINKER_FLAGS} -static-libgcc -static-libstdc++")
if (${RSOCK_STATIC_BIN}) # for docker deploy
set(RSOCK_LINKER_FLAGS "${RSOCK_LINKER_FLAGS} -static")
endif ()
set(RSOCK_LINKER_FLAGS "${RSOCK_LINKER_FLAGS} -static-libgcc -static-libstdc++ -static")
endif ()

if (RSOCK_LINKER_FLAGS)
Expand Down
2 changes: 1 addition & 1 deletion xbuild/sh/travis_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function build_on_travis {
echo "build success"
echo "check library dependency ..."
for f in *_rsock_${OS}; do
if which readelf; then # on travis, linux is statically built
if which readelf; then # on travis, linux is statically linked
readelf -d ${f}
readelf -d ${f} |grep '++'
if [ $? -eq 0 ]; then
Expand Down

0 comments on commit a1d8933

Please sign in to comment.