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 @@
#include "../3rdParty/easylogging/easylogging++.h"
TCPSocket::TCPSocket(): m_sockfd(-1){}
TCPSocket::~TCPSocket()
{
TCPSocket::~TCPSocket() {
if ( m_sockfd != -1 ){
#ifdef WIN32
closesocket(m_sockfd);
......@@ -12,11 +12,10 @@ TCPSocket::~TCPSocket()
::close(m_sockfd);
#endif
}
}
bool TCPSocket::create()
{
bool TCPSocket::create() {
close();
if ((m_sockfd = ::socket(AF_INET, SOCK_STREAM, 0)) == -1)
return false;
......@@ -40,24 +39,10 @@ bool TCPSocket::create()
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;
}
bool TCPSocket::bind(unsigned short int port, const char *ip) const
{
if ( m_sockfd == -1 )
......@@ -74,6 +59,8 @@ bool TCPSocket::bind(unsigned short int port, const char *ip) const
return false;
return true;
}
bool TCPSocket::listen(int backlog) const
{
if ( m_sockfd == -1 )
......@@ -83,6 +70,8 @@ bool TCPSocket::listen(int backlog) const
return false;
return true;
}
bool TCPSocket::accept(TCPSocket &clientSocket) const
{
if ( m_sockfd == -1 )
......@@ -95,6 +84,7 @@ bool TCPSocket::accept(TCPSocket &clientSocket) const
return true;
}
bool TCPSocket::connect(unsigned short int port, const char *ip) const
{
if ( m_sockfd == -1 )
......@@ -109,6 +99,7 @@ bool TCPSocket::connect(unsigned short int port, const char *ip) const
return true;
}
bool TCPSocket::setNonBlocking(bool flag) const
{
if ( m_sockfd == -1 )
......@@ -127,6 +118,8 @@ bool TCPSocket::setNonBlocking(bool flag) const
#endif
return true;
}
bool TCPSocket::reuseaddr() const
{
if ( m_sockfd == -1 )
......@@ -137,6 +130,8 @@ bool TCPSocket::reuseaddr() const
return false;
return true;
}
bool TCPSocket::close()
{
if ( m_sockfd == -1 )
......@@ -153,6 +148,7 @@ bool TCPSocket::close()
return true;
}
/** Server TCP Socket**/
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;
}
bool TCPServer::accept(TCPSocket &clientSocket) const
{
return TCPSocket::accept(clientSocket);
}
/** client端特有的send/receive **/
static ssize_t readn(int fd, void *buf, size_t count);
static ssize_t writen(int fd, const void *buf, size_t count);
/** client TCP Socket **/
bool TCPClient::doConnect(unsigned short port, const char *ip)
{
if( create() )
{
if( connect(port, ip) )
{
if( create() ) {
if( connect(port, ip) ) {
m_bValid = true;
return true;
}
......@@ -190,6 +187,7 @@ bool TCPClient::doConnect(unsigned short port, const char *ip)
return false;
}
void TCPClient::setSocketTimeout(int timeout)
{
#ifdef WIN32
......@@ -203,6 +201,7 @@ void TCPClient::setSocketTimeout(int timeout)
#endif // WIN32
}
//send
bool TCPClient::send(const std::string& message)
{
......@@ -222,26 +221,9 @@ bool TCPClient::send(const std::string& message)
}
delete[] tmpSend;
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)
{
//首先读取头部
......@@ -277,11 +259,18 @@ bool TCPClient::receive(std::string &message)
rlt= false;
}
temp[lenHost] = '\0';
#ifdef WIN32
message = Utf8ToGbk(temp);
#else
message = temp;
#endif
delete[] temp;
return rlt;
}
bool TCPClient::read(void *buf, size_t count)
{
#ifdef WIN32
......@@ -304,6 +293,7 @@ bool TCPClient::read(void *buf, size_t count)
return true;
}
bool TCPClient::write(const char *msg)
{
#ifdef WIN32
......@@ -352,6 +342,8 @@ static ssize_t readn(int fd, void *buf, size_t count)
}
return count;
}
static ssize_t writen(int fd, const void *buf, size_t count)
{
size_t nLeft = count;
......@@ -383,3 +375,37 @@ static ssize_t writen(int fd, const void *buf, size_t 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 @@
#include <Winsock2.h>
#pragma comment(lib,"ws2_32.lib")
#define ssize_t int
#include <windows.h>
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#endif
#define FM_BUF_SIZE 1024*128
class TCPSocket
{
class TCPSocket {
protected:
TCPSocket();
virtual ~TCPSocket();
......@@ -38,19 +37,15 @@ public:
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
{
class TCPClient : public TCPSocket {
private:
//struct Packet
//{
// unsigned int msgLen; //数据部分的长度(网络字节序)
// char text[1]; //报文的数据部分
//};
public:
TCPClient():m_bValid(false){}
~TCPClient(){}
......@@ -65,13 +60,19 @@ public:
bool isValid(){ return m_bValid; }
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:
bool m_bValid;
};
/** TCP Server **/
class TCPServer : public TCPSocket
{
class TCPServer : public TCPSocket {
public:
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