Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
familyMart_takeaway
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
guanghui.cui
familyMart_takeaway
Commits
e0484b4f
Commit
e0484b4f
authored
Mar 05, 2018
by
guanghui.cui
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
长连接添加心跳
parent
e9687bbc
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
73 additions
and
33 deletions
+73
-33
bin/config.ini
+7
-7
src/JsonModule.cpp
+16
-0
src/JsonModule.h
+7
-0
src/SocketModule.cpp
+8
-0
src/SocketModule.h
+1
-0
src/main.cpp
+32
-25
utility/zini.cpp
+2
-1
No files found.
bin/config.ini
View file @
e0484b4f
[SYS]
[SYS]
port
=
24446
port
=
24446
[ODS]
[ODS]
ip
=
"115.234.23.176"
ip
=
115.234.23.176
pushPort
=
8996
pushPort
=
8996
recvPort
=
8997
recvPort
=
8997
socketTimeout
=
60
[POS]
[POS]
ip
=
"127.0.0.1"
ip
=
127.0.0.1
port
=
24445
port
=
24445
src/JsonModule.cpp
View file @
e0484b4f
...
@@ -476,6 +476,22 @@ bool JsonModule::isInitData(const std::string &data)
...
@@ -476,6 +476,22 @@ bool JsonModule::isInitData(const std::string &data)
return
document
[
"fm_cmd"
].
GetInt
()
==
1000
;
return
document
[
"fm_cmd"
].
GetInt
()
==
1000
;
}
}
bool
JsonModule
::
isHeartbeatData
(
IN
const
char
*
data
)
{
rapidjson
::
Document
document
;
// 定义一个Document对象
document
.
Parse
(
data
);
// 解析,Parse()无返回值,也不会抛异常
if
(
document
.
HasParseError
())
// 通过HasParseError()来判断解析是否成功
{
LOG
(
ERROR
)
<<
"JSON parse error:"
<<
document
.
GetParseError
()
<<
":"
<<
document
.
GetErrorOffset
();
return
false
;
}
if
(
document
.
HasMember
(
"fm_cmd"
)){
if
(
document
[
"fm_cmd"
].
GetInt
()
==
3
)
return
true
;
}
return
false
;
}
bool
JsonModule
::
checkInitData
(
const
std
::
string
&
data
,
int
&
posListenPort
)
bool
JsonModule
::
checkInitData
(
const
std
::
string
&
data
,
int
&
posListenPort
)
{
{
rapidjson
::
Document
document
;
// 定义一个Document对象
rapidjson
::
Document
document
;
// 定义一个Document对象
...
...
src/JsonModule.h
View file @
e0484b4f
...
@@ -21,6 +21,13 @@ public:
...
@@ -21,6 +21,13 @@ public:
* 返回:...
* 返回:...
* */
* */
bool
isInitData
(
IN
const
std
::
string
&
data
);
bool
isInitData
(
IN
const
std
::
string
&
data
);
/* 功能:判断是否是心跳包
* 参数:[1]待判断数据
* 返回:...
* */
bool
isHeartbeatData
(
IN
const
char
*
data
);
/* 功能:检查初始化数据是否可用
/* 功能:检查初始化数据是否可用
* 参数:[1]待检查数据[2]POS监听端口
* 参数:[1]待检查数据[2]POS监听端口
* 返回:是否正确格式
* 返回:是否正确格式
...
...
src/SocketModule.cpp
View file @
e0484b4f
...
@@ -138,6 +138,14 @@ bool TCPClient::doConnect(unsigned short port, const char *ip)
...
@@ -138,6 +138,14 @@ bool TCPClient::doConnect(unsigned short port, const char *ip)
return
false
;
return
false
;
}
}
void
TCPClient
::
setSocketTimeout
(
int
timeout
)
{
struct
timeval
vtime
;
vtime
.
tv_sec
=
timeout
;
vtime
.
tv_usec
=
0
;
setsockopt
(
m_sockfd
,
SOL_SOCKET
,
SO_RCVTIMEO
,
&
vtime
,
sizeof
(
struct
timeval
));
}
//send
//send
bool
TCPClient
::
send
(
const
std
::
string
&
message
)
bool
TCPClient
::
send
(
const
std
::
string
&
message
)
{
{
...
...
src/SocketModule.h
View file @
e0484b4f
...
@@ -54,6 +54,7 @@ public:
...
@@ -54,6 +54,7 @@ public:
bool
write
(
const
char
*
msg
);
bool
write
(
const
char
*
msg
);
bool
receive
(
std
::
string
&
message
);
bool
receive
(
std
::
string
&
message
);
bool
send
(
const
std
::
string
&
message
);
bool
send
(
const
std
::
string
&
message
);
void
setSocketTimeout
(
int
timeout
);
bool
isValid
(){
return
m_bValid
;
}
bool
isValid
(){
return
m_bValid
;
}
private
:
private
:
...
...
src/main.cpp
View file @
e0484b4f
...
@@ -148,9 +148,11 @@ int main()
...
@@ -148,9 +148,11 @@ int main()
std
::
string
strIniPath
(
strBinPath
.
data
());
std
::
string
strIniPath
(
strBinPath
.
data
());
strIniPath
.
append
(
"config.ini"
);
strIniPath
.
append
(
"config.ini"
);
LOG
(
INFO
)
<<
"strIniPath: "
<<
strIniPath
.
data
();
ods_ip
=
ZIni
::
readString
(
"ODS"
,
"ip"
,
""
,
strIniPath
.
c_str
());
ods_ip
=
ZIni
::
readString
(
"ODS"
,
"ip"
,
""
,
strIniPath
.
c_str
());
ods_push_port
=
ZIni
::
readInt
(
"ODS"
,
"pushPort"
,
0
,
strIniPath
.
c_str
());
ods_push_port
=
ZIni
::
readInt
(
"ODS"
,
"pushPort"
,
0
,
strIniPath
.
c_str
());
ods_recv_port
=
ZIni
::
readInt
(
"ODS"
,
"recvPort"
,
0
,
strIniPath
.
c_str
());
ods_recv_port
=
ZIni
::
readInt
(
"ODS"
,
"recvPort"
,
0
,
strIniPath
.
c_str
());
int
ods_socket_timeout
=
ZIni
::
readInt
(
"ODS"
,
"socketTimeout"
,
0
,
strIniPath
.
c_str
());
pos_ip
=
ZIni
::
readString
(
"POS"
,
"ip"
,
""
,
strIniPath
.
c_str
());
pos_ip
=
ZIni
::
readString
(
"POS"
,
"ip"
,
""
,
strIniPath
.
c_str
());
pos_listen_port
=
ZIni
::
readInt
(
"POS"
,
"port"
,
0
,
strIniPath
.
c_str
());
pos_listen_port
=
ZIni
::
readInt
(
"POS"
,
"port"
,
0
,
strIniPath
.
c_str
());
...
@@ -159,7 +161,8 @@ int main()
...
@@ -159,7 +161,8 @@ int main()
LOG
(
INFO
)
<<
"[ODS]ip: "
<<
ods_ip
.
data
()
LOG
(
INFO
)
<<
"[ODS]ip: "
<<
ods_ip
.
data
()
<<
"-push port: "
<<
ods_push_port
<<
"-push port: "
<<
ods_push_port
<<
"-recv port: "
<<
ods_recv_port
;
<<
"-recv port: "
<<
ods_recv_port
<<
"-socket timeout: "
<<
ods_socket_timeout
;
LOG
(
INFO
)
<<
"[POS]ip: "
<<
pos_ip
LOG
(
INFO
)
<<
"[POS]ip: "
<<
pos_ip
<<
"-listen port: "
<<
pos_listen_port
;
<<
"-listen port: "
<<
pos_listen_port
;
LOG
(
INFO
)
<<
"[Local]listen port: "
<<
client_listen_port
;
LOG
(
INFO
)
<<
"[Local]listen port: "
<<
client_listen_port
;
...
@@ -187,11 +190,11 @@ int main()
...
@@ -187,11 +190,11 @@ int main()
if
(
ods
.
doConnect
(
ods_push_port
,
ods_ip
.
c_str
())
)
if
(
ods
.
doConnect
(
ods_push_port
,
ods_ip
.
c_str
())
)
{
{
LOG
(
INFO
)
<<
"connect ODS successful"
;
LOG
(
INFO
)
<<
"connect ODS successful"
;
ods
.
setSocketTimeout
(
ods_socket_timeout
);
//设置超时时间10s
// 注册socket信息
// 注册socket信息
while
(
true
)
while
(
true
)
{
{
//
g_init_data = "{\"fm_cmd\": 1000,\"store_id\": \"208888\",\"pos_id\": \"0001\",\"operator_id\": \"00001\",\"business_date\": \"20171225\",\"is_master\": true,\"listen_port\": 3289}";
g_init_data
=
"{
\"
fm_cmd
\"
: 1000,
\"
store_id
\"
:
\"
208888
\"
,
\"
pos_id
\"
:
\"
0001
\"
,
\"
operator_id
\"
:
\"
00001
\"
,
\"
business_date
\"
:
\"
20171225
\"
,
\"
is_master
\"
: true,
\"
listen_port
\"
: 3289}"
;
if
(
!
g_init_data
.
empty
())
if
(
!
g_init_data
.
empty
())
{
{
...
@@ -219,44 +222,48 @@ int main()
...
@@ -219,44 +222,48 @@ int main()
{
{
LOG
(
INFO
)
<<
"----------------recved ODS data------------------"
;
LOG
(
INFO
)
<<
"----------------recved ODS data------------------"
;
LOG
(
INFO
)
<<
odsPushData
;
LOG
(
INFO
)
<<
odsPushData
;
if
(
jsonTool
.
convertDataOds2Pos
(
odsPushData
,
pushPosData
)
)
if
(
!
jsonTool
.
isHeartbeatData
(
odsPushData
.
data
())){
{
if
(
jsonTool
.
convertDataOds2Pos
(
odsPushData
,
pushPosData
)
)
LOG
(
INFO
)
<<
"********convert data to pos************"
;
LOG
(
INFO
)
<<
pushPosData
;
TCPClient
pos
;
if
(
pos
.
doConnect
(
pos_listen_port
,
pos_ip
.
c_str
())
)
{
{
if
(
pos
.
write
(
pushPosData
.
c_str
())
)
LOG
(
INFO
)
<<
"********convert data to pos************"
;
LOG
(
INFO
)
<<
pushPosData
;
TCPClient
pos
;
if
(
pos
.
doConnect
(
pos_listen_port
,
pos_ip
.
c_str
())
)
{
{
char
tmpBuf
[
BUF_SIZE
]
=
{
0
};
if
(
pos
.
write
(
pushPosData
.
c_str
())
)
if
(
pos
.
read
(
tmpBuf
,
sizeof
(
tmpBuf
))
)
{
{
std
::
string
tmp
(
tmpBuf
);
char
tmpBuf
[
BUF_SIZE
]
=
{
0
};
jsonTool
.
getOdsResponseData
(
tmp
,
odsPushData
,
responseData
);
if
(
pos
.
read
(
tmpBuf
,
sizeof
(
tmpBuf
))
)
{
std
::
string
tmp
(
tmpBuf
);
jsonTool
.
getOdsResponseData
(
tmp
,
odsPushData
,
responseData
);
}
else
{
jsonTool
.
getOdsResponseData
(
101
,
"receive data from [POS] failed!"
,
responseData
);
}
}
else
}
else
{
{
jsonTool
.
getOdsResponseData
(
101
,
"
receive data from
[POS] failed!"
,
responseData
);
jsonTool
.
getOdsResponseData
(
101
,
"
send data to
[POS] failed!"
,
responseData
);
}
}
pos
.
close
();
}
else
}
else
{
{
jsonTool
.
getOdsResponseData
(
101
,
"
send data to
[POS] failed!"
,
responseData
);
jsonTool
.
getOdsResponseData
(
101
,
"
connect
[POS] failed!"
,
responseData
);
}
}
pos
.
close
();
}
else
}
else
{
{
jsonTool
.
getOdsResponseData
(
101
,
"con
nect [POS]
failed!"
,
responseData
);
jsonTool
.
getOdsResponseData
(
101
,
"con
vert data to [POS] format
failed!"
,
responseData
);
}
}
}
else
{
// TODO待加入重试机制
jsonTool
.
getOdsResponseData
(
101
,
"convert data to [POS] format failed!"
,
responseData
);
ods
.
send
(
responseData
);
}
}
// TODO待加入重试机制
ods
.
send
(
responseData
);
}
else
}
else
{
{
LOG
(
INFO
)
<<
"recv ODS pushDate failed"
;
ods
.
close
();
LOG
(
INFO
)
<<
"recv ODS pushDate failed or timeout"
;
}
}
}
}
...
...
utility/zini.cpp
View file @
e0484b4f
...
@@ -15,6 +15,7 @@
...
@@ -15,6 +15,7 @@
#include <sstream>
#include <sstream>
#include <cctype>
#include <cctype>
#include <cstdlib>
#include <cstdlib>
#include <iostream>
bool
ZIni
::
writeString
(
std
::
string
strSectName
,
bool
ZIni
::
writeString
(
std
::
string
strSectName
,
std
::
string
strKeyName
,
std
::
string
strKeyName
,
...
@@ -217,7 +218,7 @@ double ZIni::readDouble(std::string strSectName,
...
@@ -217,7 +218,7 @@ double ZIni::readDouble(std::string strSectName,
ssDefault
.
str
(),
strFileName
).
c_str
());
ssDefault
.
str
(),
strFileName
).
c_str
());
}
}
//
消除字串两段的空格,字串中间的保留
//
消除字串两段的空格,字串中间的保留
std
::
string
ZIni
::
trim
(
std
::
string
&
strInput
)
std
::
string
ZIni
::
trim
(
std
::
string
&
strInput
)
{
{
std
::
string
str
(
strInput
);
std
::
string
str
(
strInput
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment