Repository navigation
Expand file tree
/
Copy paththreadedacceptor.cpp
More file actions
140 lines (98 loc) · 3.84 KB
/
Copy paththreadedacceptor.cpp
File metadata and controls
140 lines (98 loc) · 3.84 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
Socle Library Ecosystem
Copyright (c) 2014, Ales Stibal <astib@mag0.net>, All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
#ifndef _THREADED_ACCEPTOR_CPP_
#define _THREADED_ACCEPTOR_CPP_
#include <fcntl.h>
#include <unistd.h>
#include <vector>
#include <thread>
#include <display.hpp>
#include <threadedacceptor.hpp>
#include <log/logger.hpp>
template<class Worker>
ThreadedAcceptor<Worker>::ThreadedAcceptor (std::shared_ptr<FdQueue> fdq, baseCom *c, proxyType t):
baseProxy(c),
FdQueueHandler(fdq),
hasWorkers<Worker>(fdq),
proxy_type_(t) {
baseProxy::new_raw(true);
}
template<class Worker>
ThreadedAcceptor<Worker>::~ThreadedAcceptor() {}
template<class Worker>
void ThreadedAcceptor<Worker>::on_left_new_raw(int s) {
_dia("ThreadedAcceptor::on_left_new: connection [%d] pushed to the queue",s);
hint_push_all(s);
}
template<class Worker>
void ThreadedAcceptor<Worker>::on_right_new_raw(int s) {
_dia("ThreadedAcceptor::on_right_new: connection [%d] pushed to the queue",s);
hint_push_all(s);
}
template<class Worker>
int ThreadedAcceptor<Worker>::run() {
pollroot(true);
hasWorkers<Worker>::create_workers(0, com(), proxy_type());
for( unsigned int i = 0; i < this->tasks().size() ; i++) {
auto& thread_worker = this->tasks()[i];
thread_worker.second->com()->nonlocal_dst(com()->nonlocal_dst());
thread_worker.second->pollroot(true);
thread_worker.second->parent(this);
auto* ptr = new std::thread(&Worker::run, thread_worker.second.get());
_dia("ThreadedAcceptor::run: started new thread[%d]: ptr=%x, thread_id=%d",i,ptr,ptr->get_id());
thread_worker.first.reset(ptr);
}
baseProxy::run();
return this->tasks().size();
}
template<class SubWorker>
int ThreadedAcceptorProxy<SubWorker>::handle_sockets_once(baseCom* xcom) {
if(parent() == nullptr) {
_fat("PARENT is NULL");
}
if (this->retiring() || parent()->state().dead()) {
// Keep servicing owned sessions, but never dequeue another accepted fd.
if (proxies().empty()) this->state().dead(true);
return MasterProxy::handle_sockets_once(com());
}
int s = 0;
if(auto parent_fd_handler = parent_as_handler.cast(parent()); parent_fd_handler) {
parent_fd_handler->update_load(worker_id_, proxies().size());
s = parent_fd_handler->pop(worker_id_);
}
if (s > 0) {
_dia("ThreadedAcceptorProxy::run: removed from queue: 0x%016llx (socket %d)", s, s);
try {
auto cx = std::unique_ptr<baseHostCX>(this->new_cx(s));
if (!cx->read_waiting_for_peercom()) {
cx->on_accept_socket(s);
} else {
cx->on_delay_socket(s);
}
cx->com()->nonlocal_dst(this->com()->nonlocal_dst());
if (proxy_type().is_transparent()) {
cx->com()->resolve_nonlocal_dst_socket(s);
} else
if (proxy_type().is_redirect()) {
cx->com()->resolve_redirected_dst_socket(s);
}
this->on_left_new(std::move(cx));
} catch (socle::com_error const& e) {
_err("cannot handover cx to proxy: %s", e.what());
}
}
return MasterProxy::handle_sockets_once(com());
}
#endif