playing arround with error handling

This commit is contained in:
Jan Losinshi 2008-08-26 12:55:17 +02:00
parent 4095408c58
commit 95daea3155
5 changed files with 104 additions and 20 deletions

View File

@ -14,7 +14,8 @@ namespace xerxes
{
if(fd < 0)
{
throw std::runtime_error("could not create epoll descriptor.");
throw EpollCreateErr();
//throw std::runtime_error("could not create epoll descriptor.");
}
}
@ -38,8 +39,10 @@ namespace xerxes
events[source.fd]->data.fd = target.fd;
events[target.fd]->data.fd = source.fd;
epoll_ctl(fd, EPOLL_CTL_ADD, source.fd, events[source.fd].get());
epoll_ctl(fd, EPOLL_CTL_ADD, target.fd, events[target.fd].get());
if(0 != epoll_ctl(fd, EPOLL_CTL_ADD, source.fd, events[source.fd].get()))
throw EpollAddErr();
if(0 != epoll_ctl(fd, EPOLL_CTL_ADD, target.fd, events[target.fd].get()))
throw EpollAddErr();
}
void
@ -49,14 +52,16 @@ namespace xerxes
events[socket.fd]->events = EPOLLIN;
events[socket.fd]->data.fd = -1;
epoll_ctl(fd, EPOLL_CTL_ADD, socket.fd, events[socket.fd].get());
if(0 != epoll_ctl(fd, EPOLL_CTL_ADD, socket.fd, events[socket.fd].get()))
throw EpollAddErr();
}
void
EPoll::del(Socket const& socket)
{
epoll_ctl(fd, EPOLL_CTL_DEL, socket.fd, 0);
if(0 != epoll_ctl(fd, EPOLL_CTL_DEL, socket.fd, 0))
throw EpollAddErr();
events.erase(socket.fd);
}
}

View File

@ -41,5 +41,24 @@ namespace xerxes
int fd;
std::map<int, event_t> events;
};
enum epoll_add_err_type{
ADD_ERR_SINGLE,
ADD_ERR_SOURCE,
ADD_ERR_TARGET
};
class EpollErr{};
class EpollAddErr : public EpollErr {
public:
EpollAddErr(){
EpollAddErr(ADD_ERR_SINGLE);
};
EpollAddErr(epoll_add_err_type t)
:type(t){};
epoll_add_err_type type;
};
class EpollDelErr : public EpollErr {};
class EpollCreateErr : public EpollErr {};
}
#endif

View File

@ -12,7 +12,6 @@
#include <sys/socket.h>
#include <sys/un.h>
#include "socket.hxx"
#include <iostream>
namespace xerxes
{
@ -24,8 +23,7 @@ namespace xerxes
std::cerr << "new socket" << std::endl;
if(fd < 0)
{
perror("--");
throw std::runtime_error("could not create socket.");
throw ConnCreateErr();
}
}
@ -34,8 +32,7 @@ namespace xerxes
fd = new_fd;
if(fd < 0)
{
perror("--");
throw std::runtime_error("could not create socket.");
throw ConnCreateErr();
}
}
@ -110,7 +107,12 @@ namespace xerxes
int
listen(Socket& socket, int backlog)
{
return ::listen(socket.fd, backlog);
int ret = ::listen(socket.fd, backlog);
if (ret != 0)
{
throw ConnListenErr();
}
return ret;
}
boost::shared_ptr<Socket>
@ -121,7 +123,7 @@ namespace xerxes
int new_fd = ::accept(socket.fd, address, address_len);
if(new_fd == -1)
{
throw std::runtime_error("could not accept socket.");
throw ConnAcceptErr();
}
return boost::shared_ptr<Socket>(new Socket(new_fd));
@ -132,7 +134,12 @@ namespace xerxes
sockaddr const* const serv_addr,
socklen_t addrlen)
{
return ::connect(socket.fd, serv_addr, addrlen);
int ret = ::connect(socket.fd, serv_addr, addrlen);
if (ret != 0)
{
throw ConnConnectErr();
}
return ret;
}
int
@ -190,7 +197,7 @@ namespace xerxes
int flags)
{
int ret = ::send(socket.fd, data.first.get(), len, flags);
if(ret == 0)
if(ret == 0 && len != 0)
{
throw ConResetErr();
}
@ -206,7 +213,12 @@ namespace xerxes
sockaddr const* const bind_address,
socklen_t addrlen)
{
return ::bind(socket.fd, bind_address, addrlen);
int ret = ::bind(socket.fd, bind_address, addrlen);
if(ret != 0)
{
throw ConnBindErr();
}
return ret;
}
int
@ -245,4 +257,12 @@ namespace xerxes
strncpy(adr.sun_path, opt.file.c_str(), sizeof(adr.sun_path));
return bind(socket, (struct sockaddr *) &adr, SUN_LEN(&adr));
}
SocketErr::SocketErr(){
SocketErr("unknown");
}
SocketErr::SocketErr(std::string err){
std::cerr << "Socket Exception: " << err << std::endl;
perror("ERROR:");
}
}

View File

@ -21,6 +21,7 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <iostream>
namespace xerxes
{
@ -114,9 +115,46 @@ namespace xerxes
bind_unix(Socket& socket,
SocketOption& opt);
class SocketErr{};
class ConResetErr: public SocketErr{};
class ConDataErr: public SocketErr{};
class SocketErr{
public:
SocketErr(std::string err);
SocketErr();
};
class ConResetErr: public SocketErr
{
public:
ConResetErr() :SocketErr("Connection resetted."){};
};
class ConDataErr: public SocketErr
{
public:
ConDataErr() :SocketErr("could send/recv Data."){};
};
class ConnCreateErr: public SocketErr
{
public:
ConnCreateErr() :SocketErr("could not create socket."){};
};
class ConnListenErr: public SocketErr
{
public:
ConnListenErr() :SocketErr("could not listen on socket."){};
};
class ConnAcceptErr: public SocketErr
{
public:
ConnAcceptErr() :SocketErr("could not accept on socket."){};
};
class ConnConnectErr: public SocketErr
{
public:
ConnConnectErr() :SocketErr("could not connect to socket."){};
};
class ConnBindErr: public SocketErr
{
public:
ConnBindErr() :SocketErr("could not bind on socket."){};
};
}
#endif

View File

@ -15,6 +15,7 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <signal.h>
#include "socket.hxx"
#include "epoll.hxx"
#include <boost/utility.hpp>
@ -34,6 +35,7 @@ main(int argc, char* argv[])
<< endl;
signal(SIGPIPE, SIG_IGN);
// Declare the supported options.
po::options_description desc("Allowed options");
@ -77,7 +79,7 @@ main(int argc, char* argv[])
}
SocketOption src = vm["src"].as<SocketOption>();
Socket &lstn = *(src.gen_socket());
Socket &lstn = *(src.gen_socket());
if(src.type == TCP)
{
cout << "TCP Source is " << src.hostname << "," << src.port << ".\n";