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
32f68acc
Commit
32f68acc
authored
Feb 05, 2018
by
shangshang.dai
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add 1:新增Socket类
parent
bbce0280
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
246 additions
and
67 deletions
+246
-67
src/SocketModule.cpp
+0
-0
src/SocketModule.h
+70
-16
src/main.cpp
+176
-51
No files found.
src/SocketModule.cpp
View file @
32f68acc
This diff is collapsed.
Click to expand it.
src/SocketModule.h
View file @
32f68acc
#ifndef SOCKET_MODULE_H
#ifndef SOCKET_MODULE_H
#define SOCKET_MODULE_H
#define SOCKET_MODULE_H
typedef
struct
{
#include <string>
unsigned
int
flag
;
#include <string.h>
unsigned
int
ver
;
#include <errno.h>
unsigned
int
len
;
#include <sys/types.h>
}
FMSOCKHEADER
,
*
LPFMSOCKHEADER
;
#include <sys/socket.h>
#include <netinet/in.h>
//socket 数据接收
#include <arpa/inet.h>
//返回值 成功:接收长度
// 失败: -1
#include <unistd.h>
int
socketRecvData
(
char
*
recvBuf
,
int
bufSize
,
int
socket_fd
);
#include <fcntl.h>
//通过 sockClient 发送数据
#define BUF_SIZE 1024*2
int
socketSendData
(
const
char
*
sendBuf
,
int
socket_fd
);
class
TCPSocket
//socket server线程
{
void
*
FunSocketServer
(
void
*
lpParamter
);
protected
:
TCPSocket
();
virtual
~
TCPSocket
();
bool
create
();
bool
bind
(
unsigned
short
int
port
,
const
char
*
ip
=
NULL
)
const
;
bool
listen
(
int
backlog
=
1
)
const
;
bool
accept
(
TCPSocket
&
clientSocket
)
const
;
bool
connect
(
unsigned
short
int
port
,
const
char
*
ip
)
const
;
bool
reuseaddr
()
const
;
public
:
bool
close
();
int
getfd
()
const
{
return
m_sockfd
;
}
//flag: true=SetNonBlock, false=SetBlock
bool
setNonBlocking
(
bool
flag
)
const
;
protected
:
int
m_sockfd
;
};
/** TCP Client **/
class
TCPClient
:
public
TCPSocket
{
private
:
struct
Packet
{
unsigned
int
msgLen
;
//数据部分的长度(网络字节序)
char
text
[
BUF_SIZE
];
//报文的数据部分
};
public
:
TCPClient
()
:
m_bValid
(
false
){}
~
TCPClient
(){}
/* 该函数会强制删除现有socket重新创建连接 */
bool
doConnect
(
unsigned
short
port
,
const
char
*
ip
);
bool
read
(
void
*
buf
,
size_t
count
);
bool
write
(
const
char
*
msg
);
bool
receive
(
std
::
string
&
message
);
bool
send
(
const
std
::
string
&
message
);
bool
isValid
(){
return
m_bValid
;
}
private
:
bool
m_bValid
;
};
/** TCP Server **/
class
TCPServer
:
public
TCPSocket
{
public
:
TCPServer
(){}
~
TCPServer
(){}
/* 该函数会强制删除现有socket重新创建监听 */
bool
doListen
(
unsigned
short
int
port
,
const
char
*
ip
=
NULL
,
int
backlog
=
SOMAXCONN
);
bool
accept
(
TCPSocket
&
clientSocket
)
const
;
};
#endif
#endif
src/main.cpp
View file @
32f68acc
#include <iostream>
#include <iostream>
#include <fstream>
#include <fstream>
#include <pthread.h>
#include <pthread.h>
#include "JsonModule.h"
#include "JsonModule.h"
#include "SocketModule.h"
#include "SocketModule.h"
...
@@ -12,9 +12,20 @@
...
@@ -12,9 +12,20 @@
INITIALIZE_EASYLOGGINGPP
INITIALIZE_EASYLOGGINGPP
std
::
string
str_pos2ods_request
;
std
::
string
str_pos2ods_reply
;
std
::
string
str_ods2pos_push
;
std
::
string
str_ods2pos_reply
;
std
::
string
ods_ip
;
int
ods_listen_port
;
int
client_listen_port
;
int
pos_listen_port
=
8009
;
void
logRolloutHandler
(
const
char
*
filename
,
std
::
size_t
size
)
void
logRolloutHandler
(
const
char
*
filename
,
std
::
size_t
size
)
{
{
/// 备份日志
/// 备份日志
//std::string strLogPath = GetProcDir();
//std::string strLogPath = GetProcDir();
//strLogPath.append(filename);
//strLogPath.append(filename);
...
@@ -23,69 +34,183 @@ void logRolloutHandler(const char* filename, std::size_t size)
...
@@ -23,69 +34,183 @@ void logRolloutHandler(const char* filename, std::size_t size)
LOG
(
INFO
)
<<
"备份日志:"
<<
ss
.
str
().
c_str
();
LOG
(
INFO
)
<<
"备份日志:"
<<
ss
.
str
().
c_str
();
system
(
ss
.
str
().
c_str
());
system
(
ss
.
str
().
c_str
());
}
}
void
*
listen_ods_func
(
void
*
arg
)
{
TCPClient
*
client
=
(
TCPClient
*
)
arg
;
while
(
true
)
{
std
::
string
msg
;
// 判断是否成功连接ODS
if
(
!
client
->
isValid
()
)
{
if
(
client
->
doConnect
(
ods_listen_port
,
ods_ip
.
c_str
())
)
{
LOG
(
INFO
)
<<
"重连ODS成功"
;
}
else
{
continue
;
usleep
(
800
);
}
}
if
(
client
->
receive
(
msg
)
!=
-
1
)
{
// 判断是推送还是回复
std
::
string
mark
(
"push"
);
if
(
msg
.
compare
(
0
,
mark
.
size
(),
mark
)
==
0
)
{
// 为ODS的推送消息
TCPClient
tmp_client
;
if
(
tmp_client
.
doConnect
(
pos_listen_port
,
"127.0.0.1"
))
{
if
(
tmp_client
.
send
(
msg
))
{
std
::
string
tmp_msg
;
if
(
tmp_client
.
receive
(
tmp_msg
))
{
str_pos2ods_reply
=
tmp_msg
;
}
else
{
str_pos2ods_reply
=
"获取POS返回值失败"
;
}
}
else
{
LOG
(
INFO
)
<<
"转发失败: "
<<
msg
;
str_pos2ods_reply
=
"推送给POS失败"
;
}
tmp_client
.
close
();
}
else
{
str_pos2ods_reply
=
"连接POS失败"
;
}
}
else
{
// 为ODS的返回消息
str_ods2pos_reply
=
msg
;
}
}
else
{
LOG
(
INFO
)
<<
"ods连接失效,尝试重连。。。。。。"
;
}
}
}
void
*
listen_pos_func
(
void
*
arg
)
{
TCPServer
server
;
if
(
server
.
doListen
(
client_listen_port
)
)
{
LOG
(
INFO
)
<<
"监听端口成功"
;
}
while
(
true
)
{
std
::
string
msg
;
TCPClient
connect
;
if
(
server
.
accept
(
connect
)
)
{
if
(
connect
.
receive
(
msg
)
)
{
// POS请求数据
// 赋值给中间变量,等待其他线程发送给ODS
str_pos2ods_request
=
msg
;
// 阻塞等待ODS返回
while
(
str_ods2pos_reply
.
empty
())
{
usleep
(
200
);
continue
;
}
// 返回给POS
connect
.
send
(
str_ods2pos_reply
);
str_ods2pos_reply
.
clear
();
connect
.
close
();
}
else
{
LOG
(
INFO
)
<<
"接收POS推送消息失败"
;
}
}
else
{
LOG
(
INFO
)
<<
"接受POS连接失败"
;
}
}
}
int
main
()
int
main
()
{
{
signal
(
SIGPIPE
,
SIG_IGN
);
// 初始化日志
el
::
Loggers
::
addFlag
(
el
::
LoggingFlag
::
StrictLogFileSizeCheck
);
el
::
Loggers
::
addFlag
(
el
::
LoggingFlag
::
StrictLogFileSizeCheck
);
std
::
string
strBinPath
=
GetProcDir
();
std
::
string
strBinPath
=
GetProcDir
();
std
::
string
strLogPath
(
strBinPath
.
data
());
std
::
string
strLogPath
(
strBinPath
.
data
());
strLogPath
.
append
(
"log.conf"
);
strLogPath
.
append
(
"log.conf"
);
el
::
Configurations
conf
(
strLogPath
.
data
());
el
::
Configurations
conf
(
strLogPath
.
data
());
/// 设置全部logger的配置
/// 设置全部logger的配置
el
::
Loggers
::
reconfigureAllLoggers
(
conf
);
el
::
Loggers
::
reconfigureAllLoggers
(
conf
);
/// 注册回调函数
/// 注册回调函数
el
::
Helpers
::
installPreRollOutCallback
(
logRolloutHandler
);
el
::
Helpers
::
installPreRollOutCallback
(
logRolloutHandler
);
LOG
(
INFO
)
<<
"
日志测试
"
;
LOG
(
INFO
)
<<
"
--------------程序启动--------------
"
;
//
---------ini test-------------
//
读取配置文件信息
std
::
string
strIniPath
(
strBinPath
.
data
());
std
::
string
strIniPath
(
strBinPath
.
data
());
strIniPath
.
append
(
"config.ini"
);
strIniPath
.
append
(
"config.ini"
);
std
::
string
ip
=
ZIni
::
readString
(
"SYS"
,
"ip"
,
""
,
strIniPath
.
c_str
());
ods_ip
=
ZIni
::
readString
(
"ODS"
,
"ip"
,
""
,
strIniPath
.
c_str
());
LOG
(
INFO
)
<<
"读取配置文件ip:"
<<
ip
.
data
();
ods_listen_port
=
ZIni
::
readInt
(
"ODS"
,
"listenPort"
,
-
1
,
strIniPath
.
c_str
());
//------------end---------------
client_listen_port
=
ZIni
::
readInt
(
"CLIENT"
,
"listenPort"
,
-
1
,
strIniPath
.
c_str
());
LOG
(
INFO
)
<<
"[ODS]服务器ip地址: "
<<
ods_ip
.
data
()
<<
"-监听端口: "
<<
ods_listen_port
;
//---------- json test----------
LOG
(
INFO
)
<<
"本地监听端口: "
<<
client_listen_port
;
// std::string json = GetTestJson(100,"test data","123456789");
// LOG(INFO)<<"生成JSON" <<json.data();
// LOG(INFO)<<"JSON解析";
// 监听POS请求的线程
// parseJson(json.data());
pthread_t
listen_pos_id
,
listen_ods_id
;
JsonModule
jsonMod
;
orderObj
obj
;
// 和ODS长连接通信
productAttr
product
;
TCPClient
ods_client
;
product
.
pro
.
source
=
"123"
;
productSpec
spec
;
if
(
ods_client
.
doConnect
(
ods_listen_port
,
ods_ip
.
c_str
())
)
spec
.
name
=
"只"
;
LOG
(
INFO
)
<<
"连接ODS成功"
;
product
.
vecSpec
.
push_back
(
spec
);
obj
.
vecProducts
.
push_back
(
product
);
/*创建 listen_pos 线程*/
if
(
pthread_create
(
&
listen_pos_id
,
NULL
,
listen_pos_func
,
NULL
))
std
::
string
orderInfo
=
jsonMod
.
convertToNewOrderJson
(
obj
);
LOG
(
INFO
)
<<
"创建listen_pos线程失败"
;
LOG
(
INFO
)
<<
"订单信息转换成JSON:"
<<
orderInfo
.
data
();
/*创建 listen_ods 线程*/
//------------end---------------
if
(
pthread_create
(
&
listen_ods_id
,
NULL
,
listen_ods_func
,
&
ods_client
))
LOG
(
INFO
)
<<
"创建listen_pos线程失败"
;
//---------- pthread test ---------
LOG
(
INFO
)
<<
"启动SOCKET线程"
;
while
(
true
)
pthread_t
printId
;
{
int
ret
=
pthread_create
(
&
printId
,
NULL
,
FunSocketServer
,
NULL
);
// 专门负责pos到ods的数据转发
//---------- end ------------------
if
(
!
str_pos2ods_reply
.
empty
())
{
//---------- sqlite test-----------
// 将POS返回给ODS
SQLite
sqlite
;
if
(
ods_client
.
send
(
str_pos2ods_reply
)
)
sqlite
.
initSQLite
();
{
sqlite
.
insert
(
"insert into fmTest(fm_id, statusCode,msg,prompt,fm_open_id,total_amount,paid_total_amount,invoice_amount,incentives_amount)\
str_pos2ods_reply
.
clear
();
values('aabbcc',111,'abc',123,'cba',1,1,1,1)"
);
}
else
sqlite
.
query
(
"select * from fmTest"
);
{
sqlite
.
update
(
"update fmTest set statusCode=200 where fm_id='aabbcc'"
);
}
sqlite
.
query
(
"select * from fmTest"
);
}
sqlite
.
remove
(
"delete from fmTest where fm_id='aabbcc'"
);
sqlite
.
query
(
"select * from fmTest"
);
if
(
!
str_pos2ods_request
.
empty
())
sqlite
.
closeSQLite
();
{
// 将POS的请求转发给ODS
//-----------end------------------
if
(
ods_client
.
send
(
str_pos2ods_request
))
char
pStr
[
20
];
{
std
::
cin
>>
pStr
;
str_pos2ods_request
.
clear
();
}
else
{
str_ods2pos_reply
=
"请求转发给ODS失败"
;
}
}
usleep
(
100
);
}
return
0
;
return
0
;
}
}
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