1 Star 0 Fork 107

nmred/fooking

forked from 呵大官人/fooking 
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Connection.cpp 3.45 KB
一键复制 编辑 原始数据 按行查看 历史
呵大官人 提交于 2015-05-28 19:00 +08:00 . log
#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);
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/nmred/fooking.git
git@gitee.com:nmred/fooking.git
nmred
fooking
fooking
master

搜索帮助