Commit 48d20d1e by wuyang.zou

Fix Windows Simulator Bug:

    Recv ODS push Order Doc Need utf8 -> gbk, otherwise Windows Simulator Will Block
parent 19f0c495
...@@ -3,8 +3,8 @@ ...@@ -3,8 +3,8 @@
#include "../3rdParty/easylogging/easylogging++.h" #include "../3rdParty/easylogging/easylogging++.h"
TCPSocket::TCPSocket(): m_sockfd(-1){} TCPSocket::TCPSocket(): m_sockfd(-1){}
TCPSocket::~TCPSocket()
{ TCPSocket::~TCPSocket() {
if ( m_sockfd != -1 ){ if ( m_sockfd != -1 ){
#ifdef WIN32 #ifdef WIN32
closesocket(m_sockfd); closesocket(m_sockfd);
...@@ -12,11 +12,10 @@ TCPSocket::~TCPSocket() ...@@ -12,11 +12,10 @@ TCPSocket::~TCPSocket()
::close(m_sockfd); ::close(m_sockfd);
#endif #endif
} }
} }
bool TCPSocket::create()
{ bool TCPSocket::create() {
close(); close();
if ((m_sockfd = ::socket(AF_INET, SOCK_STREAM, 0)) == -1) if ((m_sockfd = ::socket(AF_INET, SOCK_STREAM, 0)) == -1)
return false; return false;
...@@ -40,24 +39,10 @@ bool TCPSocket::create() ...@@ -40,24 +39,10 @@ bool TCPSocket::create()
LOG(INFO) << "setsockopt SO_RCVBUF error:" ; 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; return true;
} }
bool TCPSocket::bind(unsigned short int port, const char *ip) const bool TCPSocket::bind(unsigned short int port, const char *ip) const
{ {
if ( m_sockfd == -1 ) if ( m_sockfd == -1 )
...@@ -74,6 +59,8 @@ bool TCPSocket::bind(unsigned short int port, const char *ip) const ...@@ -74,6 +59,8 @@ bool TCPSocket::bind(unsigned short int port, const char *ip) const
return false; return false;
return true; return true;
} }
bool TCPSocket::listen(int backlog) const bool TCPSocket::listen(int backlog) const
{ {
if ( m_sockfd == -1 ) if ( m_sockfd == -1 )
...@@ -83,6 +70,8 @@ bool TCPSocket::listen(int backlog) const ...@@ -83,6 +70,8 @@ bool TCPSocket::listen(int backlog) const
return false; return false;
return true; return true;
} }
bool TCPSocket::accept(TCPSocket &clientSocket) const bool TCPSocket::accept(TCPSocket &clientSocket) const
{ {
if ( m_sockfd == -1 ) if ( m_sockfd == -1 )
...@@ -95,6 +84,7 @@ bool TCPSocket::accept(TCPSocket &clientSocket) const ...@@ -95,6 +84,7 @@ bool TCPSocket::accept(TCPSocket &clientSocket) const
return true; return true;
} }
bool TCPSocket::connect(unsigned short int port, const char *ip) const bool TCPSocket::connect(unsigned short int port, const char *ip) const
{ {
if ( m_sockfd == -1 ) if ( m_sockfd == -1 )
...@@ -109,6 +99,7 @@ bool TCPSocket::connect(unsigned short int port, const char *ip) const ...@@ -109,6 +99,7 @@ bool TCPSocket::connect(unsigned short int port, const char *ip) const
return true; return true;
} }
bool TCPSocket::setNonBlocking(bool flag) const bool TCPSocket::setNonBlocking(bool flag) const
{ {
if ( m_sockfd == -1 ) if ( m_sockfd == -1 )
...@@ -127,6 +118,8 @@ bool TCPSocket::setNonBlocking(bool flag) const ...@@ -127,6 +118,8 @@ bool TCPSocket::setNonBlocking(bool flag) const
#endif #endif
return true; return true;
} }
bool TCPSocket::reuseaddr() const bool TCPSocket::reuseaddr() const
{ {
if ( m_sockfd == -1 ) if ( m_sockfd == -1 )
...@@ -137,6 +130,8 @@ bool TCPSocket::reuseaddr() const ...@@ -137,6 +130,8 @@ bool TCPSocket::reuseaddr() const
return false; return false;
return true; return true;
} }
bool TCPSocket::close() bool TCPSocket::close()
{ {
if ( m_sockfd == -1 ) if ( m_sockfd == -1 )
...@@ -153,6 +148,7 @@ bool TCPSocket::close() ...@@ -153,6 +148,7 @@ bool TCPSocket::close()
return true; return true;
} }
/** Server TCP Socket**/ /** Server TCP Socket**/
bool TCPServer::doListen(unsigned short int port, const char *ip, int backlog) bool TCPServer::doListen(unsigned short int port, const char *ip, int backlog)
{ {
...@@ -167,22 +163,23 @@ bool TCPServer::doListen(unsigned short int port, const char *ip, int backlog) ...@@ -167,22 +163,23 @@ bool TCPServer::doListen(unsigned short int port, const char *ip, int backlog)
return false; return false;
} }
bool TCPServer::accept(TCPSocket &clientSocket) const bool TCPServer::accept(TCPSocket &clientSocket) const
{ {
return TCPSocket::accept(clientSocket); return TCPSocket::accept(clientSocket);
} }
/** client端特有的send/receive **/ /** client端特有的send/receive **/
static ssize_t readn(int fd, void *buf, size_t count); static ssize_t readn(int fd, void *buf, size_t count);
static ssize_t writen(int fd, const void *buf, size_t count); static ssize_t writen(int fd, const void *buf, size_t count);
/** client TCP Socket **/ /** client TCP Socket **/
bool TCPClient::doConnect(unsigned short port, const char *ip) bool TCPClient::doConnect(unsigned short port, const char *ip)
{ {
if( create() ) if( create() ) {
{ if( connect(port, ip) ) {
if( connect(port, ip) )
{
m_bValid = true; m_bValid = true;
return true; return true;
} }
...@@ -190,6 +187,7 @@ bool TCPClient::doConnect(unsigned short port, const char *ip) ...@@ -190,6 +187,7 @@ bool TCPClient::doConnect(unsigned short port, const char *ip)
return false; return false;
} }
void TCPClient::setSocketTimeout(int timeout) void TCPClient::setSocketTimeout(int timeout)
{ {
#ifdef WIN32 #ifdef WIN32
...@@ -203,6 +201,7 @@ void TCPClient::setSocketTimeout(int timeout) ...@@ -203,6 +201,7 @@ void TCPClient::setSocketTimeout(int timeout)
#endif // WIN32 #endif // WIN32
} }
//send //send
bool TCPClient::send(const std::string& message) bool TCPClient::send(const std::string& message)
{ {
...@@ -222,26 +221,9 @@ bool TCPClient::send(const std::string& message) ...@@ -222,26 +221,9 @@ bool TCPClient::send(const std::string& message)
} }
delete[] tmpSend; delete[] tmpSend;
return rlt; return rlt;
//----------------用结构体写法--------------
//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;
// rlt = false;
//}
//free(buf);
//return rlt;
} }
bool TCPClient::receive(std::string &message) bool TCPClient::receive(std::string &message)
{ {
//首先读取头部 //首先读取头部
...@@ -277,11 +259,18 @@ bool TCPClient::receive(std::string &message) ...@@ -277,11 +259,18 @@ bool TCPClient::receive(std::string &message)
rlt= false; rlt= false;
} }
temp[lenHost] = '\0'; temp[lenHost] = '\0';
#ifdef WIN32
message = Utf8ToGbk(temp);
#else
message = temp; message = temp;
#endif
delete[] temp; delete[] temp;
return rlt; return rlt;
} }
bool TCPClient::read(void *buf, size_t count) bool TCPClient::read(void *buf, size_t count)
{ {
#ifdef WIN32 #ifdef WIN32
...@@ -304,6 +293,7 @@ bool TCPClient::read(void *buf, size_t count) ...@@ -304,6 +293,7 @@ bool TCPClient::read(void *buf, size_t count)
return true; return true;
} }
bool TCPClient::write(const char *msg) bool TCPClient::write(const char *msg)
{ {
#ifdef WIN32 #ifdef WIN32
...@@ -352,6 +342,8 @@ static ssize_t readn(int fd, void *buf, size_t count) ...@@ -352,6 +342,8 @@ static ssize_t readn(int fd, void *buf, size_t count)
} }
return count; return count;
} }
static ssize_t writen(int fd, const void *buf, size_t count) static ssize_t writen(int fd, const void *buf, size_t count)
{ {
size_t nLeft = count; size_t nLeft = count;
...@@ -383,3 +375,37 @@ static ssize_t writen(int fd, const void *buf, size_t count) ...@@ -383,3 +375,37 @@ static ssize_t writen(int fd, const void *buf, size_t count)
} }
return count; return count;
} }
#ifdef WIN32
std::string TCPClient::Utf8ToGbk(const char *src_str) {
int len = MultiByteToWideChar(CP_UTF8, 0, src_str, -1, NULL, 0);
wchar_t* wszGBK = new wchar_t[len + 1];
memset(wszGBK, 0, len * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, src_str, -1, wszGBK, len);
len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
char* szGBK = new char[len + 1];
memset(szGBK, 0, len + 1);
WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, szGBK, len, NULL, NULL);
std::string strTemp(szGBK);
if (wszGBK) delete[] wszGBK;
if (szGBK) delete[] szGBK;
return strTemp;
}
std::string TCPClient::GbkToUtf8(const char *src_str) {
int len = MultiByteToWideChar(CP_ACP, 0, src_str, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len + 1];
memset(wstr, 0, len + 1);
MultiByteToWideChar(CP_ACP, 0, src_str, -1, wstr, len);
len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
char* str = new char[len + 1];
memset(str, 0, len + 1);
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
std::string strTemp = str;
if (wstr) delete[] wstr;
if (str) delete[] str;
return strTemp;
}
#endif
...@@ -10,19 +10,18 @@ ...@@ -10,19 +10,18 @@
#include <Winsock2.h> #include <Winsock2.h>
#pragma comment(lib,"ws2_32.lib") #pragma comment(lib,"ws2_32.lib")
#define ssize_t int #define ssize_t int
#include <windows.h>
#else #else
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#endif #endif
#define FM_BUF_SIZE 1024*128 #define FM_BUF_SIZE 1024*128
class TCPSocket class TCPSocket {
{
protected: protected:
TCPSocket(); TCPSocket();
virtual ~TCPSocket(); virtual ~TCPSocket();
...@@ -38,19 +37,15 @@ public: ...@@ -38,19 +37,15 @@ public:
int getfd() const{ return m_sockfd; } int getfd() const{ return m_sockfd; }
//flag: true=SetNonBlock, false=SetBlock //flag: true=SetNonBlock, false=SetBlock
bool setNonBlocking(bool flag) const; bool setNonBlocking(bool flag) const;
protected: protected:
int m_sockfd; int m_sockfd;
}; };
/** TCP Client **/ /** TCP Client **/
class TCPClient : public TCPSocket class TCPClient : public TCPSocket {
{
private: private:
//struct Packet
//{
// unsigned int msgLen; //数据部分的长度(网络字节序)
// char text[1]; //报文的数据部分
//};
public: public:
TCPClient():m_bValid(false){} TCPClient():m_bValid(false){}
~TCPClient(){} ~TCPClient(){}
...@@ -65,13 +60,19 @@ public: ...@@ -65,13 +60,19 @@ public:
bool isValid(){ return m_bValid; } bool isValid(){ return m_bValid; }
void setValid(bool valid){ m_bValid=valid; } void setValid(bool valid){ m_bValid=valid; }
#ifdef WIN32
std::string Utf8ToGbk(const char *src_str);
std::string GbkToUtf8(const char *src_str);
#endif
private: private:
bool m_bValid; bool m_bValid;
}; };
/** TCP Server **/ /** TCP Server **/
class TCPServer : public TCPSocket class TCPServer : public TCPSocket {
{
public: public:
TCPServer(){} TCPServer(){}
~TCPServer(){} ~TCPServer(){}
......
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