Belle II Software development
SocketManager Class Reference

Public Member Functions

 SocketManager (int sock)
 
int examine ()
 
std::vector< int > & connected_socket_list ()
 
bool connected (int fd, bool wreq=false)
 
bool remove (int fd)
 

Private Attributes

int m_maxfd
 
std::vector< int > m_fd
 
int m_sock
 
fd_set m_rset
 
fd_set m_wset
 
fd_set m_allset
 

Detailed Description

Definition at line 20 of file SocketManager.h.

Constructor & Destructor Documentation

◆ SocketManager()

SocketManager ( int  sock)

Definition at line 22 of file SocketManager.cc.

23{
24
25 FD_ZERO(&m_allset);
26 FD_SET(sock, &m_allset);
27 m_maxfd = sock;
28 m_sock = sock;
29}

◆ ~SocketManager()

Definition at line 31 of file SocketManager.cc.

32{
33}

Member Function Documentation

◆ connected()

bool connected ( int  fd,
bool  wreq = false 
)

Definition at line 92 of file SocketManager.cc.

93{
94 if (!wreq) {
95 if (FD_ISSET(fd, &m_rset))
96 return true;
97 else
98 return false;
99 } else {
100 if (FD_ISSET(fd, &m_wset))
101 return true;
102 else
103 return false;
104 }
105}

◆ connected_socket_list()

vector< int > & connected_socket_list ( )

Definition at line 87 of file SocketManager.cc.

88{
89 return m_fd;
90}

◆ examine()

int examine ( )

Definition at line 35 of file SocketManager.cc.

36{
37 // sleep ( 1 );
38 memcpy(&m_rset, &m_allset, sizeof(m_rset));
39 memcpy(&m_wset, &m_allset, sizeof(m_wset));
40
41 //timeval timeout;
42 //timeout.tv_sec = 0; // 1sec
43 //timeout.tv_usec = 1000; // 1msec (in microsec)
44 // printf ( "SocketManager: maxfd = %d, start select...; rset=%x, wset=%x\n", m_maxfd, m_rset, m_wset);
45
46 int rc = select(m_maxfd + 1, &m_rset, NULL, NULL, NULL);
47 // int rc = select(m_maxfd + 1, &m_rset, &m_wset, NULL, NULL);
48 // int rc = select(m_maxfd + 1, &m_rset, &m_wset, NULL, &timeout);
49 // printf ( "SocketManager: select returned with %d; rset=%x, wset=%x\n", rc, m_rset, m_wset );
50 /*
51 for ( int i=0;i<m_maxfd+1; i++ ) {
52 printf ( "Checking socket %d ", i );
53 if ( FD_ISSET( i, &m_rset) ) printf ( "Rset is set " );
54 if ( FD_ISSET( i, &m_wset) ) printf ( "Wset is set " );
55 printf ( "\n" );
56 }
57 */
58
59 if (rc < 0)
60 perror("select");
61 else if (rc == 0) // timeout
62 return -1;
63
64 // Accept if new connection request
65 if (FD_ISSET(m_sock, &m_rset)) { // New connection request
66 // int fd = (m_recv->sock())->accept();
67
68 int t;
69 struct sockaddr_in isa;
70 socklen_t i = sizeof(isa);
71 getsockname(m_sock, (struct sockaddr*)&isa, &i);
72 if ((t =::accept(m_sock, (struct sockaddr*)&isa, &i)) < 0) {
73 // m_errno = errno;
74 return (-1);
75 }
76 m_fd.push_back(t);
77 printf("New socket registered t=%d, list size = %lu\n", t, m_fd.size());
78 FD_SET(t, &m_allset);
79 if (t > m_maxfd) m_maxfd = t;
80
81 return 0;
82 } else { //
83 return 1;
84 }
85}

◆ remove()

bool remove ( int  fd)

Definition at line 107 of file SocketManager.cc.

108{
109 FD_CLR(fd, &m_allset);
110 shutdown(fd, SHUT_RDWR);
111 close(fd);
112 vector<int>::iterator pos = find(m_fd.begin(), m_fd.end(), fd);
113 if (pos != m_fd.end()) {
114 m_fd.erase(pos);
115 return true;
116 } else
117 return false;
118}

Member Data Documentation

◆ m_allset

fd_set m_allset
private

Definition at line 40 of file SocketManager.h.

◆ m_fd

std::vector<int> m_fd
private

Definition at line 35 of file SocketManager.h.

◆ m_maxfd

int m_maxfd
private

Definition at line 34 of file SocketManager.h.

◆ m_rset

fd_set m_rset
private

Definition at line 38 of file SocketManager.h.

◆ m_sock

int m_sock
private

Definition at line 37 of file SocketManager.h.

◆ m_wset

fd_set m_wset
private

Definition at line 39 of file SocketManager.h.


The documentation for this class was generated from the following files: