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 void socket_handler::set_socket_options(int sock)
51 /* fast reuse enable */
52 if (setsockopt(sock,SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)) < 0)
54 string err="error setting socket option: ";
57 throw t2n_communication_error(err);
60 /* keepalive enable */
61 if (setsockopt(sock,SOL_SOCKET, SO_KEEPALIVE, &i, sizeof(i)) < 0)
63 string err="error setting socket option: ";
66 throw t2n_communication_error(err);
71 fdflags=fcntl(sock,F_GETFD, 0);
74 string err="fcntl error on socket: ";
77 throw t2n_communication_error(err);
79 fdflags |= FD_CLOEXEC;
80 if (fcntl(sock,F_SETFD,fdflags) < 0)
82 string err="fcntl error on socket: ";
85 throw t2n_communication_error(err);
88 /* non-blocking mode */
90 flflags=fcntl(sock,F_GETFL,0);
93 string err="fcntl error on socket: ";
96 throw t2n_communication_error(err);
98 flflags |= O_NONBLOCK;
99 if (fcntl(sock,F_SETFL,flflags) < 0)
101 string err="fcntl error on socket: ";
102 err+=strerror(errno);
104 throw t2n_communication_error(err);
108 void socket_handler::close()
111 shutdown(sock,SHUT_RDWR);
115 bool socket_handler::is_closed()
117 int r=fcntl(sock,F_GETFL);
119 return !(r & O_ACCMODE);
122 bool socket_handler::data_waiting(long long usec_timeout)
125 fd_set active_fd_set;
126 FD_ZERO (&active_fd_set);
127 FD_SET (sock, &active_fd_set);
131 struct timeval *timeout_ptr;
133 if (usec_timeout == -1)
139 // timeout von long long usec in int sec + int usec umrechnen
140 tval.tv_sec = usec_timeout / 1000000;
141 tval.tv_usec = usec_timeout % 1000000;
144 if(select (FD_SETSIZE, &active_fd_set, NULL, NULL, timeout_ptr) > 0)
150 bool socket_handler::fill_buffer(std::string& buffer, long long usec_timeout)
152 // fast path for timeout==0
153 if (usec_timeout==0 || data_waiting(usec_timeout))
154 return fill_buffer(buffer);
159 bool socket_handler::fill_buffer(std::string& buffer)
161 bool try_again=false;
163 char socket_buffer[recv_buffer_size];
165 int nbytes = read (sock, socket_buffer, recv_buffer_size);
169 return false; // no data was waiting
170 else if (errno == EINTR)
172 // interrupted, try again
177 log(error,string("error reading from socket : ")+strerror(errno));
184 if (nbytes == 0 && !try_again)
190 // Data read -> store it
192 buffer.assign(socket_buffer,nbytes);
194 // more data waiting -> recurse
204 void socket_handler::socket_write(const std::string& data)
207 while (offset < data.size())
209 unsigned int write_size=write_block_size;
211 if (offset+write_size > data.size())
212 write_size = data.size()-offset;
215 while ((rtn=::write(sock, data.data()+offset, write_size)) &&
216 rtn == -1 && (errno == EAGAIN || errno == EINTR))
219 log(debug,"resuming write() call after EAGAIN or EINTR");
224 log(error,string("write() returned ")+strerror(errno));
228 else if (rtn != write_size)
231 msg << "write() wrote " << rtn << " bytes, should have been "
232 << write_size << " (complete: " << data.size() << ")";
234 log(error,msg.str());
240 offset += write_size;