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
69839807
Commit
69839807
authored
Apr 28, 2018
by
guanghui.cui
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
程序单进程运行
parent
068e2172
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
177 additions
and
22 deletions
+177
-22
.gitignore
+1
-0
bin/config.ini
+1
-1
familyMart_takeaway/familyMart_takeaway.vcxproj
+2
-2
src/main.cpp
+69
-19
utility/utility.cpp
+101
-0
utility/utility.h
+3
-0
No files found.
.gitignore
View file @
69839807
...
...
@@ -10,3 +10,4 @@
*.idb
*.suo
*.opendb
*.user
bin/config.ini
View file @
69839807
...
...
@@ -2,7 +2,7 @@
port
=
24446
[ODS]
ip
=
1
72.16.13.7
1
ip
=
1
27.0.0.
1
pushPort
=
30001
recvPort
=
30002
socketTimeout
=
60
...
...
familyMart_takeaway/familyMart_takeaway.vcxproj
View file @
69839807
...
...
@@ -91,7 +91,7 @@
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;_DEBUG;_CONSOLE
;%(PreprocessorDefinitions)
</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN32;_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;_DEBUG;_CONSOLE</PreprocessorDefinitions>
<SDLCheck>false</SDLCheck>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
</ClCompile>
...
...
@@ -121,7 +121,7 @@
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;NDEBUG;_CONSOLE
;%(PreprocessorDefinitions)
</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN32;_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;NDEBUG;_CONSOLE</PreprocessorDefinitions>
<SDLCheck>false</SDLCheck>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
</ClCompile>
...
...
src/main.cpp
View file @
69839807
...
...
@@ -18,7 +18,8 @@
INITIALIZE_EASYLOGGINGPP
#define VERSION "1.0.3" //版本号
#define VERSION "1.0.3 Beta 2" //版本号
std
::
string
g_init_data
;
std
::
string
g_init_data_ods_back
;
...
...
@@ -31,16 +32,15 @@ int ods_push_port;
int
ods_recv_port
;
int
client_listen_port
;
int
pos_listen_port
;
//std::vector<orderSendFailedObj> vecFailedOrders;
//bool bRetryThreadRunning=false; //retry线程是否正在运行
bool
bInitDone
=
false
;
//初始化完成
bool
bPriorityDone
=
true
;
//设置POS优先级
//SQLite sqlite;
//函数,订单信息发送给pos
bool
order_send_to_pos
(
IN
std
::
string
&
order_json
,
IN
std
::
string
&
ods_json
,
OUT
std
::
string
&
back_json
);
//失败订单写入vector及sqlite
//void write_failed_order(IN std::string &order_json);
//但进程运行,杀掉已有进程
void
kill_origin_process
();
void
logRolloutHandler
(
const
char
*
filename
,
std
::
size_t
size
)
{
...
...
@@ -67,7 +67,7 @@ void* listen_pos_func(void* arg)
}
else
{
LOG
(
INFO
)
<<
"local listen port: ["
<<
client_listen_port
<<
"] failed, maybe instance is exist,exit!!!"
;
exit
(
1
);
exit
(
0
);
}
while
(
true
)
...
...
@@ -143,7 +143,7 @@ void* listen_pos_func(void* arg)
LOG
(
INFO
)
<<
"POS send goods change data:"
<<
posReq
.
data
();
jsonTool
.
getPosResponseData
(
100
,
"success"
,
responseData
);
}
else
else
if
(
OPERATION_POS_CONFIRM
==
reqType
||
OPERATION_POS_CANCEL
==
reqType
||
OPERATION_POS_REFUND_AGREE
==
reqType
||
OPERATION_POS_REFUND_DISAGREE
==
reqType
)
{
LOG
(
INFO
)
<<
"REQ POS ===>> PLUGIN:"
<<
posRequestData
.
data
();
// 将POS请求数据转换为中台可接受数据格式
...
...
@@ -180,6 +180,16 @@ void* listen_pos_func(void* arg)
jsonTool
.
getPosResponseData
(
101
,
"convert data to [ODS] format failed!"
,
responseData
);
}
}
else
if
(
-
1
==
reqType
)
{
//接收到-1,程序需要重启,关闭本进程
LOG
(
INFO
)
<<
"recive exit msessage,exit!!!"
;
pos
.
write
(
"100"
);
pos
.
close
();
server
.
close
();
exit
(
0
);
}
// TODO待加入重试机制
LOG
(
INFO
)
<<
"PLUGIN ===>>POS:"
<<
responseData
.
data
();
...
...
@@ -245,10 +255,11 @@ int main(int argc,char *argv[])
printf
(
"version:%s
\n
"
,
VERSION
);
return
0
;
}
/* Mark
* 与ODS通信全部加数据头(长度)
* 与POS通信全部不加数据头
* end*/
* 与ODS通信全部加数据头(长度)
* 与POS通信全部不加数据头
* end*/
#ifdef WIN32
WSADATA
transData
;
WSAStartup
(
MAKEWORD
(
2
,
2
),
&
transData
);
...
...
@@ -256,7 +267,7 @@ int main(int argc,char *argv[])
#else
signal
(
SIGPIPE
,
SIG_IGN
);
#endif
// 初始化日志
el
::
Loggers
::
addFlag
(
el
::
LoggingFlag
::
StrictLogFileSizeCheck
);
std
::
string
strBinPath
=
GetProcDir
();
...
...
@@ -268,14 +279,8 @@ int main(int argc,char *argv[])
/// 注册回调函数
el
::
Helpers
::
installPreRollOutCallback
(
logRolloutHandler
);
LOG
(
INFO
)
<<
"---------software start---------"
;
//sqlite初始化
LOG
(
INFO
)
<<
"---------software start---------"
;
// if(!sqlite.initSQLite()){
// LOG(INFO)<<"initSQLite failed";
// return 0;
// }
// 读取配置文件信息
std
::
string
strIniPath
(
strBinPath
.
data
());
strIniPath
.
append
(
"config.ini"
);
...
...
@@ -298,6 +303,8 @@ int main(int argc,char *argv[])
LOG
(
INFO
)
<<
"[POS]ip: "
<<
pos_ip
<<
"-listen port: "
<<
pos_listen_port
;
LOG
(
INFO
)
<<
"[Local]listen port: "
<<
client_listen_port
;
kill_origin_process
();
//如果已有进程,杀掉
//end
#ifdef WIN32
...
...
@@ -508,3 +515,45 @@ bool order_send_to_pos(IN std::string &order_json,IN std::string &ods_json,OUT s
// delete[] lpSql;
// }
//实现逻辑:向本地socket发送一个命令,程序接到命令后自杀,如果没有自杀成功,根据名称杀掉
void
kill_origin_process
()
{
std
::
string
execName
;
//可执行文件名
#ifdef WIN32
#ifdef _DEBUG
execName
=
"takeaway_d.exe"
;
#else
execName
=
"takeaway.exe"
;
#endif // _DEBUG
#else
execName
=
"takeaway"
;
#endif
bool
rlt
=
true
;
TCPClient
pos
;
JsonModule
jsonTool
;
std
::
string
tmp
=
"{
\"
fm_cmd
\"
: -1}"
;
if
(
pos
.
doConnect
(
client_listen_port
,
pos_ip
.
c_str
())){
if
(
pos
.
write
(
tmp
.
c_str
())){
char
tmpBuf
[
BUF_SIZE
]
=
{
0
};
if
(
pos
.
read
(
tmpBuf
,
sizeof
(
tmpBuf
))){
LOG
(
INFO
)
<<
"kill back:"
<<
tmpBuf
;
if
(
strcmp
(
tmpBuf
,
"100"
)
==
0
)
{
os_sleep
(
2
);
//成功杀掉进程,延时2s等待释放端口资源
}
else
rlt
=
false
;
}
else
rlt
=
false
;
}
else
rlt
=
false
;
pos
.
close
();
}
if
(
!
rlt
)
{
//如果关闭失败,根据进程名称杀掉进程
kill_process_by_name
(
execName
.
c_str
());
}
}
\ No newline at end of file
utility/utility.cpp
View file @
69839807
...
...
@@ -5,14 +5,18 @@
#include <iostream>
#include <string.h>
#include <chrono>
#include "../3rdParty/easylogging/easylogging++.h"
#ifdef WIN32
#include <Windows.h>
#include <ctime>
#include <tlhelp32.h>
#else
#include <iconv.h>
#include <sys/time.h>
#include <unistd.h>
#include <dirent.h>
#include <signal.h>
#endif
//函数名: GetProcDir()
...
...
@@ -202,3 +206,99 @@ void os_sleep(int seconds)
sleep
(
seconds
);
#endif
}
#ifdef WIN32
DWORD
getPidByName
(
const
char
*
name
)
{
HANDLE
hSnapshot
=
CreateToolhelp32Snapshot
(
TH32CS_SNAPPROCESS
,
0
);
if
(
INVALID_HANDLE_VALUE
==
hSnapshot
)
{
return
0
;
}
HANDLE
currentProcess
=
GetCurrentProcess
();
DWORD
currentPid
=
GetProcessId
(
currentProcess
);
LOG
(
INFO
)
<<
"this process pid is:"
<<
currentPid
;
PROCESSENTRY32
pe
=
{
sizeof
(
pe
)
};
int
i
=
0
;
for
(
BOOL
ret
=
Process32First
(
hSnapshot
,
&
pe
);
ret
;
ret
=
Process32Next
(
hSnapshot
,
&
pe
))
{
i
++
;
if
(
strcmp
(
pe
.
szExeFile
,
name
)
==
0
&&
currentPid
!=
pe
.
th32ProcessID
)
{
CloseHandle
(
hSnapshot
);
return
pe
.
th32ProcessID
;
}
}
CloseHandle
(
hSnapshot
);
return
0
;
}
#else
pid_t
getPidByName
(
const
char
*
name
)
{
DIR
*
pdir
=
NULL
;
struct
dirent
*
pde
=
NULL
;
FILE
*
pf
=
NULL
;
char
buff
[
128
];
pid_t
pid
;
char
szName
[
128
];
pid_t
nowPid
=
getpid
();
LOG
(
INFO
)
<<
"this process pid is:"
<<
nowPid
;
// 遍历/proc目录下所有pid目录
pdir
=
opendir
(
"/proc"
);
if
(
!
pdir
)
{
perror
(
"open /proc fail.
\n
"
);
return
-
1
;
}
while
((
pde
=
readdir
(
pdir
)))
{
if
((
pde
->
d_name
[
0
]
<
'0'
)
||
(
pde
->
d_name
[
0
]
>
'9'
))
{
continue
;
}
sprintf
(
buff
,
"/proc/%s/status"
,
pde
->
d_name
);
pf
=
fopen
(
buff
,
"r"
);
if
(
pf
)
{
fgets
(
buff
,
sizeof
(
buff
),
pf
);
fclose
(
pf
);
sscanf
(
buff
,
"%*s %s"
,
szName
);
pid
=
atoi
(
pde
->
d_name
);
if
(
strcmp
(
szName
,
name
)
==
0
&&
pid
!=
nowPid
)
{
closedir
(
pdir
);
return
pid
;
}
}
}
closedir
(
pdir
);
return
0
;
}
#endif
void
kill_process_by_name
(
const
char
*
name
)
{
#ifdef WIN32
DWORD
pid
=
getPidByName
(
name
);
LOG
(
INFO
)
<<
"origin pid is:"
<<
pid
;
if
(
pid
>
0
)
{
HANDLE
hProcess
=
OpenProcess
(
PROCESS_TERMINATE
,
FALSE
,
pid
);
BOOL
bOkey
=
TerminateProcess
(
hProcess
,
0
);
if
(
bOkey
)
{
WaitForSingleObject
(
hProcess
,
3000
);
CloseHandle
(
hProcess
);
hProcess
=
NULL
;
LOG
(
INFO
)
<<
pid
<<
" is killed"
;
}
else
{
LOG
(
INFO
)
<<
"kill failed"
;
}
os_sleep
(
3
);
//延时,防止杀掉进程后,端口还没有释放
}
#else
int
pid
=
getPidByName
(
name
);
LOG
(
INFO
)
<<
"origin pid is:"
<<
pid
;
if
(
pid
>
0
)
{
int
rlt
=
kill
(
pid
,
SIGKILL
);
if
(
0
==
rlt
)
{
LOG
(
INFO
)
<<
pid
<<
" is killed"
;
}
else
{
LOG
(
INFO
)
<<
"kill failed"
;
}
os_sleep
(
3
);
//延时,防止杀掉进程后,端口还没有释放
}
#endif
}
\ No newline at end of file
utility/utility.h
View file @
69839807
...
...
@@ -25,4 +25,7 @@ std::string getdatetime(int64_t timestamp);
//延时秒数
void
os_sleep
(
int
seconds
);
//根据进程名称杀死进程
void
kill_process_by_name
(
const
char
*
name
);
#endif
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