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
296c12ce
Commit
296c12ce
authored
Jul 12, 2018
by
guanghui.cui
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
socket发送接收 空间动态分配
parent
e160fdd7
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
69 additions
and
47 deletions
+69
-47
src/SocketModule.cpp
+62
-40
src/SocketModule.h
+3
-3
src/main.cpp
+4
-4
No files found.
src/SocketModule.cpp
View file @
296c12ce
#include "SocketModule.h"
#
include
"SocketModule.h"
#include <stdio.h>
#include "../3rdParty/easylogging/easylogging++.h"
...
...
@@ -22,27 +22,38 @@ bool TCPSocket::create()
return
false
;
//查看默认 发送/接收 缓存区大小
// int rcvbuf_len;
//#ifdef WIN32
// int len;
//#else
// socklen_t len;
//#endif // WIN32
//
// len = sizeof(rcvbuf_len);
// if (getsockopt(m_sockfd, SOL_SOCKET, SO_RCVBUF, (char *)&rcvbuf_len, &len) < 0) {
// perror("getsockopt: ");
// return -1;
// }
// LOG(INFO) << "the recevice buf len:" << rcvbuf_len;
//
// int sendbuf_len;
// len = sizeof(sendbuf_len);
// if (getsockopt(m_sockfd, SOL_SOCKET, SO_SNDBUF, (char *)&sendbuf_len, &len) < 0) {
// perror("getsockopt: ");
// return -1;
// }
// LOG(INFO) << "the send buf len:" << sendbuf_len;
int
rcvbuf_len
;
#ifdef WIN32
int
len
;
#else
socklen_t
len
;
#endif // WIN32
int
buf_len_set
=
128
*
1024
;
//设置 发送/接收 缓存区为128k
len
=
sizeof
(
buf_len_set
);
if
(
setsockopt
(
m_sockfd
,
SOL_SOCKET
,
SO_SNDBUF
,
(
char
*
)
&
buf_len_set
,
len
)
<
0
)
{
LOG
(
INFO
)
<<
"setsockopt SO_SNDBUF error"
;
}
if
(
setsockopt
(
m_sockfd
,
SOL_SOCKET
,
SO_RCVBUF
,
(
char
*
)
&
buf_len_set
,
len
)
<
0
)
{
LOG
(
INFO
)
<<
"setsockopt SO_RCVBUF error:"
;
}
//len = sizeof(rcvbuf_len);
//if (getsockopt(m_sockfd, SOL_SOCKET, SO_RCVBUF, (char *)&rcvbuf_len, &len) < 0) {
// perror("getsockopt: ");
// return -1;
//}
//LOG(INFO) << "the recevice buf len:" << rcvbuf_len;
//int sendbuf_len;
//len = sizeof(sendbuf_len);
//if (getsockopt(m_sockfd, SOL_SOCKET, SO_SNDBUF, (char *)&sendbuf_len, &len) < 0) {
// perror("getsockopt: ");
// return -1;
//}
//LOG(INFO) << "the send buf len:" << sendbuf_len;
return
true
;
}
...
...
@@ -192,51 +203,62 @@ void TCPClient::setSocketTimeout(int timeout)
//send
bool
TCPClient
::
send
(
const
std
::
string
&
message
)
{
Packet
buf
;
buf
.
msgLen
=
htonl
(
message
.
length
());
strcpy
(
buf
.
text
,
message
.
c_str
());
if
(
writen
(
m_sockfd
,
&
buf
,
sizeof
(
buf
.
msgLen
)
+
message
.
length
())
==
-
1
)
bool
rlt
=
true
;
Packet
*
buf
=
nullptr
;
//LOG(INFO) << "message length:" << message.length();
int
msgLength
=
message
.
length
();
buf
=
(
Packet
*
)
malloc
(
msgLength
+
sizeof
(
Packet
));
memset
(
buf
,
0
,
msgLength
+
sizeof
(
Packet
));
buf
->
msgLen
=
htonl
(
message
.
length
());
strcpy
(
buf
->
text
,
message
.
c_str
());
if
(
writen
(
m_sockfd
,
buf
,
msgLength
+
sizeof
(
Packet
))
==
-
1
)
{
m_bValid
=
false
;
r
eturn
false
;
r
lt
=
false
;
}
return
true
;
free
(
buf
);
return
rlt
;
}
bool
TCPClient
::
receive
(
std
::
string
&
message
)
{
//首先读取头部
Packet
buf
=
{
0
,
0
};
size_t
readBytes
=
readn
(
m_sockfd
,
&
buf
.
msgLen
,
sizeof
(
buf
.
msgLen
));
//Packet buf = {0, 0};
unsigned
int
msgLen
=
0
;
size_t
readBytes
=
readn
(
m_sockfd
,
&
msgLen
,
sizeof
(
msgLen
));
if
(
readBytes
==
(
size_t
)
-
1
)
{
m_bValid
=
false
;
return
false
;
}
else
if
(
readBytes
!=
sizeof
(
buf
.
msgLen
))
else
if
(
readBytes
!=
sizeof
(
msgLen
))
{
m_bValid
=
false
;
return
false
;
}
//然后读取数据部分
unsigned
int
lenHost
=
ntohl
(
buf
.
msgLen
);
bool
rlt
=
true
;
unsigned
int
lenHost
=
ntohl
(
msgLen
);
char
*
temp
=
new
char
[
lenHost
+
1
];
if
(
lenHost
>
FM_BUF_SIZE
)
{
//如果数据长度大于最大可接收范围,返回false
LOG
(
INFO
)
<<
"Out of maximum range,message length:"
<<
lenHost
;
return
false
;
LOG
(
INFO
)
<<
"Too long message,the message length is:"
<<
lenHost
;
}
readBytes
=
readn
(
m_sockfd
,
buf
.
text
,
lenHost
);
readBytes
=
readn
(
m_sockfd
,
temp
,
lenHost
);
if
(
readBytes
==
(
size_t
)
-
1
)
{
m_bValid
=
false
;
r
eturn
false
;
r
lt
=
false
;
}
else
if
(
readBytes
!=
lenHost
)
{
m_bValid
=
false
;
r
eturn
false
;
r
lt
=
false
;
}
message
=
buf
.
text
;
return
true
;
temp
[
lenHost
]
=
'\0'
;
message
=
temp
;
delete
[]
temp
;
return
rlt
;
}
bool
TCPClient
::
read
(
void
*
buf
,
size_t
count
)
...
...
src/SocketModule.h
View file @
296c12ce
#ifndef SOCKET_MODULE_H
#
ifndef
SOCKET_MODULE_H
#define SOCKET_MODULE_H
#include <string>
...
...
@@ -19,7 +19,7 @@
#include <fcntl.h>
#endif
#define FM_BUF_SIZE 1024*1
5
#define FM_BUF_SIZE 1024*1
28
class
TCPSocket
{
...
...
@@ -49,7 +49,7 @@ private:
struct
Packet
{
unsigned
int
msgLen
;
//数据部分的长度(网络字节序)
char
text
[
FM_BUF_SIZE
];
//报文的数据部分
char
text
[
1
];
//报文的数据部分
};
public
:
TCPClient
()
:
m_bValid
(
false
){}
...
...
src/main.cpp
View file @
296c12ce
...
...
@@ -18,7 +18,7 @@
INITIALIZE_EASYLOGGINGPP
#define VERSION "1.0.6" //版本号
#define VERSION "1.0.6
Beta2
" //版本号
std
::
string
g_init_data
;
std
::
string
g_init_data_ods_back
;
...
...
@@ -254,7 +254,7 @@ int main(int argc,char *argv[])
el
::
Helpers
::
installPreRollOutCallback
(
logRolloutHandler
);
LOG
(
INFO
)
<<
"---------software start---------"
;
LOG
(
INFO
)
<<
"---------"
<<
"version"
<<
VERSION
<<
"---------"
;
// 读取配置文件信息
std
::
string
strIniPath
(
strBinPath
.
data
());
strIniPath
.
append
(
"config.ini"
);
...
...
@@ -430,7 +430,7 @@ bool order_send_to_pos(IN std::string &order_json,IN std::string &ods_json,OUT s
pos
.
setSocketTimeout
(
60
);
//设置超时
if
(
pos
.
write
(
order_json
.
c_str
())
)
{
char
tmpBuf
[
FM_BUF_SIZE
]
=
{
0
};
char
tmpBuf
[
1024
*
10
]
=
{
0
};
if
(
pos
.
read
(
tmpBuf
,
sizeof
(
tmpBuf
))
)
{
LOG
(
INFO
)
<<
"POS ===>> PLUGIN:"
<<
tmpBuf
;
...
...
@@ -477,7 +477,7 @@ void kill_origin_process()
std
::
string
tmp
=
"{
\"
fm_cmd
\"
: -1}"
;
if
(
pos
.
doConnect
(
client_listen_port
,
pos_ip
.
c_str
())){
if
(
pos
.
write
(
tmp
.
c_str
())){
char
tmpBuf
[
FM_BUF_SIZE
]
=
{
0
};
char
tmpBuf
[
100
]
=
{
0
};
if
(
pos
.
read
(
tmpBuf
,
sizeof
(
tmpBuf
))){
LOG
(
INFO
)
<<
"kill back:"
<<
tmpBuf
;
if
(
strcmp
(
tmpBuf
,
"100"
)
==
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