mirror of https://github.com/agdsn/xerxes.git
playing arround with error handling
This commit is contained in:
parent
4095408c58
commit
95daea3155
17
epoll.cxx
17
epoll.cxx
|
|
@ -14,7 +14,8 @@ namespace xerxes
|
||||||
{
|
{
|
||||||
if(fd < 0)
|
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[source.fd]->data.fd = target.fd;
|
||||||
events[target.fd]->data.fd = source.fd;
|
events[target.fd]->data.fd = source.fd;
|
||||||
|
|
||||||
epoll_ctl(fd, EPOLL_CTL_ADD, source.fd, events[source.fd].get());
|
if(0 != epoll_ctl(fd, EPOLL_CTL_ADD, source.fd, events[source.fd].get()))
|
||||||
epoll_ctl(fd, EPOLL_CTL_ADD, target.fd, events[target.fd].get());
|
throw EpollAddErr();
|
||||||
|
if(0 != epoll_ctl(fd, EPOLL_CTL_ADD, target.fd, events[target.fd].get()))
|
||||||
|
throw EpollAddErr();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
@ -49,14 +52,16 @@ namespace xerxes
|
||||||
events[socket.fd]->events = EPOLLIN;
|
events[socket.fd]->events = EPOLLIN;
|
||||||
events[socket.fd]->data.fd = -1;
|
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
|
void
|
||||||
EPoll::del(Socket const& socket)
|
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);
|
events.erase(socket.fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
19
epoll.hxx
19
epoll.hxx
|
|
@ -41,5 +41,24 @@ namespace xerxes
|
||||||
int fd;
|
int fd;
|
||||||
std::map<int, event_t> events;
|
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
|
#endif
|
||||||
|
|
|
||||||
40
socket.cxx
40
socket.cxx
|
|
@ -12,7 +12,6 @@
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/un.h>
|
#include <sys/un.h>
|
||||||
#include "socket.hxx"
|
#include "socket.hxx"
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
namespace xerxes
|
namespace xerxes
|
||||||
{
|
{
|
||||||
|
|
@ -24,8 +23,7 @@ namespace xerxes
|
||||||
std::cerr << "new socket" << std::endl;
|
std::cerr << "new socket" << std::endl;
|
||||||
if(fd < 0)
|
if(fd < 0)
|
||||||
{
|
{
|
||||||
perror("--");
|
throw ConnCreateErr();
|
||||||
throw std::runtime_error("could not create socket.");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -34,8 +32,7 @@ namespace xerxes
|
||||||
fd = new_fd;
|
fd = new_fd;
|
||||||
if(fd < 0)
|
if(fd < 0)
|
||||||
{
|
{
|
||||||
perror("--");
|
throw ConnCreateErr();
|
||||||
throw std::runtime_error("could not create socket.");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -110,7 +107,12 @@ namespace xerxes
|
||||||
int
|
int
|
||||||
listen(Socket& socket, int backlog)
|
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>
|
boost::shared_ptr<Socket>
|
||||||
|
|
@ -121,7 +123,7 @@ namespace xerxes
|
||||||
int new_fd = ::accept(socket.fd, address, address_len);
|
int new_fd = ::accept(socket.fd, address, address_len);
|
||||||
if(new_fd == -1)
|
if(new_fd == -1)
|
||||||
{
|
{
|
||||||
throw std::runtime_error("could not accept socket.");
|
throw ConnAcceptErr();
|
||||||
}
|
}
|
||||||
|
|
||||||
return boost::shared_ptr<Socket>(new Socket(new_fd));
|
return boost::shared_ptr<Socket>(new Socket(new_fd));
|
||||||
|
|
@ -132,7 +134,12 @@ namespace xerxes
|
||||||
sockaddr const* const serv_addr,
|
sockaddr const* const serv_addr,
|
||||||
socklen_t addrlen)
|
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
|
int
|
||||||
|
|
@ -190,7 +197,7 @@ namespace xerxes
|
||||||
int flags)
|
int flags)
|
||||||
{
|
{
|
||||||
int ret = ::send(socket.fd, data.first.get(), len, flags);
|
int ret = ::send(socket.fd, data.first.get(), len, flags);
|
||||||
if(ret == 0)
|
if(ret == 0 && len != 0)
|
||||||
{
|
{
|
||||||
throw ConResetErr();
|
throw ConResetErr();
|
||||||
}
|
}
|
||||||
|
|
@ -206,7 +213,12 @@ namespace xerxes
|
||||||
sockaddr const* const bind_address,
|
sockaddr const* const bind_address,
|
||||||
socklen_t addrlen)
|
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
|
int
|
||||||
|
|
@ -245,4 +257,12 @@ namespace xerxes
|
||||||
strncpy(adr.sun_path, opt.file.c_str(), sizeof(adr.sun_path));
|
strncpy(adr.sun_path, opt.file.c_str(), sizeof(adr.sun_path));
|
||||||
return bind(socket, (struct sockaddr *) &adr, SUN_LEN(&adr));
|
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:");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
44
socket.hxx
44
socket.hxx
|
|
@ -21,6 +21,7 @@
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/epoll.h>
|
#include <sys/epoll.h>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
namespace xerxes
|
namespace xerxes
|
||||||
{
|
{
|
||||||
|
|
@ -114,9 +115,46 @@ namespace xerxes
|
||||||
bind_unix(Socket& socket,
|
bind_unix(Socket& socket,
|
||||||
SocketOption& opt);
|
SocketOption& opt);
|
||||||
|
|
||||||
class SocketErr{};
|
class SocketErr{
|
||||||
class ConResetErr: public SocketErr{};
|
public:
|
||||||
class ConDataErr: public SocketErr{};
|
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
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
|
#include <signal.h>
|
||||||
#include "socket.hxx"
|
#include "socket.hxx"
|
||||||
#include "epoll.hxx"
|
#include "epoll.hxx"
|
||||||
#include <boost/utility.hpp>
|
#include <boost/utility.hpp>
|
||||||
|
|
@ -34,6 +35,7 @@ main(int argc, char* argv[])
|
||||||
<< endl;
|
<< endl;
|
||||||
|
|
||||||
|
|
||||||
|
signal(SIGPIPE, SIG_IGN);
|
||||||
|
|
||||||
// Declare the supported options.
|
// Declare the supported options.
|
||||||
po::options_description desc("Allowed options");
|
po::options_description desc("Allowed options");
|
||||||
|
|
@ -77,7 +79,7 @@ main(int argc, char* argv[])
|
||||||
}
|
}
|
||||||
|
|
||||||
SocketOption src = vm["src"].as<SocketOption>();
|
SocketOption src = vm["src"].as<SocketOption>();
|
||||||
Socket &lstn = *(src.gen_socket());
|
Socket &lstn = *(src.gen_socket());
|
||||||
if(src.type == TCP)
|
if(src.type == TCP)
|
||||||
{
|
{
|
||||||
cout << "TCP Source is " << src.hostname << "," << src.port << ".\n";
|
cout << "TCP Source is " << src.hostname << "," << src.port << ".\n";
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue