File: evasocket.cc

package info (click to toggle)
apt-cacher-ng 3.3.1-2~bpo10+1
  • links: PTS, VCS
  • area: main
  • in suites: buster-backports
  • size: 2,040 kB
  • sloc: cpp: 17,564; sh: 553; ansic: 401; perl: 377; makefile: 126
file content (99 lines) | stat: -rw-r--r-- 2,037 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * evasocket.cc
 *
 *  Created on: 15.03.2019
 *      Author: EB
 */

#include "evasocket.h"
#include "evabase.h"

#include "fileio.h"
#include <event2/event.h>
#include <memory>

acng::evasocket::evasocket(int fd) : m_fd(fd)
{
}

acng::evasocket::~evasocket()
{
	checkforceclose(m_fd);
}

std::shared_ptr<acng::evasocket> acng::evasocket::create(int fd)
{
	return std::shared_ptr<acng::evasocket>(new evasocket(fd));
}

acng::event_socket::event_socket(std::shared_ptr<evabase> ev_base, std::shared_ptr<evasocket> sock,
		short create_flags,
		std::function<void(const std::shared_ptr<evasocket> & sock, short)> action)
:	m_evbase(ev_base), m_sock(sock), m_parent_action(action)
{
	m_event = event_new(ev_base->base, sock->fd(), create_flags, event_socket::on_io, this);
}

acng::event_socket::~event_socket()
{
	if(m_event)
		event_free(m_event);
}

void acng::event_socket::on_io(evutil_socket_t, short what, void* uptr)
{
	auto es = (event_socket*) uptr;
	es->m_parent_action(es->m_sock, what);
}

void acng::event_socket::enable(const struct timeval *tv)
{
	if(m_event)
		event_add(m_event, tv);
}

void acng::event_socket::disable()
{
	if(m_event)
		event_del(m_event);
}
/*
acng::event_socket::event_socket(event_socket&& o)
{
	m_evbase.swap(o.m_evbase);
	m_sock.swap(o.m_sock);
	m_parent_action.swap(o.m_parent_action);
	m_event = o.m_event;
	o.m_event = nullptr;
}
*/

acng::socket_activity_base::socket_activity_base(std::shared_ptr<evabase> evbase)
: m_evbase(evbase)
{
	m_evbase->register_activity(this);
}

acng::socket_activity_base::~socket_activity_base()
{
	m_evbase->unregister_activity(this);
}
/*
std::shared_ptr<acng::socket_activity_base> acng::socket_activity_base::self_lock()
{
	m_selfref = std::shared_from_this(this);
	m_evbase->register_activity(m_selfref);
	return m_selfref;
}

std::shared_ptr<acng::socket_activity_base> acng::socket_activity_base::release_self_lock()
{
	decltype(m_selfref) ret;
	if(m_selfref)
	{
		m_evbase->unregister_activity(m_selfref);
		ret.swap(m_selfref);
	}
	return ret;
}
*/