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"
47 /// set options like fast reuse and keepalive every socket should have
48 void socket_handler::set_socket_options(int sock)
52 /* fast reuse enable */
53 if (setsockopt(sock,SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)) < 0)
55 string err="error setting socket option: ";
58 throw t2n_communication_error(err);
61 /* keepalive enable */
62 if (setsockopt(sock,SOL_SOCKET, SO_KEEPALIVE, &i, sizeof(i)) < 0)
64 string err="error setting socket option: ";
67 throw t2n_communication_error(err);
72 fdflags=fcntl(sock,F_GETFD, 0);
75 string err="fcntl error on socket: ";
78 throw t2n_communication_error(err);
80 fdflags |= FD_CLOEXEC;
81 if (fcntl(sock,F_SETFD,fdflags) < 0)
83 string err="fcntl error on socket: ";
86 throw t2n_communication_error(err);
89 /* non-blocking mode */
91 flflags=fcntl(sock,F_GETFL,0);
94 string err="fcntl error on socket: ";
97 throw t2n_communication_error(err);
99 flflags |= O_NONBLOCK;
100 if (fcntl(sock,F_SETFL,flflags) < 0)
102 string err="fcntl error on socket: ";
103 err+=strerror(errno);
105 throw t2n_communication_error(err);
109 /// close the underlying socket connection. Don't call directly, use the version provided
110 /// by the connection class you are using.
111 void socket_handler::close()
114 shutdown(sock,SHUT_RDWR);
118 /// is the underlying socket connection still open?
119 bool socket_handler::is_closed()
121 int r=fcntl(sock,F_GETFL);
123 return !(r & O_ACCMODE);
126 /** @brief check if new data is waiting on the raw socket
127 @param usec_timeout wait until new data is found, max timeout usecs.
131 bool socket_handler::data_waiting(long long usec_timeout)
134 fd_set active_fd_set;
135 FD_ZERO (&active_fd_set);
136 FD_SET (sock, &active_fd_set);
140 struct timeval *timeout_ptr;
142 if (usec_timeout == -1)
148 // timeout von long long usec in int sec + int usec umrechnen
149 tval.tv_sec = usec_timeout / 1000000;
150 tval.tv_usec = usec_timeout % 1000000;
153 if(select (FD_SETSIZE, &active_fd_set, NULL, NULL, timeout_ptr) > 0)
159 /** @brief read data from the raw socket and copy it into the provided buffer
160 @param buffer the buffer where to append the new data
161 @param usec_timeout wait until new data is found, max timeout usecs.
165 bool socket_handler::fill_buffer(std::string& buffer, long long usec_timeout)
167 // fast path for timeout==0
168 if (usec_timeout==0 || data_waiting(usec_timeout))
169 return fill_buffer(buffer);
174 /** @brief read data from the raw socket and copy it into the provided buffer. Returns
175 instantly if no data is waiting.
176 @param buffer the buffer where to append the new data
178 bool socket_handler::fill_buffer(std::string& buffer)
180 bool try_again=false;
182 char socket_buffer[recv_buffer_size];
184 int nbytes = read (sock, socket_buffer, recv_buffer_size);
188 return false; // no data was waiting
189 else if (errno == EINTR)
191 // interrupted, try again
196 log(error,string("error reading from socket : ")+strerror(errno));
203 if (nbytes == 0 && !try_again)
209 // Data read -> store it
211 buffer.assign(socket_buffer,nbytes);
213 // more data waiting -> recurse
223 /// writes raw data to the socket. Don't use directly, use the write() function provided by the
224 /// connection because it encapsulates the data.
225 void socket_handler::socket_write(const std::string& data)
228 while (offset < data.size())
230 unsigned int write_size=write_block_size;
232 if (offset+write_size > data.size())
233 write_size = data.size()-offset;
236 while ((rtn=::write(sock, data.data()+offset, write_size)) &&
237 rtn == -1 && (errno == EAGAIN || errno == EINTR))
240 log(debug,"resuming write() call after EAGAIN or EINTR");
245 log(error,string("write() returned ")+strerror(errno));
249 else if (rtn != write_size)
252 msg << "write() wrote " << rtn << " bytes, should have been "
253 << write_size << " (complete: " << data.size() << ")";
255 log(error,msg.str());
261 offset += write_size;