Skip to content

Commit b0c09be

Browse files
rinneintelfx
authored andcommitted
Generalize handling OOB objects in the event loop
This does not add any new functionality, but makes it easier to do so in future. Signed-off-by: Timo J. Rinne <tri@iki.fi> Signed-off-by: Ivan Shapovalov <intelfx@intelfx.name>
1 parent 7207ebc commit b0c09be

6 files changed

Lines changed: 120 additions & 53 deletions

File tree

src/agent/agent.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ ProxyAgent::ProxyAgent( bool is_server, bool dummy )
138138

139139
ProxyAgent::~ProxyAgent( void ) {
140140
#ifdef SUPPORT_AGENT_FORWARDING
141-
shutdown_server();
141+
shutdown();
142142
#endif
143143
}
144144

@@ -154,7 +154,7 @@ void ProxyAgent::close_sessions( void ) {
154154
#endif
155155
}
156156

157-
void ProxyAgent::shutdown_server( void ) {
157+
void ProxyAgent::shutdown( void ) {
158158
#ifdef SUPPORT_AGENT_FORWARDING
159159
detach_oob();
160160
if (ok) {
@@ -176,7 +176,7 @@ void ProxyAgent::attach_oob(OutOfBand *oob_ctl) {
176176
detach_oob();
177177
fatal_assert(oob_ctl != NULL);
178178
oob_ctl_ptr = oob_ctl;
179-
comm = oob_ctl_ptr->init(AGENT_FORWARD_OOB_NAME, Network::OOB_MODE_RELIABLE_DATAGRAM);
179+
comm = oob_ctl_ptr->init(AGENT_FORWARD_OOB_NAME, Network::OOB_MODE_RELIABLE_DATAGRAM, this);
180180
fatal_assert(comm != NULL);
181181
}
182182

src/agent/agent.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ namespace Agent {
7979
friend class ProxyAgent;
8080
};
8181

82-
class ProxyAgent {
82+
class ProxyAgent : public Network::OutOfBandPlugin
83+
{
8384
private:
8485
Network::OutOfBandCommunicator *comm;
8586
Network::OutOfBand *oob_ctl_ptr;
@@ -100,16 +101,19 @@ namespace Agent {
100101
ProxyAgent operator=(const ProxyAgent&); // unimplemented
101102

102103
public:
103-
ProxyAgent( bool is_server, bool dummy = false );
104-
~ProxyAgent( void );
105-
void attach_oob(Network::OutOfBand *oob_ctl);
106-
bool active() { return ok && ((! server) || (l_sock >= 0)); }
107-
std::string listener_path( void ) { if ( ok && server && l_sock >= 0 ) return l_path; return ""; }
104+
// Required by parent class
105+
bool active( void ) { return ok && ((! server) || (l_sock >= 0)); }
108106
void pre_poll( void );
109107
void post_poll( void );
110108
void post_tick( void );
111109
void close_sessions( void );
112-
void shutdown_server( void );
110+
void shutdown( void );
111+
void attach_oob(Network::OutOfBand *oob_ctl);
112+
113+
// Class specific stuff
114+
ProxyAgent( bool is_server, bool dummy = false );
115+
~ProxyAgent( void );
116+
std::string listener_path( void ) { if ( ok && server && l_sock >= 0 ) return l_path; return ""; }
113117

114118
friend class AgentConnection;
115119
};

src/frontend/mosh-server.cc

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -700,9 +700,7 @@ static void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &
700700

701701
bool child_released = false;
702702

703-
if ( agent.active() ) {
704-
agent.attach_oob( network.oob() );
705-
}
703+
agent.attach_oob(network.oob());
706704

707705
while ( true ) {
708706
try {
@@ -742,9 +740,7 @@ static void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &
742740
sel.add_fd( host_fd );
743741
}
744742

745-
if ( agent.active() ) {
746-
agent.pre_poll();
747-
}
743+
network.oob()->pre_poll();
748744

749745
int active_fds = sel.select( timeout );
750746
if ( active_fds < 0 ) {
@@ -871,7 +867,7 @@ static void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &
871867
/* If the pty slave is closed, reading from the master can fail with
872868
EIO (see #264). So we treat errors on read() like EOF. */
873869
if ( bytes_read <= 0 ) {
874-
agent.shutdown_server();
870+
network.oob()->shutdown();
875871
network.start_shutdown();
876872
} else {
877873
terminal_to_host += terminal.act( string( buf, bytes_read ) );
@@ -903,7 +899,7 @@ static void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &
903899
if ( sel.any_signal() || idle_shutdown ) {
904900
/* shutdown signal */
905901
if ( network.has_remote_addr() && (!network.shutdown_in_progress()) ) {
906-
agent.shutdown_server();
902+
network.oob()->shutdown();
907903
network.start_shutdown();
908904
} else {
909905
break;
@@ -948,29 +944,25 @@ static void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &
948944
&& time_since_remote_state >= timeout_if_no_client ) {
949945
fprintf( stderr, "No connection within %llu seconds.\n",
950946
static_cast<unsigned long long>( timeout_if_no_client / 1000 ) );
951-
agent.shutdown_server();
947+
network.oob()->shutdown();
952948
break;
953949
}
954950

955-
if ( agent.active() ) {
956-
if ( time_since_remote_state > (AGENT_IDLE_TIMEOUT * 1000) || time_since_remote_state > 30000 ) {
957-
agent.close_sessions();
958-
}
959-
agent.post_poll();
951+
if ( time_since_remote_state > (AGENT_IDLE_TIMEOUT * 1000) || time_since_remote_state > 30000 ) {
952+
network.oob()->close_sessions();
960953
}
954+
network.oob()->post_poll();
961955

962956
network.tick();
963957

964-
if ( agent.active() ) {
965-
agent.post_tick();
966-
}
958+
network.oob()->post_tick();
967959

968960
} catch ( const Network::NetworkException &e ) {
969961
fprintf( stderr, "%s\n", e.what() );
970962
spin();
971963
} catch ( const Crypto::CryptoException &e ) {
972964
if ( e.fatal ) {
973-
agent.shutdown_server();
965+
network.oob()->shutdown();
974966
throw;
975967
} else {
976968
fprintf( stderr, "Crypto exception: %s\n", e.what() );

src/frontend/stmclient.cc

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -436,9 +436,8 @@ bool STMClient::main( void )
436436
#endif
437437

438438
Agent::ProxyAgent agent( false, ! forward_agent );
439-
if ( agent.active() ) {
440-
agent.attach_oob( network->oob() );
441-
}
439+
440+
agent.attach_oob(network->oob());
442441

443442
/* prepare to poll for events */
444443
Select &sel = Select::get_instance();
@@ -465,9 +464,7 @@ bool STMClient::main( void )
465464
}
466465
sel.add_fd( STDIN_FILENO );
467466

468-
if ( agent.active() ) {
469-
agent.pre_poll();
470-
}
467+
network->oob()->pre_poll();
471468

472469
int active_fds = sel.select( wait_time );
473470
if ( active_fds < 0 ) {
@@ -496,11 +493,11 @@ bool STMClient::main( void )
496493
break;
497494
} else if ( !network->shutdown_in_progress() ) {
498495
overlays.get_notification_engine().set_notification_string( wstring( L"Exiting..." ), true );
499-
agent.shutdown_server();
496+
network->oob()->shutdown();
500497
network->start_shutdown();
501498
} else {
502499
/* XXX: cannot be reached? preserved to avoid non-identity transformation during rebase */
503-
agent.shutdown_server();
500+
network->oob()->shutdown();
504501
}
505502
}
506503

@@ -521,7 +518,7 @@ bool STMClient::main( void )
521518
break;
522519
} else if ( !network->shutdown_in_progress() ) {
523520
overlays.get_notification_engine().set_notification_string( wstring( L"Signal received, shutting down..." ), true );
524-
agent.shutdown_server();
521+
network->oob()->shutdown();
525522
network->start_shutdown();
526523
}
527524
}
@@ -550,7 +547,7 @@ bool STMClient::main( void )
550547
if ( timestamp() - network->get_latest_remote_state().timestamp > 15000 ) {
551548
if ( !network->shutdown_in_progress() ) {
552549
overlays.get_notification_engine().set_notification_string( wstring( L"Timed out waiting for server..." ), true );
553-
agent.shutdown_server();
550+
network->oob()->shutdown();
554551
network->start_shutdown();
555552
}
556553
} else {
@@ -562,15 +559,11 @@ bool STMClient::main( void )
562559
overlays.get_notification_engine().set_notification_string( L"" );
563560
}
564561

565-
if ( agent.active() ) {
566-
agent.post_poll();
567-
}
562+
network->oob()->post_poll();
568563

569564
network->tick();
570565

571-
if ( agent.active() ) {
572-
agent.post_tick();
573-
}
566+
network->oob()->post_tick();
574567

575568
string & send_error = network->get_send_error();
576569
if ( !send_error.empty() ) {

src/network/outofband.cc

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,62 @@ OutOfBand::OutOfBand()
6262
ack_num_out = 0;
6363
}
6464

65-
OutOfBandCommunicator *OutOfBand::init(string name, OutOfBandMode mode) {
65+
OutOfBandCommunicator *OutOfBand::init(string name, OutOfBandMode mode, OutOfBandPlugin *plugin) {
6666
map < string, OutOfBandCommunicator * >::iterator i = comms.find(name);
6767
if (i != comms.end()) {
6868
return NULL;
6969
}
70-
OutOfBandCommunicator *comm = new OutOfBandCommunicator(mode, name, this);
70+
OutOfBandCommunicator *comm = new OutOfBandCommunicator(mode, name, this, plugin);
7171
comms[name] = comm;
7272
return comm;
7373
}
7474

75+
void OutOfBand::pre_poll( void ) {
76+
map < string, OutOfBandCommunicator * >::iterator i = comms.begin();
77+
while (i != comms.end()) {
78+
OutOfBandCommunicator *comm = (i++)->second;
79+
if (comm->plugin_ptr->active()) {
80+
comm->plugin_ptr->pre_poll();
81+
}
82+
}
83+
}
84+
85+
void OutOfBand::post_poll( void ) {
86+
map < string, OutOfBandCommunicator * >::iterator i = comms.begin();
87+
while (i != comms.end()) {
88+
OutOfBandCommunicator *comm = (i++)->second;
89+
if (comm->plugin_ptr->active()) {
90+
comm->plugin_ptr->post_poll();
91+
}
92+
}
93+
}
94+
95+
void OutOfBand::post_tick( void ) {
96+
map < string, OutOfBandCommunicator * >::iterator i = comms.begin();
97+
while (i != comms.end()) {
98+
OutOfBandCommunicator *comm = (i++)->second;
99+
if (comm->plugin_ptr->active()) {
100+
comm->plugin_ptr->post_tick();
101+
}
102+
}
103+
}
104+
105+
void OutOfBand::close_sessions( void ) {
106+
map < string, OutOfBandCommunicator * >::iterator i = comms.begin();
107+
while (i != comms.end()) {
108+
OutOfBandCommunicator *comm = (i++)->second;
109+
comm->plugin_ptr->close_sessions();
110+
}
111+
}
112+
113+
void OutOfBand::shutdown( void ) {
114+
map < string, OutOfBandCommunicator * >::iterator i = comms.begin();
115+
while (i != comms.end()) {
116+
OutOfBandCommunicator *comm = (i++)->second;
117+
comm->plugin_ptr->shutdown();
118+
}
119+
}
120+
75121
void OutOfBand::uninit(string name) {
76122
map < string, OutOfBandCommunicator * >::iterator i = comms.find(name);
77123
if (i == comms.end()) {
@@ -199,11 +245,16 @@ string OutOfBand::output(void) {
199245
return "";
200246
}
201247

202-
OutOfBandCommunicator::OutOfBandCommunicator(OutOfBandMode oob_mode, string oob_name, OutOfBand *oob_ctl) {
203-
mode = oob_mode;
204-
name = oob_name;
205-
oob_ctl_ptr = oob_ctl;
206-
stream_buf = "";
248+
OutOfBandCommunicator::OutOfBandCommunicator( OutOfBandMode oob_mode, string oob_name,
249+
OutOfBand *oob_ctl, OutOfBandPlugin *plugin)
250+
: mode( oob_mode ),
251+
name( oob_name ),
252+
stream_buf( "" ),
253+
datagram_queue(),
254+
plugin_ptr( plugin ),
255+
oob_ctl_ptr( oob_ctl )
256+
{
257+
/* */
207258
}
208259

209260
void OutOfBandCommunicator::send(string data) {

src/network/outofband.h

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ namespace Network {
5454
enum OutOfBandMode { OOB_MODE_STREAM = 1, OOB_MODE_DATAGRAM = 2, OOB_MODE_RELIABLE_DATAGRAM = 3 };
5555

5656
class OutOfBand;
57+
class OutOfBandPlugin;
58+
class OutOfBandCommunicator;
5759

5860
class OutOfBandCommunicator
5961
{
@@ -62,9 +64,12 @@ namespace Network {
6264
string name;
6365
string stream_buf;
6466
queue < string > datagram_queue;
67+
OutOfBandPlugin *plugin_ptr;
6568
OutOfBand *oob_ctl_ptr;
6669
OutOfBand *oob(void) { return oob_ctl_ptr; }
67-
OutOfBandCommunicator(OutOfBandMode oob_mode, string oob_name, OutOfBand *oob_ctl);
70+
OutOfBandCommunicator(OutOfBandMode oob_mode, string oob_name, OutOfBand *oob_ctl, OutOfBandPlugin *plugin);
71+
OutOfBandCommunicator(const OutOfBandCommunicator&);
72+
OutOfBandCommunicator operator=(const OutOfBandCommunicator&);
6873

6974
public:
7075
void send(string data);
@@ -90,7 +95,14 @@ namespace Network {
9095
public:
9196
OutOfBand();
9297
~OutOfBand() { uninit(); }
93-
OutOfBandCommunicator *init(string name, OutOfBandMode mode);
98+
99+
void pre_poll( void );
100+
void post_poll( void );
101+
void post_tick( void );
102+
void close_sessions( void );
103+
void shutdown( void );
104+
105+
OutOfBandCommunicator *init(string name, OutOfBandMode mode, OutOfBandPlugin *plugin);
94106
void uninit(string name);
95107
void uninit(OutOfBandCommunicator *comm);
96108
void uninit(void);
@@ -102,6 +114,21 @@ namespace Network {
102114

103115
friend class OutOfBandCommunicator;
104116
};
117+
118+
class OutOfBandPlugin
119+
{
120+
public:
121+
virtual bool active( void ) = 0;
122+
virtual void pre_poll( void ) = 0;
123+
virtual void post_poll( void ) = 0;
124+
virtual void post_tick( void ) = 0;
125+
virtual void close_sessions( void ) = 0;
126+
virtual void shutdown( void ) = 0;
127+
virtual void attach_oob(Network::OutOfBand *oob_ctl) = 0;
128+
129+
friend class OutOfBand;
130+
};
131+
105132
}
106133

107134
#endif

0 commit comments

Comments
 (0)