本文整理汇总了C++中Link::fd方法的典型用法代码示例。如果您正苦于以下问题:C++ Link::fd方法的具体用法?C++ Link::fd怎么用?C++ Link::fd使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Link
的用法示例。
在下文中一共展示了Link::fd方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: accept_session
Session* Server::accept_session(){
Link *link = serv_link->accept();
if(link == NULL){
log_error("accept failed! %s", strerror(errno));
return NULL;
}
link->nodelay();
link->noblock();
link->create_time = millitime();
link->active_time = link->create_time;
Session *sess = new Session();
sess->link = link;
this->sessions[sess->id] = sess;
for(int i=0; i<this->handlers.size(); i++){
Handler *handler = this->handlers[i];
HandlerState state = handler->accept(*sess);
if(state == HANDLE_FAIL){
delete link;
delete sess;
return NULL;
}
}
this->link_count ++;
log_debug("new link from %s:%d, fd: %d, links: %d",
link->remote_ip, link->remote_port, link->fd(), this->link_count);
fdes->set(link->fd(), FDEVENT_IN, DEFAULT_TYPE, sess);
return sess;
}
示例2: proc_result
int NetworkServer::proc_result(ProcJob *job, ready_list_t *ready_list){
Link *link = job->link;
int result = job->result;
if(job->cmd){
job->cmd->calls += 1;
job->cmd->time_wait += job->time_wait;
job->cmd->time_proc += job->time_proc;
}
delete job;
if(result == PROC_ERROR){
link->mark_error();
ready_list->push_back(link);
}else{
if(link->output->empty()){
fdes->clr(link->fd(), FDEVENT_OUT);
if(link->input->empty()){
fdes->set(link->fd(), FDEVENT_IN, 1, link);
}else{
ready_list->push_back(link);
}
}else{
fdes->clr(link->fd(), FDEVENT_IN);
fdes->set(link->fd(), FDEVENT_OUT, 1, link);
}
}
return result;
}
示例3: proc_client_event
/*
event:
read => ready_list OR close
write => NONE
proc =>
done: write & (read OR ready_list)
async: stop (read & write)
1. When writing to a link, it may happen to be in the ready_list,
so we cannot close that link in write process, we could only
just mark it as closed.
2. When reading from a link, it is never in the ready_list, so it
is safe to close it in read process, also safe to put it into
ready_list.
3. Ignore FDEVENT_ERR
A link is in either one of these places:
1. ready list
2. async worker queue
So it safe to delete link when processing ready list and async worker result.
*/
int NetworkServer::proc_client_event(const Fdevent *fde, ready_list_t *ready_list){
Link *link = (Link *)fde->data.ptr;
if(fde->events & FDEVENT_IN){
ready_list->push_back(link);
if(link->error()){
return 0;
}
int len = link->read();
//log_debug("fd: %d read: %d", link->fd(), len);
if(len <= 0){
log_debug("fd: %d, read: %d, delete link", link->fd(), len);
link->mark_error();
return 0;
}
}
if(fde->events & FDEVENT_OUT){
if(link->error()){
return 0;
}
int len = link->write();
if(len <= 0){
log_debug("fd: %d, write: %d, delete link", link->fd(), len);
link->mark_error();
return 0;
}
if(link->output->empty()){
fdes->clr(link->fd(), FDEVENT_OUT);
}
}
return 0;
}
示例4: read_session
int Server::read_session(Session *sess){
Link *link = sess->link;
if(link->error()){
return 0;
}
int len = link->read();
if(len <= 0){
this->close_session(sess);
return -1;
}
while(1){
Request req;
int ret = link->recv(&req.msg);
if(ret == -1){
log_info("fd: %d, parse error, delete link", link->fd());
this->close_session(sess);
return -1;
}else if(ret == 0){
// 报文未就绪, 继续读网络
break;
}
req.stime = millitime();
req.sess = *sess;
Response resp;
for(int i=0; i<this->handlers.size(); i++){
Handler *handler = this->handlers[i];
req.time_wait = 1000 * (millitime() - req.stime);
HandlerState state = handler->proc(req, &resp);
req.time_proc = 1000 * (millitime() - req.stime) - req.time_wait;
if(state == HANDLE_RESP){
link->send(resp.msg);
if(link && !link->output.empty()){
fdes->set(link->fd(), FDEVENT_OUT, DEFAULT_TYPE, sess);
}
if(log_level() >= Logger::LEVEL_DEBUG){
log_debug("w:%.3f,p:%.3f, req: %s resp: %s",
req.time_wait, req.time_proc,
msg_str(req.msg).c_str(),
msg_str(resp.msg).c_str());
}
}else if(state == HANDLE_FAIL){
this->close_session(sess);
return -1;
}
}
}
return 0;
}
示例5: close_session
int Server::close_session(Session *sess){
Link *link = sess->link;
for(int i=0; i<this->handlers.size(); i++){
Handler *handler = this->handlers[i];
handler->close(*sess);
}
this->link_count --;
log_debug("delete link %s:%d, fd: %d, links: %d",
link->remote_ip, link->remote_port, link->fd(), this->link_count);
fdes->del(link->fd());
this->sessions.erase(sess->id);
delete link;
delete sess;
return 0;
}
示例6: write_session
int Server::write_session(Session *sess){
Link *link = sess->link;
if(link->error()){
return 0;
}
int len = link->write();
if(len <= 0){
log_debug("fd: %d, write: %d, delete link", link->fd(), len);
this->close_session(sess);
return -1;
}
if(link->output.empty()){
fdes->clr(link->fd(), FDEVENT_OUT);
}
return 0;
}
示例7: loop_once
int Server::loop_once(){
const Fdevents::events_t *events;
events = fdes->wait(20);
if(events == NULL){
log_fatal("events.wait error: %s", strerror(errno));
return 0;
}
for(int i=0; i<(int)events->size(); i++){
const Fdevent *fde = events->at(i);
if(fde->data.ptr == serv_link){
this->accept_session();
}else if(fde->data.num == HANDLER_TYPE){
Handler *handler = (Handler *)fde->data.ptr;
while(Response *resp = handler->handle()){
Session *sess = this->get_session(resp->sess.id);
if(sess){
Link *link = sess->link;
link->send(resp->msg);
if(link && !link->output.empty()){
fdes->set(link->fd(), FDEVENT_OUT, DEFAULT_TYPE, sess);
}
}
delete resp;
}
}else{
Session *sess = (Session *)fde->data.ptr;
Link *link = sess->link;
if(fde->events & FDEVENT_IN){
if(this->read_session(sess) == -1){
continue;
}
}
if(fde->events & FDEVENT_OUT){
if(this->write_session(sess) == -1){
continue;
}
}
if(link && !link->output.empty()){
fdes->set(link->fd(), FDEVENT_OUT, DEFAULT_TYPE, sess);
}
}
}
return 0;
}
示例8: proc_client_event
int NetworkServer::proc_client_event(const Fdevent *fde, ready_list_t *ready_list){
Link *link = (Link *)fde->data.ptr;
if(fde->events & FDEVENT_ERR){
ready_list->push_back(link);
log_warn("fd: %d, wait error, delete link", link->fd());
link->mark_error();
return 0;
}
if(fde->events & FDEVENT_IN){
ready_list->push_back(link);
if(link->error()){
return 0;
}
int len = link->read();
if(len <= 0){
if(link->readed < sizeof(head_t) || link->request.head.bodylen < MAX_PACKAGE_SIZE){
log_frame("fd: %d, read: %d, delete link [ readed: %d, head_size: %d, bodylen: %u, max: %d ]",
link->fd(), len, link->readed, sizeof(head_t), link->request.head.bodylen, MAX_PACKAGE_SIZE);
link->mark_error();
}
return 0;
}
}
if(fde->events & FDEVENT_OUT){
if(link->error()){
return 0;
}
int len = link->write();
if(len < 0){
log_warn("[%s] fd: %d, write: %d, delete link", link->get_basic_string(), link->fd(), len);
link->mark_error();
return 0;
}
if(link->response.head.bodylen + sizeof(head_t) <= link->writed){
log_frame("[%s] fd: %d, write: %d, finish writing", link->get_basic_string());
fdes->clr(link->fd(), FDEVENT_OUT);
}
if(!long_connect || 0 != link->response.head.status){
link_count--;
fdes->del(link->fd());
log_frame("[%s] short connection, close [ fd: %d ]", link->get_basic_string(), link->fd());
SAFE_DELETE(link);
}else{
link->readed = link->writed = 0;
link->request.reset();
link->response.reset();
link->reset_basic_string();
fdes->set(link->fd(), FDEVENT_IN, 1, link);
log_frame("[%s] long connection, try to receive request next time [ fd: %d ]", link->get_basic_string(), link->fd());
}
}
return 0;
}
示例9: init_links
void init_links(int num, const char *ip, int port){
fdes = new Fdevents();
free_links = new std::vector<Link *>();
for(int i=0; i<num; i++){
Link *link = Link::connect(ip, port);
if(!link){
fprintf(stderr, "connect error! %s\n", strerror(errno));
exit(0);
}
fdes->set(link->fd(), FDEVENT_IN, 0, link);
free_links->push_back(link);
}
}
示例10: proc_client_event
/*
event:
read => ready_list OR close
write => ready_list
proc_result =>
done: write & (read OR ready_list)
async: stop (read & write)
1. When writing to a link, it may happen to be in the ready_list,
so we cannot close that link in write process, we could only
just mark it as closed.
2. When reading from a link, it is never in the ready_list, so it
is safe to close it in read process, also safe to put it into
ready_list.
3. Ignore FDEVENT_ERR
A link is in either one of these places:
1. ready list
2. async worker queue
3. fdes
So it safe to delete link when processing ready list and async worker result.
*/
int NetworkServer::proc_client_event(const Fdevent *fde, ready_list_t *ready_list){
Link *link = (Link *)fde->data.ptr;
if(fde->events & FDEVENT_IN){
int len = link->read();
//log_debug("fd: %d read: %d", link->fd(), len);
if(len <= 0){
double serv_time = millitime() - link->create_time;
log_debug("fd: %d, read: %d, delete link, s:%.3f", link->fd(), len, serv_time);
link->mark_error();
ready_list->push_back(link);
return 0;
}
ready_list->push_back(link);
}else if(fde->events & FDEVENT_OUT){
int len = link->write();
//log_debug("fd: %d, write: %d", link->fd(), len);
if(len <= 0){
log_debug("fd: %d, write: %d, delete link", link->fd(), len);
link->mark_error();
ready_list->push_back(link);
return 0;
}
if(link->output->empty()){
fdes->clr(link->fd(), FDEVENT_OUT);
if(link->input->empty()){
fdes->set(link->fd(), FDEVENT_IN, 1, link);
}else{
ready_list->push_back(link);
}
}else{
fdes->clr(link->fd(), FDEVENT_IN);
fdes->set(link->fd(), FDEVENT_OUT, 1, link);
}
}
return 0;
}
示例11: proc_result
int NetworkServer::proc_result(ProcJob *job, ready_list_t *ready_list){
Link *link = job->link;
int result = job->result;
log_debug("[%s] process finished, wait time:%.3f,proc time:%.3f, method: %s, status: %u",
job->link->get_basic_string(), job->time_wait, job->time_proc,
job->req->head.method,
job->link->response.head.status);
if(job->cmd){
job->cmd->calls += 1;
job->cmd->time_wait += job->time_wait;
job->cmd->time_proc += job->time_proc;
}
SAFE_DELETE(job);
int len = 0;
if(result == PROC_ERROR){
log_info("[%s] fd: %d, proc error, maybe network is unavailable, delete link", link->get_basic_string(), link->fd());
goto proc_err;
}
//try to write for one time
//if there is no writing buffer, 0 will be returned
len = link->write();
if(len < 0){
log_warn("[%s] fd: %d, write: %d, delete link", link->get_basic_string(), link->fd(), len);
goto proc_err;
}
if(link->response.head.bodylen + sizeof(head_t) > link->writed){
//need more writing operation
fdes->set(link->fd(), FDEVENT_OUT, 1, link);
}else{
if(!long_connect || 0 != link->response.head.status){
//TODO: for short connection
link_count--;
fdes->del(link->fd());
log_frame("[%s] short connection, close [ fd: %d ]", link->get_basic_string(), link->fd());
SAFE_DELETE(link);
}else{
//TODO: long connection£¬ continue to read data
link->readed = link->writed = 0;
link->request.reset();
link->response.reset();
link->reset_basic_string();
fdes->set(link->fd(), FDEVENT_IN, 1, link);
log_frame("[%s] long connection, try to receive request next time [ fd: %d ]", link->get_basic_string(), link->fd());
}
}
return PROC_OK;
proc_err:
this->link_count --;
fdes->del(link->fd());
SAFE_DELETE(link);
return PROC_ERROR;
}
示例12: proc_result
int proc_result(ProcJob &job, Fdevents &select, ready_list_t &ready_list){
Link *link = job.link;
if(job.result == PROC_ERROR){
log_info("fd: %d, proc error, delete link", link->fd());
select.del(link->fd());
delete link;
return PROC_ERROR;
}
if(job.cmd){
job.cmd->calls += 1;
job.cmd->time_wait += job.time_wait;
job.cmd->time_proc += job.time_proc;
}
if(!link->output->empty()){
//log_trace("add %d to select.out", link->fd());
select.set(link->fd(), FDEVENT_OUT, 1, link);
if(select.isset(link->fd(), FDEVENT_IN)){
//log_trace("delete %d from select.in", link->fd());
select.clr(link->fd(), FDEVENT_IN);
}
}else{
if(link->input->empty()){
if(!select.isset(link->fd(), FDEVENT_IN)){
//log_trace("add %d to select.in", link->fd());
select.set(link->fd(), FDEVENT_IN, 1, link);
}
}else{
if(select.isset(link->fd(), FDEVENT_IN)){
//log_trace("delete %d from select.in", link->fd());
select.clr(link->fd(), FDEVENT_IN);
}
ready_list.push_back(link);
}
}
return PROC_OK;
}
示例13: proc_result
int NetworkServer::proc_result(ProcJob *job, ready_list_t *ready_list){
Link *link = job->link;
int len;
if(job->cmd){
job->cmd->calls += 1;
job->cmd->time_wait += job->time_wait;
job->cmd->time_proc += job->time_proc;
double total_time = job->time_wait + job->time_proc;
if (total_time >= this->slow_time) {
slowlog_warn("proc slow, remote_ip: %s wait: %.3f proc: %.3f total: %.3f req: %s",
link->remote_ip, job->time_wait, job->time_proc, total_time,
serialize_req(*link->last_recv()).c_str());
}
}
if(job->result == PROC_ERROR){
log_info("fd: %d, proc error, delete link", link->fd());
goto proc_err;
}
if(link->error()){
log_debug("fd: %d, link error, delete link", link->fd());
goto proc_err;
}
len = link->write();
//log_debug("write: %d", len);
if(len < 0){
log_debug("fd: %d, write: %d, delete link", link->fd(), len);
goto proc_err;
}
this->bytes_written += len;
if(!link->output->empty()){
fdes->set(link->fd(), FDEVENT_OUT, 1, link);
}
if(link->input->empty()){
fdes->set(link->fd(), FDEVENT_IN, 1, link);
link->unRef();
log_debug("proc_result remote_ip: %s ref: %d", link->remote_ip, link->ref_count);
}else{
fdes->clr(link->fd(), FDEVENT_IN);
ready_list->push_back(link);
}
return PROC_OK;
proc_err:
destroy_link(link);
return PROC_ERROR;
}
示例14: proc_result
int NetworkServer::proc_result(ProcJob *job, link_dict_t *ready_dict){
Link *link = job->link;
int len;
if(job->cmd){
total_calls ++;
job->cmd->calls ++;
job->cmd->time_wait += job->time_wait;
job->cmd->time_proc += job->time_proc;
}
if(job->result == PROC_ERROR){
log_info("fd: %d, proc error, delete link", link->fd());
goto proc_err;
}
len = link->write();
//log_debug("write: %d", len);
if(len < 0){
log_debug("fd: %d, write: %d, delete link", link->fd(), len);
goto proc_err;
}
if(!link->output->empty()){
fdes->set(link->fd(), FDEVENT_OUT, 1, link);
}
if(link->input->empty()){
fdes->set(link->fd(), FDEVENT_IN, 1, link);
}else{
fdes->clr(link->fd(), FDEVENT_IN);
ready_dict->insert(std::make_pair(link->fd(), link));
}
return PROC_OK;
proc_err:
this->link_count --;
fdes->del(link->fd());
delete link;
return PROC_ERROR;
}
示例15: _run_thread
void* BackendDump::_run_thread(void *arg){
struct run_arg *p = (struct run_arg*)arg;
const BackendDump *backend = p->backend;
Link *link = (Link *)p->link;
delete p;
//
link->noblock(false);
const std::vector<Bytes>* req = link->last_recv();
std::string start = "";
if(req->size() > 1){
Bytes b = req->at(1);
start.assign(b.data(), b.size());
}
std::string end = "";
if(req->size() > 2){
Bytes b = req->at(2);
end.assign(b.data(), b.size());
}
int limit = 10;
if(req->size() > 3){
Bytes b = req->at(3);
limit = b.Int();
}
Buffer *output = link->output;
int count = 0;
bool quit = false;
Iterator *it = backend->ssdb->iterator(start, end, limit);
link->send("begin");
while(!quit){
if(!it->next()){
quit = true;
char buf[20];
snprintf(buf, sizeof(buf), "%d", count);
link->send("end", buf);
}else{
count ++;
Bytes key = it->key();
Bytes val = it->val();
output->append_record("set");
output->append_record(key);
output->append_record(val);
output->append('\n');
if(output->size() < output->total()/2){
continue;
}
}
if(link->flush() == -1){
log_info("fd: %d, send error", link->fd());
break;
}
}
log_info("fd: %d, delete link", link->fd());
delete link;
return (void *)NULL;
}