delete i->second;
}
+int server::add_connection(connection* newconn)
+{
+ connections[next_id]=newconn;
+ return next_id++;
+}
+
/**
Gets a connection by id
next_id=0;
}
+ int add_connection(connection* newconn);
+
public:
virtual ~server();
void set_default_timeout(int _default_timeout)
{ default_timeout=_default_timeout; }
+ int get_default_timeout(void)
+ { return default_timeout; }
void set_logging(std::ostream *_logstream, log_level_values _log_level)
{
void socket_server::new_connection()
{
+ struct sockaddr_un clientname;
+ unsigned int size = sizeof (clientname);
+ int newsock = accept (sock,(struct sockaddr *) &clientname,&size);
+ if (newsock < 0)
+ {
+ if (errno == EAGAIN)
+ {
+ log(error, "accept error (EAGAIN): no connection waiting");
+ return;
+ }
+
+ /* default: break */
+ string err="error accepting connection: ";
+ err+=strerror(errno);
+ log(error, err);
+ throw t2n_server_error(err);
+ }
+
+ FD_SET (newsock, &connection_set);
+
+ int i=1;
+
+ /* keepalive enable */
+ if (setsockopt(newsock,SOL_SOCKET, SO_KEEPALIVE, &i, sizeof(i)) < 0)
+ {
+ string err="error setting socket option: ";
+ err+=strerror(errno);
+ log(error, err);
+ throw t2n_server_error(err);
+ }
+
+ /* close on exec */
+ int fdflags;
+ fdflags=fcntl(newsock,F_GETFD, 0);
+ if (fdflags < 0)
+ {
+ string err="fcntl error on socket: ";
+ err+=strerror(errno);
+ log(error, err);
+ throw t2n_server_error(err);
+ }
+ fdflags |= FD_CLOEXEC;
+ if (fcntl(newsock,F_SETFD,fdflags) < 0)
+ {
+ string err="fcntl error on socket: ";
+ err+=strerror(errno);
+ log(error, err);
+ throw t2n_server_error(err);
+ }
+
+ /* non-blocking mode */
+ int flflags;
+ flflags=fcntl(newsock,F_GETFL,0);
+ if (flflags < 0)
+ {
+ string err="fcntl error on socket: ";
+ err+=strerror(errno);
+ log(error, err);
+ throw t2n_server_error(err);
+ }
+ flflags |= O_NONBLOCK;
+ if (fcntl(newsock,F_SETFL,flflags) < 0)
+ {
+ string err="fcntl error on socket: ";
+ err+=strerror(errno);
+ log(error, err);
+ throw t2n_server_error(err);
+ }
+
+ add_connection(new socket_connection(newsock, get_default_timeout()));
+
+ return;
}
void socket_server::fill_buffer(long long usec_timeout)
return;
}
-socket_connection::socket_connection(int _socket, int _timeout)
+socket_connection::socket_connection(int _sock, int _timeout)
: connection(_timeout)
{
-
+ sock=_sock;
}
void socket_connection::close()
*/
class socket_connection : public connection
{
+ friend class socket_server;
+
private:
- int socket;
+ int sock;
- friend void socket_server::fill_buffer(long long usec_timeout);
- socket_connection(int _socket, int _timeout);
+ socket_connection(int _sock, int _timeout);
public:
namespace libt2n
{
-/**
- generic libt2n exception
-*/
-// a generic exception that can be handeled with libt2n
+/// a generic exception that can be handeled with libt2n
class t2n_exception : public std::exception
{
private:
};
BOOST_CLASS_EXPORT(t2n_exception)
-// a (unspecified) problem with libt2n communication
+/// a (unspecified) problem with libt2n communication
class t2n_communication_error : public t2n_exception
{
private:
};
BOOST_CLASS_EXPORT(t2n_communication_error)
-// can't connect to libt2n server
+/// can't connect to libt2n server
class t2n_connect_error : public t2n_communication_error
{
private:
};
BOOST_CLASS_EXPORT(t2n_connect_error)
-// error establishing a socket on the server (only thrown on the server-side)
+/// error establishing a socket on the server (only thrown on the server-side)
class t2n_server_error : public t2n_communication_error
{
private:
};
BOOST_CLASS_EXPORT(t2n_server_error)
-// error transmitting or receiving libt2n messages
+/// error transmitting or receiving libt2n messages
class t2n_transfer_error : public t2n_communication_error
{
private:
};
BOOST_CLASS_EXPORT(t2n_transfer_error)
-// tried to talk to an incompatible libt2n version
+/// tried to talk to an incompatible libt2n version
class t2n_version_mismatch : public t2n_communication_error
{
private:
};
BOOST_CLASS_EXPORT(t2n_version_mismatch)
-// illegal libt2n command received
+/// illegal libt2n command received
class t2n_command_error : public t2n_exception
{
private:
};
BOOST_CLASS_EXPORT(t2n_command_error)
-// error serializing or deserializing a libt2n command packet
+/// error serializing or deserializing a libt2n command packet
class t2n_serialization_error : public t2n_exception
{
private:
};
BOOST_CLASS_EXPORT(t2n_serialization_error)
-// a runtime error within the remote function
-// derive your own custom exceptions from this one
+/**
+ a runtime error within the remote function
+ derive your own custom exceptions from this one
+*/
class t2n_runtime_error : public t2n_exception
{
private: