Belle II Software development
TCPServerSocket Class Reference
Inheritance diagram for TCPServerSocket:
FileDescriptor

Public Member Functions

 TCPServerSocket (const std::string &ip, unsigned short port)
 
void setIP (const std::string &ip)
 
void setPort (unsigned short port)
 
const std::string & getIP () const
 
unsigned short getPort () const
 
int open (int nqueue=5)
 
int open (const std::string &ip, unsigned short port, int nqueue=5)
 
TCPSocket accept ()
 
int get_fd () const
 
bool select (int sec=-1, int usec=-1)
 
bool select2 (int sec=-1, int usec=-1)
 
bool close ()
 

Protected Attributes

int m_fd
 

Private Attributes

std::string m_ip
 
unsigned short m_port
 

Detailed Description

Definition at line 20 of file TCPServerSocket.h.

Constructor & Destructor Documentation

◆ TCPServerSocket() [1/2]

TCPServerSocket ( )
inline

Definition at line 23 of file TCPServerSocket.h.

24 : m_ip(""), m_port(0) {}

◆ TCPServerSocket() [2/2]

TCPServerSocket ( const std::string &  ip,
unsigned short  port 
)
inline

Definition at line 25 of file TCPServerSocket.h.

26 : m_ip(ip), m_port(port) {}

◆ ~TCPServerSocket()

virtual ~TCPServerSocket ( )
inlinevirtual

Definition at line 27 of file TCPServerSocket.h.

27{}

Member Function Documentation

◆ accept()

TCPSocket accept ( )

Definition at line 78 of file TCPServerSocket.cc.

79{
80 socklen_t len = sizeof(sockaddr_in);
81 sockaddr_in addr;
82 memset(&addr, 0, sizeof(sockaddr_in));
83 addr.sin_family = AF_INET;
84 addr.sin_addr.s_addr = INADDR_ANY;
85 addr.sin_port = htons(m_port);
86 int fd;
87 errno = 0;
88 while (true) {
89 if ((fd = ::accept(m_fd, (sockaddr*) & (addr), &len)) == -1) {
90 switch (errno) {
91 case EINTR: continue;
92 case EAGAIN: continue;
93 default:
94 perror("accept");
95 throw (IOException("Fail to accept."));
96 }
97 }
98 break;
99 }
100 TCPSocket s(fd);
101 s.m_ip = inet_ntoa(addr.sin_addr);
102 s.m_port = ntohs(addr.sin_port);
103 return s;
104}

◆ close()

bool close ( )
inherited

Definition at line 90 of file FileDescriptor.cc.

91{
92 if (m_fd > 0) {
93 if (::close(m_fd) != 0) {
94 return false;
95 }
96 }
97 m_fd = -1;
98 return true;
99}

◆ get_fd()

int get_fd ( ) const
inherited

Definition at line 33 of file FileDescriptor.cc.

34{
35 return m_fd;
36}

◆ getIP()

const std::string & getIP ( ) const
inline

Definition at line 32 of file TCPServerSocket.h.

32{ return m_ip; }

◆ getPort()

unsigned short getPort ( ) const
inline

Definition at line 33 of file TCPServerSocket.h.

33{ return m_port; }

◆ open() [1/2]

int open ( const std::string &  ip,
unsigned short  port,
int  nqueue = 5 
)

Definition at line 24 of file TCPServerSocket.cc.

26{
27 m_ip = ip;
28 m_port = port;
29 return open(nqueue);
30}

◆ open() [2/2]

int open ( int  nqueue = 5)

Definition at line 32 of file TCPServerSocket.cc.

33{
34 if (m_fd > 0) {
35 throw (IOException("Socket is working already."));
36 }
37 sockaddr_in addr;
38 memset(&addr, 0, sizeof(sockaddr_in));
39 addr.sin_family = AF_INET;
40 addr.sin_addr.s_addr = INADDR_ANY;
41 addr.sin_port = htons(m_port);
42
43 m_fd = socket(PF_INET, SOCK_STREAM, 0);
44 if (m_fd == -1) {
45 m_fd = 0;
46 throw (IOException("Fail to create a server socket."));
47 }
48 int enable = 1;
49 if (setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)) == -1) {
50 m_fd = 0;
51 throw (IOException("Fail to set resue address for the socket."));
52 }
53 struct hostent* host = NULL;
54 host = gethostbyname(m_ip.c_str());
55 if (host == NULL) {
56 unsigned long ip_address = inet_addr(m_ip.c_str());
57 if ((signed long) ip_address < 0) {
58 throw (std::exception());
59 // throw (IOException("Wrong host name or ip")); // TODO which throw is the correct one?
60 } else {
61 host = gethostbyaddr((char*)&ip_address, sizeof(ip_address), AF_INET);
62 }
63 }
64 if (host == NULL) {
65 throw (IOException("Fail to get host ip: %s", m_ip.c_str()));
66 }
67 addr.sin_addr.s_addr = (*(unsigned long*)host->h_addr_list[0]);
68
69 if (bind(m_fd, (const sockaddr*) & (addr), sizeof(sockaddr_in)) != 0) {
70 throw (IOException("Fail to bind the socket. %s:%d", m_ip.c_str(), m_port));
71 }
72 if (listen(m_fd, nqueue) != 0) {
73 throw (IOException("Fail to listen to the socket."));
74 }
75 return m_fd;
76}

◆ select()

bool select ( int  sec = -1,
int  usec = -1 
)
inherited

Definition at line 38 of file FileDescriptor.cc.

39{
40 if (m_fd <= 0) {
41 return false;
42 }
43 fd_set fds;
44 FD_ZERO(&fds);
45 FD_SET(m_fd, &fds);
46 int ret;
47 if (sec >= 0 && usec >= 0) {
48 timeval t = {sec, usec};
49 ret = ::select(FD_SETSIZE, &fds, NULL, NULL, &t);
50 } else {
51 ret = ::select(FD_SETSIZE, &fds, NULL, NULL, NULL);
52 }
53 if (ret < 0) {
54 perror("select");
55 throw (IOException("Failed to select"));
56 }
57 if (FD_ISSET(m_fd, &fds)) {
58 return true;
59 } else {
60 return false;
61 }
62}

◆ select2()

bool select2 ( int  sec = -1,
int  usec = -1 
)
inherited

Definition at line 64 of file FileDescriptor.cc.

65{
66 if (m_fd <= 0) {
67 return false;
68 }
69 fd_set fds;
70 FD_ZERO(&fds);
71 FD_SET(m_fd, &fds);
72 int ret;
73 if (sec >= 0 && usec >= 0) {
74 timeval t = {sec, usec};
75 ret = ::select(FD_SETSIZE, NULL, &fds, NULL, &t);
76 } else {
77 ret = ::select(FD_SETSIZE, NULL, &fds, NULL, NULL);
78 }
79 if (ret < 0) {
80 perror("select");
81 throw (IOException("Failed to select"));
82 }
83 if (FD_ISSET(m_fd, &fds)) {
84 return true;
85 } else {
86 return false;
87 }
88}

◆ setIP()

void setIP ( const std::string &  ip)
inline

Definition at line 30 of file TCPServerSocket.h.

30{ m_ip = ip; }

◆ setPort()

void setPort ( unsigned short  port)
inline

Definition at line 31 of file TCPServerSocket.h.

31{ m_port = port; }

Member Data Documentation

◆ m_fd

int m_fd
protectedinherited

Definition at line 31 of file FileDescriptor.h.

◆ m_ip

std::string m_ip
private

Definition at line 39 of file TCPServerSocket.h.

◆ m_port

unsigned short m_port
private

Definition at line 40 of file TCPServerSocket.h.


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