代码拉取完成,页面将自动刷新
同步操作将从 呵大官人/fooking 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#include "Connection.h"
#include "Log.h"
NS_USING;
Connection::Connection(EventLoop *loop, int fd):
pEventLoop(loop),
sSocket(fd),
bWriting(false),
bClosed(false),
pData(NULL),
nPort(0),
nTimeout(0),
nTimerId(0),
nError(0)
{
EV_CB_INIT(cbRead);
EV_CB_INIT(cbClose);
EV_CB_INIT(cbWriteComplete);
EV_CB_INIT(cbConnect);
memset(sHost, 0, sizeof(sHost));
if(sSocket.isValid()){
sSocket.setNonBlock();
initSocket();
}
}
Connection::~Connection()
{
//TODO
}
void Connection::initSocket()
{
sSocket.setNonDelay();
sSocket.setKeepAlive();
pEventLoop->addEventListener(sSocket.getFd(), EV_IO_READ, EV_IO_CB(this, Connection::onRead), NULL);
}
void Connection::initConnectEvent()
{
pEventLoop->addEventListener(sSocket.getFd(), EV_IO_ALL, EV_IO_CB(this, Connection::onConnect), NULL);
if(nTimeout){
nTimerId = pEventLoop->setTimer(nTimeout, EV_TIMER_CB(this, Connection::onTimeout));
}
}
void Connection::setHostAndPort(const char *host, short port)
{
strcpy(sHost, host);
nPort = port;
}
int Connection::connectTcp(const char *host, short port)
{
if(sSocket.connectTcp(host, port, 1) == SOCKET_ERR){
int err = sSocket.getError();
LOG_ERR("connect tcp server failure, errno=%d, error=%s", err, strerror(err));
return -1;
}
initConnectEvent();
return 0;
}
int Connection::connectUnix(const char *path)
{
if(sSocket.connectUnix(path, 1) == SOCKET_ERR){
int err = sSocket.getError();
LOG_ERR("connect unix domain failure, errno=%d", err, strerror(err));
return -1;
}
initConnectEvent();
return 0;
}
int Connection::send(const char *data, int len)
{
writeBuffer.append(data, len);
if(!bWriting){
bWriting = true;
pEventLoop->addEventListener(sSocket.getFd(), EV_IO_WRITE, EV_IO_CB(this, Connection::onWrite), NULL);
}
return 0;
}
int Connection::send(Buffer &buffer)
{
return send(buffer.data(), buffer.size());
}
void Connection::close()
{
if(bClosed){
return ;
}
//remove io event
pEventLoop->removeEventListener(sSocket.getFd(), EV_IO_ALL);
//remove timer event
if(nTimerId){
pEventLoop->stopTimer(nTimerId);
}
bClosed = true;
sSocket.close();
EV_INVOKE(cbClose, this);
}
void Connection::onTimeout(TimerId id, void *data)
{
nError = ETIMEDOUT;
close();
}
void Connection::onConnect(int fd, int r, void *data)
{
int sockerr = 0;
socklen_t errlen = sizeof(sockerr);
//remove all event
pEventLoop->removeEventListener(sSocket.getFd(), EV_IO_ALL);
//remove timer
if(nTimeout){
pEventLoop->stopTimer(nTimerId);
}
//check fd
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &sockerr, &errlen) == -1){
sockerr = errno;
}
//error
if(sockerr){
nError = sockerr;
close();
return ;
}
initSocket();
EV_INVOKE(cbConnect, this);
}
void Connection::onRead(int fd, int r, void *data)
{
char buf[8192];
int n = sSocket.read(buf, 8192);
LOG("on read, fd=%d, recv=%d", fd, n);
if(n == SOCKET_ERR){
nError = sSocket.getError();
close();
}else if(n > 0){
readBuffer.append(buf, n);
EV_INVOKE(cbRead, this);
}
}
void Connection::onWrite(int fd, int r, void *data)
{
char *buffer = writeBuffer.data();
size_t size = writeBuffer.size();
int n = sSocket.write(buffer, size);
LOG("write to fd=%d, len=%d", fd, n);
if(n == SOCKET_ERR){
nError = sSocket.getError();
close();
}else if(n > 0){
int remain = writeBuffer.seek(n);
if(remain <= 0){
LOG("write completed");
bWriting = false;
pEventLoop->removeEventListener(sSocket.getFd(), EV_IO_WRITE);
EV_INVOKE(cbWriteComplete, this);
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。