Belle II Software development
SocketManager.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <daq/dataflow/SocketManager.h>
10
11#include <netinet/in.h>
12
13#include <algorithm>
14#include <cstdio>
15#include <cstring>
16#include <sys/socket.h>
17#include <unistd.h>
18
19using namespace Belle2;
20using namespace std;
21
22SocketManager::SocketManager(int sock)
23{
24
25 FD_ZERO(&m_allset);
26 FD_SET(sock, &m_allset);
27 m_maxfd = sock;
28 m_sock = sock;
29}
30
31SocketManager::~SocketManager()
32{
33}
34
35int SocketManager::examine()
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}
86
87vector<int>& SocketManager::connected_socket_list()
88{
89 return m_fd;
90}
91
92bool SocketManager::connected(int fd, bool wreq)
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}
106
107bool SocketManager::remove(int fd)
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}
119
120
121
122
123
Abstract base class for different kinds of events.
STL namespace.