libtasks Documentation  1.6
net_io_task.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2014 ADTECH GmbH
3  * Licensed under MIT (https://github.com/adtechlabs/libtasks/blob/master/COPYING)
4  *
5  * Author: Andreas Pohl
6  */
7 
8 #include <tasks/worker.h>
9 #include <tasks/net_io_task.h>
10 #include <tasks/logging.h>
11 
12 namespace tasks {
13 
14 net_io_task::net_io_task(net::socket& socket, int events) : net_io_task(events) {
15  tdbg(get_string() << ": ctor" << std::endl);
16  m_socket = socket;
17  if (-1 != m_socket.fd()) {
18  init_watcher();
19  }
20 }
21 
23  tdbg(get_string() << ": dtor" << std::endl);
24  // NOTE: The watcher will be stoped by dispose().
25  if (m_auto_close) {
26  tdbg(get_string() << ": closing socket" << std::endl);
27  m_socket.close();
28  }
29 }
30 
32  worker->exec_in_worker_ctx([worker, task](struct ev_loop* loop) {
33  tdbg(task->get_string() << ": adding net_io_task" << std::endl);
34  task->init_watcher();
35  task->assign_worker(worker);
36  ev_io_start(loop, task->watcher());
37  });
38 }
39 
41 
42 } // tasks
static std::shared_ptr< dispatcher > instance()
Definition: dispatcher.h:75
The base class for any task.
Definition: task.h:19
static void add_task(net_io_task *task)
Definition: net_io_task.cpp:40
virtual void close()
Definition: io_base.h:24
The net_io_task implements the base for socket based network tasks.
Definition: net_io_task.h:21
#define tdbg(m)
Definition: logging.h:54
virtual void init_watcher()
Initialize the watcher.
int fd() const
Definition: io_base.h:22
The socket class.
Definition: socket.h:35
net::socket m_socket
Definition: net_io_task.h:66
std::string get_string() const
A debug helper.
Definition: net_io_task.h:28
void assign_worker(worker *worker)
Definition: event_task.cpp:13
net_io_task(int events)
Definition: net_io_task.h:23
net::socket & socket()
Provide access to the underlying socket object.
Definition: net_io_task.h:35
bool exec_in_worker_ctx(task_func_t f)
Definition: worker.h:89
virtual ~net_io_task()
Definition: net_io_task.cpp:22
ev_io * watcher() const
Return the io watcher object.
Definition: io_task_base.h:40