143 lines
2.6 KiB
C++
143 lines
2.6 KiB
C++
|
|
#ifndef _LUA_SOCKET_HPP_
|
|||
|
|
#define _LUA_SOCKET_HPP_
|
|||
|
|
|
|||
|
|
/******************************************************************
|
|||
|
|
*<EFBFBD><EFBFBD>װһ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>socket<EFBFBD><EFBFBD><EFBFBD><EFBFBD>lua<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
*
|
|||
|
|
*****************************************************************/
|
|||
|
|
#include "net/base_socket.h"
|
|||
|
|
|
|||
|
|
using namespace net;
|
|||
|
|
|
|||
|
|
class LuaSocket : public BaseSocket
|
|||
|
|
{
|
|||
|
|
public:
|
|||
|
|
LuaSocket(): buff_(NULL)
|
|||
|
|
{
|
|||
|
|
//printf("LuaSocket():%lld \n", (long long int)this);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
virtual ~LuaSocket()
|
|||
|
|
{
|
|||
|
|
//printf("~LuaSocket():%lld \n", (long long int)this);
|
|||
|
|
|
|||
|
|
if (buff_)
|
|||
|
|
{
|
|||
|
|
free(buff_);
|
|||
|
|
buff_ = NULL;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><>ʱֻ֧<D6BB><D6A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
|
|||
|
|
//ͨ<><CDA8>32λIP<49><50>ַ<EFBFBD><D6B7><EFBFBD>ӵ<EFBFBD><D3B5>ƶ<EFBFBD><C6B6>Ķ˿ڣ<CBBF><DAA3><EFBFBD><EFBFBD><EFBFBD>ֵΪsocket<65><74><EFBFBD><EFBFBD><EFBFBD>ţ<EFBFBD>0<EFBFBD><30>ʾ<EFBFBD>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڷ<EFBFBD><DAB7><EFBFBD><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD>0<EFBFBD><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
int connect(const double addr, const int port, bool block = true)
|
|||
|
|
{
|
|||
|
|
unsigned long long d;
|
|||
|
|
memcpy(&d, &addr, sizeof(addr));
|
|||
|
|
return BaseSocket::connect((u_long)d, port, block);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><D7BD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD><30>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD>ѶϿ<D1B6><CFBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>SOCKET_ERROR<4F><52>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
//<2F><>ʱֻ֧<D6BB><D6A7>д<EFBFBD><D0B4><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>httpЭ<70><D0AD>
|
|||
|
|
int send(char* buf)
|
|||
|
|
{
|
|||
|
|
if (!buf) return SOCKET_ERROR;
|
|||
|
|
|
|||
|
|
return BaseSocket::send((void*)buf, (int)strlen(buf));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const char* readline(int& retval)
|
|||
|
|
{
|
|||
|
|
retval = 0;
|
|||
|
|
// <20><><EFBFBD><EFBFBD>SOCKET_ERROR != retval && 0 != retval <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF>Զ<EFBFBD>
|
|||
|
|
const int max_size = 1024;
|
|||
|
|
|
|||
|
|
if (!buff_) buff_ = (char*)malloc(max_size);
|
|||
|
|
|
|||
|
|
retval = recv(buff_, max_size - 1, 0);
|
|||
|
|
|
|||
|
|
if (retval >= 0)
|
|||
|
|
buff_[retval] = 0;
|
|||
|
|
|
|||
|
|
return buff_;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const char* readall(int& retval)
|
|||
|
|
{
|
|||
|
|
//printf("readall start:%lld \n", (long long int)this);
|
|||
|
|
const int max_size = 1024;
|
|||
|
|
int total = 0;
|
|||
|
|
retval = 0;
|
|||
|
|
|
|||
|
|
for (;;)
|
|||
|
|
{
|
|||
|
|
total += max_size;
|
|||
|
|
buff_ = (char*)(buff_ ? realloc(buff_, total) : malloc(total));
|
|||
|
|
|
|||
|
|
assert(buff_);
|
|||
|
|
|
|||
|
|
int len = recv(buff_ + retval, total - retval - 1);
|
|||
|
|
|
|||
|
|
if (len <= 0)
|
|||
|
|
{
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
retval += len;
|
|||
|
|
|
|||
|
|
if (retval < (total - 1)) break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//printf("readall end:%lld \n", (long long int)this);
|
|||
|
|
buff_[retval] = 0;
|
|||
|
|
return buff_;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public:
|
|||
|
|
static LuaSocket* NewSocket()
|
|||
|
|
{
|
|||
|
|
//printf("NewSocket threadid %d\n", (int)GetCurrentThreadId());
|
|||
|
|
LuaSocket* s = new LuaSocket();
|
|||
|
|
int err = createSocket(&s->socket_);
|
|||
|
|
|
|||
|
|
if (err)
|
|||
|
|
{
|
|||
|
|
delete s;
|
|||
|
|
return NULL;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//printf("NewSocket:%lld \n", (long long int)s);
|
|||
|
|
return s;
|
|||
|
|
}
|
|||
|
|
static void Release(LuaSocket* s, int step)
|
|||
|
|
{
|
|||
|
|
if (!s) return;
|
|||
|
|
|
|||
|
|
//printf("Release:%lld \n", (long long int)s);
|
|||
|
|
|
|||
|
|
s->Close();
|
|||
|
|
delete s;
|
|||
|
|
}
|
|||
|
|
static double GetHostByName(const char* host)
|
|||
|
|
{
|
|||
|
|
hostent* hostn = gethostbyname(host);
|
|||
|
|
u_long addr = 0;
|
|||
|
|
|
|||
|
|
if (hostn)
|
|||
|
|
{
|
|||
|
|
addr = *(u_long*)hostn->h_addr_list[0];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
double d;
|
|||
|
|
memcpy(&d, &addr, sizeof(d));
|
|||
|
|
return d;
|
|||
|
|
}
|
|||
|
|
private:
|
|||
|
|
char* buff_;
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
#endif
|
|||
|
|
|