5 #ifndef CSOCKET_PLAIN_H
6 #define CSOCKET_PLAIN_H
12 #include <netinet/in.h>
15 #include <netinet/tcp.h>
16 #include <sys/ioctl.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
24 #include "rofl/common/csocket.h"
25 #include "rofl/common/ctimerid.h"
26 #include "rofl/common/caddrinfos.h"
27 #include "rofl/common/openflow/openflow_common.h"
59 size_t msg_bytes_sent;
61 mem(mem), dest(dest), msg_bytes_sent(0) {};
63 mem(0), msg_bytes_sent(0) {
68 if (
this == &e)
return *
this;
71 msg_bytes_sent = e.msg_bytes_sent;
75 operator<< (std::ostream& os,
struct pout_entry_t const& entry) {
76 os <<
indent(0) <<
"<struct pout_entry_t >" << std::endl;
77 os <<
indent(2) <<
"<mem:0x" << entry.mem <<
" >" << std::endl;
78 os <<
indent(2) <<
"<dest:" << entry.dest <<
" >" << std::endl;
79 os <<
indent(2) <<
"<msg_bytes_sent:" << entry.msg_bytes_sent <<
" >" << std::endl;
89 static bool const PARAM_DEFAULT_VALUE_DO_RECONNECT;
90 static std::string
const PARAM_DEFAULT_VALUE_REMOTE_HOSTNAME;
91 static std::string
const PARAM_DEFAULT_VALUE_REMOTE_PORT;
92 static std::string
const PARAM_DEFAULT_VALUE_LOCAL_HOSTNAME;
93 static std::string
const PARAM_DEFAULT_VALUE_LOCAL_PORT;
94 static std::string
const PARAM_DEFAULT_VALUE_DOMAIN;
95 static std::string
const PARAM_DEFAULT_VALUE_TYPE;
96 static std::string
const PARAM_DEFAULT_VALUE_PROTOCOL;
105 FLAG_ACTIVE_SOCKET = 5,
106 FLAG_DO_RECONNECT = 6,
108 FLAG_TX_WOULD_BLOCK = 8,
109 FLAG_TX_WOULD_BLOCK_NOTIFIED = 9,
112 std::bitset<16> sockflags;
114 enum csocket_plain_timer_t {
118 enum csocket_plain_event_t {
119 EVENT_CONN_RESET = 1,
120 EVENT_DISCONNECTED = 2,
123 static const unsigned int DEFAULT_MAX_TXQUEUE_SIZE;
124 unsigned int max_txqueue_size;
126 ctimerid reconnect_timerid;
127 int reconnect_start_timeout;
128 int reconnect_in_seconds;
129 int reconnect_counter;
131 #define RECONNECT_START_TIMEOUT 1 // start reconnect timeout (default 1s)
163 cparams
const& params);
183 cparams
const& params);
215 void *buf,
size_t count) {
217 return recv(buf, count, 0, from);
250 is_established()
const {
return sockflags.test(FLAG_CONNECTED); };
256 write_would_block()
const {
return sockflags.test(FLAG_TX_WOULD_BLOCK); };
263 get_default_params();
287 int type = SOCK_STREAM,
290 std::string devname = std::string(
""));
310 csockaddr la = csockaddr(),
312 int type = SOCK_STREAM,
314 bool do_reconnect =
false);
462 bool reset_timeout =
false);
475 int opaque,
void *data = (
void*)0);
495 handle_revent(
int fd);
507 handle_wevent(
int fd);
515 handle_xevent(
int fd);
534 os << dynamic_cast<csocket const&>( sock );
537 if (sock.sockflags.test(FLAG_LISTENING)) {
540 if (sock.sockflags.test(FLAG_CONNECTING)) {
543 if (sock.sockflags.test(FLAG_RAW_SOCKET)) {
546 if (sock.sockflags.test(FLAG_CONNECTED)) {
549 if (sock.sockflags.test(FLAG_ACTIVE_SOCKET)) {
550 os <<
"ACTIVE-SOCKET ";
552 if (sock.sockflags.test(FLAG_DO_RECONNECT)) {
553 os <<
"DO-RECONNECT ";
555 if (sock.sockflags.test(FLAG_CLOSING)) {
558 if (sock.sockflags.test(FLAG_TX_WOULD_BLOCK)) {
559 os <<
"TX-WOULD-BLOCK ";
561 os <<
">" << std::endl;
567 std::stringstream sstr;
568 sstr <<
"sd:" <<
sd <<
" local:" <<
laddr.str() <<
" remote:" <<
raddr.str()
569 <<
" #tx-queue:" <<
pout_squeue.size() <<
" flags: ";
570 if (sockflags.test(FLAG_LISTENING)) {
571 sstr <<
"LISTENING, ";
573 if (sockflags.test(FLAG_CONNECTING)) {
574 sstr <<
"CONNECTING, ";
576 if (sockflags.test(FLAG_RAW_SOCKET)) {
577 sstr <<
"RAW-SOCKET, ";
579 if (sockflags.test(FLAG_CONNECTED)) {
580 sstr <<
"CONNECTED, ";
582 if (sockflags.test(FLAG_ACTIVE_SOCKET)) {
583 sstr <<
"ACTIVE-SOCKET, ";
585 if (sockflags.test(FLAG_DO_RECONNECT)) {
586 sstr <<
"DO-RECONNECT, ";
588 if (sockflags.test(FLAG_CLOSING)) {
591 if (sockflags.test(FLAG_TX_WOULD_BLOCK)) {
592 sstr <<
"TX-WOULD-BLOCK, ";
csocket_plain(csocket_env *owner)
Constructor for new empty csocket_plain instances.
Definition: csocket_plain.cc:45
int type
Definition: csocket.h:485
virtual void handle_accept_refused(rofl::csocket &socket)=0
Called once accepting a request from a remote entity failed.
virtual void handle_read()
Definition: csocket_plain.h:431
virtual void handle_accept_refused()
Definition: csocket_plain.h:345
virtual void close()
Closes this socket.
Definition: csocket_plain.cc:926
virtual void reconnect()
Reconnect this socket.
Definition: csocket_plain.cc:910
virtual void handle_accepted(rofl::csocket &socket)=0
Called once this csocket entity has succeeded its accept() method.
std::list< pout_entry_t > pout_squeue
Definition: csocket_plain.h:86
A single socket.
Definition: csocket.h:182
virtual ssize_t recv(void *buf, size_t count)
Reads bytes from socket.
Definition: csocket_plain.h:214
Definition: csockaddr.h:38
virtual void handle_write()
Definition: csocket_plain.h:446
virtual void listen(cparams const ¶ms)
Open socket in listening mode (server side).
Definition: csocket_plain.cc:275
virtual void handle_conn_refused()
Definition: csocket_plain.h:373
int backlog
Definition: csocket.h:487
A single unencrypted socket.This class provides basic support for socket based communication. Its aim is to encapsulate functionality for establishing a socket in active and passive mode. For using a socket, the owning class must implement the interface defined in csocket_plain_owner.
Definition: csocket_plain.h:51
virtual ~csocket_plain()
Destructor.
Definition: csocket_plain.cc:62
virtual void handle_read(rofl::csocket &socket)=0
Called once new data is available for reading from the socket.
pthread_rwlock_t pout_squeue_lock
Definition: csocket_plain.h:85
Definition: csocket_plain.h:56
int domain
Definition: csocket.h:484
virtual void dequeue_packet()
Definition: csocket_plain.cc:1081
cparams socket_params
Definition: csocket.h:488
virtual void handle_conn_failed()
Definition: csocket_plain.h:387
virtual void handle_closed(rofl::csocket &socket)=0
Called once the socket has been shutdown and closed.
C++ abstraction for malloc'ed memory areas.
Definition: cmemory.h:44
virtual void handle_listen(rofl::csocket &socket, int newsd)=0
Called once a listening socket has accepted a connection request from a remote peer entity...
virtual void handle_connect_refused(rofl::csocket &socket)=0
Called once a connection request to a remote entity failed.
Single event used internally by class crofl::cioloop.
Definition: cevent.h:20
csocket_env * socket_env
Definition: csocket.h:475
virtual void handle_listen(int newsd)
Definition: csocket_plain.h:403
virtual void send(cmemory *mem, rofl::csockaddr const &dest=rofl::csockaddr())
Store a packet for transmission.
Definition: csocket_plain.cc:1038
int sd
Definition: csocket.h:481
virtual void accept(cparams const &socket_params, int sd)
Handle accepted socket descriptor obtained from external listening socket.
Definition: csocket_plain.cc:523
virtual void handle_accepted()
Definition: csocket_plain.h:331
virtual void handle_connected(rofl::csocket &socket)=0
Called once a connection request has succeeded its connect() method.
virtual void handle_connected()
Definition: csocket_plain.h:359
virtual void handle_connect_failed(rofl::csocket &socket)=0
Called once a connection request to a remote entity failed.
csockaddr raddr
Definition: csocket.h:483
virtual void handle_closed()
Definition: csocket_plain.h:416
csockaddr laddr
Definition: csocket.h:482
int protocol
Definition: csocket.h:486
virtual void handle_write(rofl::csocket &socket)=0
Called once the socket accept additional data for sending.
virtual void connect(cparams const ¶ms)
Open socket and connect to peer entity (client side).
Definition: csocket_plain.cc:588