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>
38 #include "socket_handler.hxx"
39 #include "t2n_exception.hxx"
46 void socket_handler::set_socket_options(int sock)
50 /* fast reuse enable */
51 if (setsockopt(sock,SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)) < 0)
53 string err="error setting socket option: ";
56 throw t2n_communication_error(err);
59 /* keepalive enable */
60 if (setsockopt(sock,SOL_SOCKET, SO_KEEPALIVE, &i, sizeof(i)) < 0)
62 string err="error setting socket option: ";
65 throw t2n_communication_error(err);
70 fdflags=fcntl(sock,F_GETFD, 0);
73 string err="fcntl error on socket: ";
76 throw t2n_communication_error(err);
78 fdflags |= FD_CLOEXEC;
79 if (fcntl(sock,F_SETFD,fdflags) < 0)
81 string err="fcntl error on socket: ";
84 throw t2n_communication_error(err);
87 /* non-blocking mode */
89 flflags=fcntl(sock,F_GETFL,0);
92 string err="fcntl error on socket: ";
95 throw t2n_communication_error(err);
97 flflags |= O_NONBLOCK;
98 if (fcntl(sock,F_SETFL,flflags) < 0)
100 string err="fcntl error on socket: ";
101 err+=strerror(errno);
103 throw t2n_communication_error(err);
107 void socket_handler::close()
112 bool socket_handler::data_waiting(long long usec_timeout)
115 fd_set active_fd_set;
116 FD_ZERO (&active_fd_set);
117 FD_SET (sock, &active_fd_set);
121 struct timeval *timeout_ptr;
123 if (usec_timeout == -1)
129 // timeout von long long usec in int sec + int usec umrechnen
130 tval.tv_sec = usec_timeout / 1000000;
131 tval.tv_usec = usec_timeout % 1000000;
134 if(select (FD_SETSIZE, &active_fd_set, NULL, NULL, timeout_ptr) > 0)
140 bool socket_handler::fill_buffer(std::string& buffer, long long usec_timeout)
142 // fast path for timeout==-1
143 if (usec_timeout==-1 || data_waiting(usec_timeout))
144 return fill_buffer(buffer);
149 bool socket_handler::fill_buffer(std::string& buffer)
151 bool try_again=false;
153 char socket_buffer[recv_buffer_size];
155 int nbytes = read (sock, socket_buffer, recv_buffer_size);
159 return false; // no data was waiting
160 else if (errno == EINTR)
162 // interrupted, try again
167 log(error,string("error reading from socket : ")+strerror(errno));
174 if (nbytes == 0 && !try_again)
180 // Data read -> store it
182 buffer.assign(socket_buffer,nbytes);
184 // more data waiting -> recurse
194 void socket_handler::write(const std::string& data)
197 while (offset < data.size())
199 unsigned int write_size=write_block_size;
201 if (offset+write_size > data.size())
202 write_size = data.size()-offset;
205 while ((rtn=::write(sock, data.data()+offset, write_size)) &&
206 rtn == -1 && (errno == EAGAIN || errno == EINTR))
209 log(debug,"resuming write() call after EAGAIN or EINTR");
214 log(error,string("write() returned ")+strerror(errno));
218 else if (rtn != write_size)
221 msg << "write() wrote " << rtn << " bytes, should have been "
222 << write_size << " (complete: " << data.size() << ")";
224 log(error,msg.str());
230 offset += write_size;