Commit 296c12ce by guanghui.cui

socket发送接收 空间动态分配

parent e160fdd7
#include "SocketModule.h" #include "SocketModule.h"
#include <stdio.h> #include <stdio.h>
#include "../3rdParty/easylogging/easylogging++.h" #include "../3rdParty/easylogging/easylogging++.h"
...@@ -22,27 +22,38 @@ bool TCPSocket::create() ...@@ -22,27 +22,38 @@ bool TCPSocket::create()
return false; return false;
//查看默认 发送/接收 缓存区大小 //查看默认 发送/接收 缓存区大小
// int rcvbuf_len; int rcvbuf_len;
//#ifdef WIN32 #ifdef WIN32
// int len; int len;
//#else #else
// socklen_t len; socklen_t len;
//#endif // WIN32 #endif // WIN32
//
// len = sizeof(rcvbuf_len); int buf_len_set = 128 * 1024; //设置 发送/接收 缓存区为128k
// if (getsockopt(m_sockfd, SOL_SOCKET, SO_RCVBUF, (char *)&rcvbuf_len, &len) < 0) { len = sizeof(buf_len_set);
// perror("getsockopt: ");
// return -1; if (setsockopt(m_sockfd, SOL_SOCKET, SO_SNDBUF, (char *)&buf_len_set, len) <0 ) {
// } LOG(INFO) << "setsockopt SO_SNDBUF error";
// LOG(INFO) << "the recevice buf len:" << rcvbuf_len; }
//
// int sendbuf_len; if (setsockopt(m_sockfd, SOL_SOCKET, SO_RCVBUF, (char *)&buf_len_set, len) <0 ) {
// len = sizeof(sendbuf_len); LOG(INFO) << "setsockopt SO_RCVBUF error:" ;
// if (getsockopt(m_sockfd, SOL_SOCKET, SO_SNDBUF, (char *)&sendbuf_len, &len) < 0) { }
// perror("getsockopt: ");
// return -1; //len = sizeof(rcvbuf_len);
// } //if (getsockopt(m_sockfd, SOL_SOCKET, SO_RCVBUF, (char *)&rcvbuf_len, &len) < 0) {
// LOG(INFO) << "the send buf len:" << sendbuf_len; // 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; return true;
} }
...@@ -192,51 +203,62 @@ void TCPClient::setSocketTimeout(int timeout) ...@@ -192,51 +203,62 @@ void TCPClient::setSocketTimeout(int timeout)
//send //send
bool TCPClient::send(const std::string& message) bool TCPClient::send(const std::string& message)
{ {
Packet buf; bool rlt = true;
buf.msgLen = htonl(message.length()); Packet *buf = nullptr;
strcpy(buf.text, message.c_str());
if (writen(m_sockfd, &buf, sizeof(buf.msgLen)+message.length()) == -1) //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; m_bValid = false;
return false; rlt = false;
} }
return true; free(buf);
return rlt;
} }
bool TCPClient::receive(std::string &message) bool TCPClient::receive(std::string &message)
{ {
//首先读取头部 //首先读取头部
Packet buf = {0, 0}; //Packet buf = {0, 0};
size_t readBytes = readn(m_sockfd, &buf.msgLen, sizeof(buf.msgLen)); unsigned int msgLen = 0;
size_t readBytes = readn(m_sockfd, &msgLen, sizeof(msgLen));
if (readBytes == (size_t)-1) if (readBytes == (size_t)-1)
{ {
m_bValid = false; m_bValid = false;
return false; return false;
} }
else if (readBytes != sizeof(buf.msgLen)) else if (readBytes != sizeof(msgLen))
{ {
m_bValid = false; m_bValid = false;
return 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) { if (lenHost > FM_BUF_SIZE) {
//如果数据长度大于最大可接收范围,返回false LOG(INFO) << "Too long message,the message length is:" << lenHost;
LOG(INFO) << "Out of maximum range,message length:" << lenHost;
return false;
} }
readBytes = readn(m_sockfd, buf.text, lenHost); readBytes = readn(m_sockfd, temp, lenHost);
if (readBytes == (size_t)-1) if (readBytes == (size_t)-1)
{ {
m_bValid = false; m_bValid = false;
return false; rlt= false;
} }
else if (readBytes != lenHost) else if (readBytes != lenHost)
{ {
m_bValid = false; m_bValid = false;
return false; rlt= false;
} }
message = buf.text; temp[lenHost] = '\0';
return true; message = temp;
delete[] temp;
return rlt;
} }
bool TCPClient::read(void *buf, size_t count) bool TCPClient::read(void *buf, size_t count)
......
#ifndef SOCKET_MODULE_H #ifndef SOCKET_MODULE_H
#define SOCKET_MODULE_H #define SOCKET_MODULE_H
#include <string> #include <string>
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
#include <fcntl.h> #include <fcntl.h>
#endif #endif
#define FM_BUF_SIZE 1024*15 #define FM_BUF_SIZE 1024*128
class TCPSocket class TCPSocket
{ {
...@@ -49,7 +49,7 @@ private: ...@@ -49,7 +49,7 @@ private:
struct Packet struct Packet
{ {
unsigned int msgLen; //数据部分的长度(网络字节序) unsigned int msgLen; //数据部分的长度(网络字节序)
char text[FM_BUF_SIZE]; //报文的数据部分 char text[1]; //报文的数据部分
}; };
public: public:
TCPClient():m_bValid(false){} TCPClient():m_bValid(false){}
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
INITIALIZE_EASYLOGGINGPP INITIALIZE_EASYLOGGINGPP
#define VERSION "1.0.6" //版本号 #define VERSION "1.0.6 Beta2" //版本号
std::string g_init_data; std::string g_init_data;
std::string g_init_data_ods_back; std::string g_init_data_ods_back;
...@@ -254,7 +254,7 @@ int main(int argc,char *argv[]) ...@@ -254,7 +254,7 @@ int main(int argc,char *argv[])
el::Helpers::installPreRollOutCallback(logRolloutHandler); el::Helpers::installPreRollOutCallback(logRolloutHandler);
LOG(INFO) << "---------software start---------"; LOG(INFO) << "---------software start---------";
LOG(INFO) << "---------"<< "version"<< VERSION <<"---------";
// 读取配置文件信息 // 读取配置文件信息
std::string strIniPath(strBinPath.data()); std::string strIniPath(strBinPath.data());
strIniPath.append("config.ini"); 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 ...@@ -430,7 +430,7 @@ bool order_send_to_pos(IN std::string &order_json,IN std::string &ods_json,OUT s
pos.setSocketTimeout(60); //设置超时 pos.setSocketTimeout(60); //设置超时
if( pos.write(order_json.c_str()) ) if( pos.write(order_json.c_str()) )
{ {
char tmpBuf[FM_BUF_SIZE] = {0}; char tmpBuf[1024*10] = {0};
if( pos.read(tmpBuf,sizeof(tmpBuf)) ) if( pos.read(tmpBuf,sizeof(tmpBuf)) )
{ {
LOG(INFO) << "POS ===>> PLUGIN:"<<tmpBuf; LOG(INFO) << "POS ===>> PLUGIN:"<<tmpBuf;
...@@ -477,7 +477,7 @@ void kill_origin_process() ...@@ -477,7 +477,7 @@ void kill_origin_process()
std::string tmp = "{\"fm_cmd\": -1}"; std::string tmp = "{\"fm_cmd\": -1}";
if (pos.doConnect(client_listen_port, pos_ip.c_str())){ if (pos.doConnect(client_listen_port, pos_ip.c_str())){
if (pos.write(tmp.c_str())){ if (pos.write(tmp.c_str())){
char tmpBuf[FM_BUF_SIZE] = { 0 }; char tmpBuf[100] = { 0 };
if (pos.read(tmpBuf, sizeof(tmpBuf))){ if (pos.read(tmpBuf, sizeof(tmpBuf))){
LOG(INFO) << "kill back:" << tmpBuf; LOG(INFO) << "kill back:" << tmpBuf;
if (strcmp(tmpBuf, "100") == 0) { if (strcmp(tmpBuf, "100") == 0) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment