-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add dll dependency. add keepalive param:5s(default)
add keepalive:5s. win:buf size 2M. DefaultFakeConn add rsock dll dependency
- Loading branch information
nmq
committed
May 7, 2018
1 parent
13ce521
commit d621fa8
Showing
27 changed files
with
377 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// | ||
// Created by System Administrator on 5/7/18. | ||
// | ||
|
||
#include <cstdlib> | ||
#include <plog/Log.h> | ||
#include "NetConnKeepAliveHelper.h" | ||
#include "../conn/IAppGroup.h" | ||
#include "../conn/INetGroup.h" | ||
#include "NetConnKeepAlive.h" | ||
|
||
NetConnKeepAliveHelper::NetConnKeepAliveHelper(IAppGroup *group, uv_loop_t *loop, bool active) { | ||
mAppGroup = group; | ||
mKeepAlive = new NetConnKeepAlive(this); | ||
if (active) { | ||
setupTimer(loop); | ||
} | ||
} | ||
|
||
void NetConnKeepAliveHelper::Close() { | ||
if (mFlushTimer) { | ||
uv_timer_stop(mFlushTimer); | ||
uv_close(reinterpret_cast<uv_handle_t *>(mFlushTimer), close_cb); | ||
mFlushTimer = nullptr; | ||
} | ||
if (mKeepAlive) { | ||
mKeepAlive->Close(); | ||
delete mKeepAlive; | ||
mKeepAlive = nullptr; | ||
} | ||
} | ||
|
||
int NetConnKeepAliveHelper::OnSendResponse(uint8_t cmd, ssize_t nread, const rbuf_t &rbuf) { | ||
return mAppGroup->doSendCmd(cmd, nread, rbuf); | ||
} | ||
|
||
int NetConnKeepAliveHelper::OnSendRequest(uint8_t cmd, ssize_t nread, const rbuf_t &rbuf) { | ||
return mAppGroup->doSendCmd(cmd, nread, rbuf); | ||
} | ||
|
||
int NetConnKeepAliveHelper::OnRecvResponse(IntKeyType connKey) { | ||
return RemoveRequest(connKey); | ||
} | ||
|
||
INetConn *NetConnKeepAliveHelper::ConnOfIntKey(IntKeyType connKey) { | ||
return mAppGroup->NetGroup()->ConnOfIntKey(connKey); | ||
} | ||
|
||
int NetConnKeepAliveHelper::SendNetConnRst(const ConnInfo &src, IntKeyType connKey) { | ||
return mAppGroup->sendNetConnRst(src, connKey); | ||
} | ||
|
||
void NetConnKeepAliveHelper::setupTimer(uv_loop_t *loop) { | ||
if (!mFlushTimer) { | ||
mFlushTimer = static_cast<uv_timer_t *>(malloc(sizeof(uv_timer_t))); | ||
uv_timer_init(loop, mFlushTimer); | ||
mFlushTimer->data = this; | ||
uv_timer_start(mFlushTimer, timer_cb, FIRST_FLUSH_DELAY, FLUSH_INTERVAL); | ||
} | ||
} | ||
|
||
void NetConnKeepAliveHelper::timer_cb(uv_timer_t *timer) { | ||
NetConnKeepAliveHelper *helper = static_cast<NetConnKeepAliveHelper *>(timer->data); | ||
helper->onFlush(); | ||
} | ||
|
||
void NetConnKeepAliveHelper::onFlush() { | ||
auto conns = mAppGroup->NetGroup()->GetAllConns(); | ||
for (auto &e: conns) { | ||
auto *conn = dynamic_cast<INetConn *>(e.second); | ||
auto it = mReqMap.find(conn->IntKey()); | ||
if (it != mReqMap.end()) { | ||
it->second++; | ||
} else { | ||
mReqMap.emplace(conn->IntKey(), 0); | ||
} | ||
|
||
if (it == mReqMap.end() || it->second < MAX_RETRY) { // new or still valid | ||
mKeepAlive->SendRequest(conn->IntKey()); | ||
} | ||
} | ||
|
||
// todo: make an interface. and move these into inetgroup | ||
auto aCopy = mReqMap; | ||
for (auto &e: aCopy) { | ||
if (e.second >= MAX_RETRY) { // keep alive timeout | ||
LOGE << "keepalive timeout"; | ||
mAppGroup->onNetconnDead(e.first); | ||
RemoveRequest(e.first); | ||
} | ||
} | ||
} | ||
|
||
INetConnKeepAlive *NetConnKeepAliveHelper::GetIKeepAlive() const { | ||
return mKeepAlive; | ||
} | ||
|
||
int NetConnKeepAliveHelper::RemoveRequest(IntKeyType connKey) { | ||
return mReqMap.erase(connKey); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// | ||
// Created by System Administrator on 5/7/18. | ||
// | ||
|
||
#ifndef RSOCK_NETCONNKEEPALIVEHELPER_H | ||
#define RSOCK_NETCONNKEEPALIVEHELPER_H | ||
|
||
#include <map> | ||
|
||
#include "INetConnKeepAlive.h" | ||
|
||
class IAppGroup; | ||
|
||
class NetConnKeepAliveHelper : public INetConnKeepAlive::INetConnAliveHelper { | ||
public: | ||
using IntKeyType = uint32_t; | ||
|
||
explicit NetConnKeepAliveHelper(IAppGroup *group, uv_loop_t *loop, bool active); | ||
|
||
int OnSendResponse(uint8_t cmd, ssize_t nread, const rbuf_t &rbuf) override; | ||
|
||
int OnRecvResponse(IntKeyType connKey) override; | ||
|
||
int OnSendRequest(uint8_t cmd, ssize_t nread, const rbuf_t &rbuf) override; | ||
|
||
INetConn *ConnOfIntKey(IntKeyType connKey) override; | ||
|
||
int SendNetConnRst(const ConnInfo &src, IntKeyType connKey) override; | ||
|
||
void Close() override; | ||
|
||
INetConnKeepAlive *GetIKeepAlive() const override; | ||
|
||
int RemoveRequest(IntKeyType connKey) override; | ||
|
||
private: | ||
void onFlush(); | ||
|
||
private: | ||
void setupTimer(uv_loop_t *loop); | ||
|
||
static void timer_cb(uv_timer_t *timer); | ||
|
||
private: | ||
const int MAX_RETRY = 3; | ||
const uint32_t FLUSH_INTERVAL = 5000; // every 2sec | ||
const uint32_t FIRST_FLUSH_DELAY = 5000; // on app start | ||
IAppGroup *mAppGroup = nullptr; | ||
uv_timer_t *mFlushTimer = nullptr; | ||
std::map<IntKeyType, int> mReqMap; | ||
INetConnKeepAlive *mKeepAlive = nullptr; | ||
}; | ||
|
||
#endif //RSOCK_NETCONNKEEPALIVEHELPER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// | ||
// Created by System Administrator on 5/7/18. | ||
// | ||
|
||
#include "../conn/IAppGroup.h" | ||
#include "ResetHelper.h" | ||
#include "ConnReset.h" | ||
#include "../util/rsutil.h" | ||
|
||
ResetHelper::ResetHelper(IAppGroup *appGroup) { | ||
mAppGroup = appGroup; | ||
mReset = new ConnReset(this); | ||
} | ||
|
||
void ResetHelper::Close() { | ||
if (mReset) { | ||
mReset->Close(); | ||
delete mReset; | ||
mReset = nullptr; | ||
} | ||
mAppGroup = nullptr; | ||
} | ||
|
||
int ResetHelper::OnSendNetConnReset(uint8_t cmd, const ConnInfo &src, ssize_t nread, const rbuf_t &rbuf) { | ||
if (cmd == EncHead::TYPE_NETCONN_RST) { | ||
auto rbuf2 = new_buf(0, "", (void *) &src); | ||
mAppGroup->Output(rbuf2.len, rbuf2); // directly send | ||
} | ||
return mAppGroup->doSendCmd(cmd, nread, rbuf); | ||
} | ||
|
||
int ResetHelper::OnSendConvRst(uint8_t cmd, ssize_t nread, const rbuf_t &rbuf) { | ||
return mAppGroup->doSendCmd(cmd, nread, rbuf); | ||
} | ||
|
||
int ResetHelper::OnRecvNetconnRst(const ConnInfo &src, IntKeyType key) { | ||
return mAppGroup->onPeerNetConnRst(src, key); | ||
} | ||
|
||
int ResetHelper::OnRecvConvRst(const ConnInfo &src, uint32_t conv) { | ||
return mAppGroup->onPeerConvRst(src, conv); | ||
} | ||
|
||
IReset *ResetHelper::GetReset() { | ||
return mReset; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// Created by System Administrator on 5/7/18. | ||
// | ||
|
||
#ifndef RSOCK_RESETHELPER_H | ||
#define RSOCK_RESETHELPER_H | ||
|
||
#include "IReset.h" | ||
|
||
class IAppGroup; | ||
|
||
class ResetHelper : public IReset::IRestHelper { | ||
public: | ||
explicit ResetHelper(IAppGroup *appGroup); | ||
|
||
void Close() override; | ||
|
||
int OnSendNetConnReset(uint8_t cmd, const ConnInfo &src, ssize_t nread, const rbuf_t &rbuf) override; | ||
|
||
int OnSendConvRst(uint8_t cmd, ssize_t nread, const rbuf_t &rbuf) override; | ||
|
||
int OnRecvNetconnRst(const ConnInfo &src, IntKeyType key) override; | ||
|
||
int OnRecvConvRst(const ConnInfo &src, uint32_t conv) override; | ||
|
||
IReset *GetReset() override; | ||
|
||
private: | ||
IAppGroup *mAppGroup = nullptr; | ||
IReset *mReset = nullptr; | ||
}; | ||
|
||
|
||
#endif //RSOCK_RESETHELPER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// | ||
// Created by System Administrator on 5/7/18. | ||
// | ||
|
||
#include "DefaultFakeConn.h" | ||
|
||
DefaultFakeConn::DefaultFakeConn() : INetConn("DefaultFakeConn") {} | ||
|
||
bool DefaultFakeConn::Alive() { | ||
return true; | ||
} | ||
|
||
bool DefaultFakeConn::IsUdp() { | ||
return false; | ||
} | ||
|
||
ConnInfo *DefaultFakeConn::GetInfo() { | ||
return nullptr; | ||
} | ||
|
||
IntKeyType DefaultFakeConn::IntKey() { | ||
return 0; | ||
} | ||
|
||
int DefaultFakeConn::OnRecv(ssize_t nread, const rbuf_t &rbuf) { | ||
return IConn::OnRecv(nread, rbuf); | ||
} |
Oops, something went wrong.