1 /***************************************************************************
2 * Copyright (C) 2006 by Gerd v. Egidy *
5 * This library is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU Lesser General Public License version *
7 * 2.1 as published by the Free Software Foundation. *
9 * This library is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU Lesser General Public License for more details. *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
24 #include <sys/types.h>
25 #include <sys/socket.h>
28 #include <arpa/inet.h>
29 #include <netinet/in.h>
39 #include "socket_handler.hxx"
40 #include "t2n_exception.hxx"
48 /// set options like fast reuse and keepalive every socket should have
49 void socket_handler::set_socket_options(int sock)
53 /* fast reuse enable */
54 if (setsockopt(sock,SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)) < 0)
55 EXCEPTIONSTREAM(error,t2n_communication_error,"error setting socket option: " << strerror(errno));
57 /* keepalive enable */
58 if (setsockopt(sock,SOL_SOCKET, SO_KEEPALIVE, &i, sizeof(i)) < 0)
59 EXCEPTIONSTREAM(error,t2n_communication_error,"error setting socket option: " << strerror(errno));
63 fdflags=fcntl(sock,F_GETFD, 0);
65 EXCEPTIONSTREAM(error,t2n_communication_error,"fcntl error on socket: " << strerror(errno));
67 fdflags |= FD_CLOEXEC;
68 if (fcntl(sock,F_SETFD,fdflags) < 0)
69 EXCEPTIONSTREAM(error,t2n_communication_error,"fcntl error on socket: " << strerror(errno));
71 /* non-blocking mode */
73 flflags=fcntl(sock,F_GETFL,0);
75 EXCEPTIONSTREAM(error,t2n_communication_error,"fcntl error on socket: " << strerror(errno));
77 flflags |= O_NONBLOCK;
78 if (fcntl(sock,F_SETFL,flflags) < 0)
79 EXCEPTIONSTREAM(error,t2n_communication_error,"fcntl error on socket: " << strerror(errno));
82 /// close the underlying socket connection. Don't call directly, use the version provided
83 /// by the connection class you are using.
84 void socket_handler::close()
87 shutdown(sock,SHUT_RDWR);
91 /// is the underlying socket connection still open?
92 bool socket_handler::is_closed()
94 int r=fcntl(sock,F_GETFL);
96 return !(r & O_ACCMODE);
99 /** @brief check if new data is waiting on the raw socket
100 @param usec_timeout wait until new data is found, max timeout usecs.
104 bool socket_handler::data_waiting(long long usec_timeout)
107 fd_set active_fd_set;
108 FD_ZERO (&active_fd_set);
109 FD_SET (sock, &active_fd_set);
113 struct timeval *timeout_ptr;
115 if (usec_timeout == -1)
121 // timeout von long long usec in int sec + int usec umrechnen
122 tval.tv_sec = usec_timeout / 1000000;
123 tval.tv_usec = usec_timeout % 1000000;
126 if(select (FD_SETSIZE, &active_fd_set, NULL, NULL, timeout_ptr) > 0)
132 /** @brief read data from the raw socket and copy it into the provided buffer
133 @param buffer the buffer where to append the new data
134 @param usec_timeout wait until new data is found, max timeout usecs.
138 bool socket_handler::fill_buffer(std::string& buffer, long long usec_timeout)
140 // fast path for timeout==0
141 if (usec_timeout==0 || data_waiting(usec_timeout))
142 return fill_buffer(buffer);
147 /** @brief read data from the raw socket and copy it into the provided buffer. Returns
148 instantly if no data is waiting.
149 @param buffer the buffer where to append the new data
151 bool socket_handler::fill_buffer(std::string& buffer)
153 bool try_again=false;
155 char socket_buffer[recv_buffer_size];
157 int nbytes = read (sock, socket_buffer, recv_buffer_size);
161 return false; // no data was waiting
162 else if (errno == EINTR)
164 // interrupted, try again
165 LOGSTREAM(debug,"EINTR received on read(), trying again");
170 LOGSTREAM(error,"error reading from socket : " << strerror(errno));
177 if (nbytes == 0 && !try_again)
179 LOGSTREAM(debug,"0 bytes received on read(), closing connection");
184 // Data read -> store it
187 buffer.assign(socket_buffer,nbytes);
188 LOGSTREAM(debug,nbytes << " read");
191 // more data waiting -> recurse
201 /// writes raw data to the socket. Don't use directly, use the write() function provided by the
202 /// connection because it encapsulates the data.
203 void socket_handler::socket_write(const std::string& data)
206 while (offset < data.size())
208 unsigned int write_size=write_block_size;
210 if (offset+write_size > data.size())
211 write_size = data.size()-offset;
214 while ((rtn=::write(sock, data.data()+offset, write_size)) &&
215 rtn == -1 && (errno == EAGAIN || errno == EINTR))
218 LOGSTREAM(debug,"resuming write() call after EAGAIN or EINTR");
223 LOGSTREAM(error,"write() returned " << strerror(errno));
227 else if (rtn != write_size)
229 LOGSTREAM(error,"write() wrote " << rtn << " bytes, should have been "
230 << write_size << " (complete: " << data.size() << ")");
236 offset += write_size;
239 LOGSTREAM(debug,"wrote " << data.size() << " bytes");