Skip to content

Commit 660fca5

Browse files
authored
Merge pull request #22 from youngmonkeys/fix-ping-schedule-issue
fix ping schedule issue
2 parents fe9a501 + 2c2bc31 commit 660fca5

File tree

14 files changed

+173
-15
lines changed

14 files changed

+173
-15
lines changed

.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
example/example
2+
libezyfox-client.a
3+
.idea/
4+
CMakeFiles/
5+
Makefile
6+
cmake-build-debug/
7+
cmake_install.cmake
8+
example/.idea/
9+
example/CMakeCache.txt
10+
example/CMakeFiles/
11+
example/Makefile
12+
example/Testing/
13+
example/cmake_install.cmake
14+
example/example.cbp
15+
ezyfox-client.cbp
116
**/xcuserdata/
217
.DS_Store
318
*.DS_Store

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ protected:
8383
}
8484
};
8585

86-
class ExAppAccessHandler : public handler::EzyAccessAppHandler {
86+
class ExAppAccessHandler : public handler::EzyAppAccessHandler {
8787
protected:
8888
void postHandle(entity::EzyApp* app, entity::EzyArray* data) {
8989
auto obj = new entity::EzyObject();

example/CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
cmake_minimum_required(VERSION 3.6)
2+
3+
set( CMAKE_CXX_FLAGS "-std=gnu++11 -pthread")
4+
5+
add_definitions(-DEZY_DEBUG)
6+
7+
set(APP_NAME example)
8+
9+
project(${APP_NAME})
10+
11+
add_subdirectory(../ ../)
12+
13+
# add cross-platforms source files and header files
14+
list(APPEND APP_SOURCE
15+
main.cpp
16+
)
17+
list(APPEND APP_HEADER
18+
)
19+
20+
# mark app complie info and libs info
21+
set(all_code_files
22+
${APP_HEADER}
23+
${APP_SOURCE}
24+
)
25+
26+
add_executable(${APP_NAME} main.cpp)
27+
28+
target_link_libraries(${APP_NAME} ezyfox-client)

example/main.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//
2+
// main.cpp
3+
// ezyfox-console
4+
//
5+
// Created by Dzung on 08/08/2021.
6+
//
7+
8+
#include <iostream>
9+
#include "EzyHeaders.h"
10+
11+
const char* ZONE_NAME = "example";
12+
const char* APP_NAME = "hello-world";
13+
14+
class HandshakeHandler : public handler::EzyHandshakeHandler {
15+
protected:
16+
request::EzyRequest* getLoginRequest() {
17+
auto request = request::EzyLoginRequest::create();
18+
request->setZoneName(ZONE_NAME);
19+
request->setUsername("dungtv");
20+
request->setPassword("123456");
21+
return request;
22+
};
23+
};
24+
25+
class LoginSuccessHandler : public handler::EzyLoginSuccessHandler {
26+
protected:
27+
void handleLoginSuccess(entity::EzyValue* responseData) {
28+
auto appRequest = request::EzyAppAccessRequest::create();
29+
appRequest->setAppName(APP_NAME);
30+
mClient->send(appRequest);
31+
}
32+
};
33+
34+
class AppAccessHandler : public handler::EzyAppAccessHandler {
35+
protected:
36+
void postHandle(entity::EzyApp* app, entity::EzyArray* data) {
37+
auto requestData = new entity::EzyObject();
38+
requestData->setString("who", "C/C++ Developer");
39+
app->send("greet", requestData);
40+
}
41+
};
42+
43+
class GreetResponseHandler : public handler::EzyAbstractAppDataHandler<entity::EzyObject> {
44+
protected:
45+
void process(entity::EzyApp* app, entity::EzyObject* data) {
46+
logger::log("recived message: %s", data->getString("message").c_str());
47+
}
48+
};
49+
50+
int main(int argc, const char * argv[]) {
51+
auto config = config::EzyClientConfig::create();
52+
config->setClientName(ZONE_NAME);
53+
auto mSocketClient = EzyUTClient::create(config);
54+
EzyClients::getInstance()->addClient(mSocketClient);
55+
auto setup = mSocketClient->setup();
56+
setup->addDataHandler(constant::Handshake, new HandshakeHandler());
57+
setup->addDataHandler(constant::Login, new LoginSuccessHandler());
58+
setup->addDataHandler(constant::AppAccess, new AppAccessHandler());
59+
60+
auto appSetup = setup->setupApp("hello-world");
61+
appSetup->addDataHandler("greet", new GreetResponseHandler());
62+
63+
mSocketClient->connect("tvd12.com", 3005);
64+
65+
socket::EzyMainEventsLoop* eventsLoop = new socket::EzyMainEventsLoop();
66+
eventsLoop->start();
67+
EZY_SAFE_DELETE(eventsLoop);
68+
}
69+

src/EzyClient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ EzyClient* EzyClient::create(config::EzyClientConfig *config) {
3636
void EzyClient::init(config::EzyClientConfig *config) {
3737
mConfig = config;
3838
mName = config->getClientName();
39-
mPingManager = new manager::EzyPingManager();
39+
mPingManager = new manager::EzyPingManager(config->getPing());
4040
mPingSchedule = new socket::EzyPingSchedule(this);
4141
mHandlerManager = new manager::EzyHandlerManager(this);
4242
mRequestSerializer = new request::EzyRequestSerializer();

src/codec/EzyEncryption.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,22 @@ char* EzyRSA::decrypt(const char* message, int size, std::string privateKey, int
148148
}
149149

150150
std::string EzyRSA::decodePublicKey(const char *publicKey, int size) {
151-
std::string copy(publicKey);
152-
copy.erase(copy.begin(), copy.begin() + beginPublicKey.size());
153-
copy.erase(copy.end() - endPublicKey.size(), copy.end());
154-
copy.erase(std::remove(copy.begin(), copy.end(), '\n'), copy.end());
155-
return copy;
151+
int unnecessarySize = (int)beginPublicKey.size() + (int)endPublicKey.size();
152+
if(size < unnecessarySize) {
153+
return "";
154+
}
155+
char* copy = (char*)malloc(size - unnecessarySize);
156+
int start = (int)beginPublicKey.size();
157+
int end = size - (int)beginPublicKey.size();
158+
int actualSize = 0;
159+
for(int i = start ; i < end ; ++i) {
160+
if(publicKey[i] != '\n') {
161+
copy[actualSize ++] = publicKey[i];
162+
}
163+
}
164+
std::string answer = std::string(copy, actualSize);
165+
EZY_SAFE_FREE(copy);
166+
return answer;
156167
}
157168

158169
//====================================================

src/config/EzyClientConfig.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ EzyClientConfig* EzyClientConfig::create() {
99

1010
EzyClientConfig::EzyClientConfig() {
1111
this->mClientName = "";
12+
this->mPing = new EzyPingConfig();
1213
this->mSocket = new EzySocketConfig();
1314
this->mReconnect = new EzyReconnectConfig();
1415
}
1516

1617
EzyClientConfig::~EzyClientConfig() {
1718
EZY_SAFE_DELETE(mSocket);
19+
EZY_SAFE_DELETE(mPing);
1820
EZY_SAFE_DELETE(mReconnect);
1921
}
2022

@@ -29,6 +31,11 @@ void EzyClientConfig::setSocket(EzySocketConfig *socket) {
2931
this->mSocket = socket;
3032
}
3133

34+
void EzyClientConfig::setPing(EzyPingConfig* ping) {
35+
EZY_SAFE_DELETE(mPing);
36+
this->mPing = ping;
37+
}
38+
3239
void EzyClientConfig::setReconnect(EzyReconnectConfig* reconnect) {
3340
EZY_SAFE_DELETE(mReconnect);
3441
this->mReconnect = reconnect;
@@ -44,6 +51,12 @@ EzySocketConfig::EzySocketConfig() {
4451
this->mUdpDecodeReserveSize = 8 * 1024;
4552
}
4653

54+
//============================
55+
EzyPingConfig::EzyPingConfig() {
56+
this->mPingPeriod = 3000;
57+
this->mMaxLostPingCount = 5;
58+
}
59+
4760
//============================
4861

4962
EzyReconnectConfig::EzyReconnectConfig() {

src/config/EzyClientConfig.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,23 @@
77
EZY_NAMESPACE_START_WITH_ONLY(config)
88

99
class EzySocketConfig;
10+
class EzyPingConfig;
1011
class EzyReconnectConfig;
1112

1213
class EzyClientConfig {
1314
protected:
1415
EZY_SYNTHESIZE(std::string, ZoneName);
1516
EZY_SYNTHESIZE_WRITEONLY(std::string, ClientName);
1617
EZY_SYNTHESIZE_READONLY(EzySocketConfig*, Socket);
18+
EZY_SYNTHESIZE_READONLY(EzyPingConfig*, Ping);
1719
EZY_SYNTHESIZE_READONLY(EzyReconnectConfig*, Reconnect);
1820
public:
1921
static EzyClientConfig* create();
2022
EzyClientConfig();
2123
~EzyClientConfig();
2224
std::string getClientName();
2325
void setSocket(EzySocketConfig* socket);
26+
void setPing(EzyPingConfig* ping);
2427
void setReconnect(EzyReconnectConfig* reconnect);
2528
};
2629

@@ -37,6 +40,17 @@ class EzySocketConfig {
3740
EzySocketConfig();
3841
};
3942

43+
//============================
44+
45+
class EzyPingConfig {
46+
protected:
47+
EZY_SYNTHESIZE(int, PingPeriod);
48+
EZY_SYNTHESIZE(int, MaxLostPingCount);
49+
public:
50+
EzyPingConfig();
51+
};
52+
53+
4054
//============================
4155

4256
class EzyReconnectConfig {

src/logger/EzyLogger.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <algorithm>
33
#include <cstdarg>
44
#include <string>
5+
#include <cstring>
56

67
#if defined(_WIN32) || defined(WINRT)
78
#include "Windows.h"

src/manager/EzyAppManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ entity::EzyApp* EzyAppManager::removeApp(int appId) {
3434
if(app) {
3535
mAppByIds.erase(appId);
3636
mAppByNames.erase(app->getName());
37-
auto appIndex = std::find(mAppList.begin(), mAppList.end(), app);
37+
auto appIndex = stdex::find(mAppList.begin(), mAppList.end(), app);
3838
if(appIndex != mAppList.end()) {
3939
mAppList.erase(appIndex);
4040
}

0 commit comments

Comments
 (0)