debug support added

This commit is contained in:
Jan Losinshi 2008-08-26 17:51:51 +02:00
parent e79f72cafd
commit ef46b68dd6
3 changed files with 157 additions and 30 deletions

View File

@ -7,6 +7,7 @@
*/ */
#include "epoll.hxx" #include "epoll.hxx"
#include "xerxes.hxx"
namespace xerxes namespace xerxes
{ {
@ -15,7 +16,10 @@ namespace xerxes
if(fd < 0) if(fd < 0)
{ {
throw EpollCreateErr(); throw EpollCreateErr();
//throw std::runtime_error("could not create epoll descriptor."); }
if(be_debug)
{
std::cout << "epoll fd created" << std::endl;
} }
} }
@ -25,6 +29,10 @@ namespace xerxes
{ {
close(fd); close(fd);
} }
if(be_debug)
{
std::cout << "epoll closed" << std::endl;
}
} }
void void
@ -43,6 +51,10 @@ namespace xerxes
throw EpollAddErr(EPOLL_ADD_ERR_SOURCE); throw EpollAddErr(EPOLL_ADD_ERR_SOURCE);
if(0 != epoll_ctl(fd, EPOLL_CTL_ADD, target.fd, events[target.fd].get())) if(0 != epoll_ctl(fd, EPOLL_CTL_ADD, target.fd, events[target.fd].get()))
throw EpollAddErr(EPOLL_ADD_ERR_TARGET); throw EpollAddErr(EPOLL_ADD_ERR_TARGET);
if(be_debug)
{
std::cout << "fd " << target.fd << " and fd " << source.fd << " added to epoll" << std::endl;
}
} }
void void
@ -54,6 +66,10 @@ namespace xerxes
if(0 != 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(); throw EpollAddErr();
if(be_debug)
{
std::cout << "fd " << socket.fd << " added to epoll" << std::endl;
}
} }
void void
@ -62,6 +78,10 @@ namespace xerxes
if(0 != epoll_ctl(fd, EPOLL_CTL_DEL, socket.fd, 0)) if(0 != epoll_ctl(fd, EPOLL_CTL_DEL, socket.fd, 0))
throw EpollAddErr(); throw EpollAddErr();
events.erase(socket.fd); events.erase(socket.fd);
if(be_debug)
{
std::cout << "fd " << socket.fd << " deleted from epoll" << std::endl;
}
} }
EpollErr::EpollErr() EpollErr::EpollErr()
@ -70,10 +90,13 @@ namespace xerxes
} }
EpollErr::EpollErr(std::string err) EpollErr::EpollErr(std::string err)
{
if(!be_quiet)
{ {
std::cerr << "Epoll Error: " << err << std::endl; std::cerr << "Epoll Error: " << err << std::endl;
perror("ERRNO"); perror("ERRNO");
} }
}
EpollAddErr::EpollAddErr() EpollAddErr::EpollAddErr()
{ {
EpollAddErr(ADD_ERR_SINGLE); EpollAddErr(ADD_ERR_SINGLE);

View File

@ -12,6 +12,7 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/un.h> #include <sys/un.h>
#include "socket.hxx" #include "socket.hxx"
#include "xerxes.hxx"
namespace xerxes namespace xerxes
{ {
@ -24,6 +25,10 @@ namespace xerxes
{ {
throw ConnCreateErr(); throw ConnCreateErr();
} }
if(be_debug)
{
std::cout << "socket with fd " << fd << " created" << std::endl;
}
} }
Socket::Socket(int new_fd) Socket::Socket(int new_fd)
@ -40,6 +45,10 @@ namespace xerxes
if(fd) if(fd)
{ {
close(fd); close(fd);
if(be_debug)
{
std::cout << "socket with fd " << fd << " closed" << std::endl;
}
} }
} }
@ -100,6 +109,10 @@ namespace xerxes
MysqlData MysqlData
makeData(int len) makeData(int len)
{ {
if(be_debug)
{
std::cout << "Init transfer Buffer" << std::endl;
}
return MysqlData(buffer_t(new char[len]), len); return MysqlData(buffer_t(new char[len]), len);
} }
@ -111,6 +124,10 @@ namespace xerxes
{ {
throw ConnListenErr(); throw ConnListenErr();
} }
if(be_debug)
{
std::cout << "listen on socket" << std::endl;
}
return ret; return ret;
} }
@ -124,6 +141,10 @@ namespace xerxes
{ {
throw ConnAcceptErr(); throw ConnAcceptErr();
} }
if(be_debug)
{
std::cout << "connection accepted" << std::endl;
}
return boost::shared_ptr<Socket>(new Socket(new_fd)); return boost::shared_ptr<Socket>(new Socket(new_fd));
} }
@ -138,6 +159,10 @@ namespace xerxes
{ {
throw ConnConnectErr(); throw ConnConnectErr();
} }
if(be_debug)
{
std::cout << "socket connected" << std::endl;
}
return ret; return ret;
} }
@ -145,6 +170,10 @@ namespace xerxes
connect_inet(Socket& socket, connect_inet(Socket& socket,
SocketOption& opt) SocketOption& opt)
{ {
if(be_debug)
{
std::cout << "connect inet to: " << opt.hostname << ":" << opt.port << std::endl;
}
addrinfo hints; addrinfo hints;
addrinfo* res; addrinfo* res;
@ -164,8 +193,11 @@ namespace xerxes
connect_unix(Socket& socket, connect_unix(Socket& socket,
SocketOption& opt) SocketOption& opt)
{ {
if(be_debug)
{
std::cout << "connect unix to: " << opt.file << std::endl;
}
struct sockaddr_un adr; struct sockaddr_un adr;
memset(&adr, 0, sizeof(adr)); memset(&adr, 0, sizeof(adr));
adr.sun_family = AF_UNIX; adr.sun_family = AF_UNIX;
strncpy(adr.sun_path, opt.file.c_str(), sizeof(adr.sun_path)); strncpy(adr.sun_path, opt.file.c_str(), sizeof(adr.sun_path));
@ -186,6 +218,11 @@ namespace xerxes
{ {
throw ConDataErr(); throw ConDataErr();
} }
if(be_debug)
{
std::cout << "recived " << ret << " bytes from fd " << socket.fd << std::endl;
}
return ret; return ret;
} }
@ -196,7 +233,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 && len != 0) if((ret == 0 && len != 0) || len != ret)
{ {
throw ConResetErr(); throw ConResetErr();
} }
@ -204,6 +241,11 @@ namespace xerxes
{ {
throw ConDataErr(); throw ConDataErr();
} }
if(be_debug)
{
std::cout << "sended " << ret << " of " << len << " bytes to " << socket.fd << std::endl;
}
return ret; return ret;
} }
@ -217,6 +259,12 @@ namespace xerxes
{ {
throw ConnBindErr(); throw ConnBindErr();
} }
if(be_debug)
{
std::cout << "socket bound" << std::endl;
}
return ret; return ret;
} }
@ -224,6 +272,10 @@ namespace xerxes
bind_inet(Socket& socket, bind_inet(Socket& socket,
SocketOption& opt) SocketOption& opt)
{ {
if(be_debug)
{
std::cout << "bind inet socket to: " << opt.hostname << ":" << opt.port << std::endl;
}
addrinfo hints; addrinfo hints;
addrinfo* res; addrinfo* res;
@ -250,10 +302,14 @@ namespace xerxes
bind_unix(Socket& socket, bind_unix(Socket& socket,
SocketOption& opt) SocketOption& opt)
{ {
if(be_debug)
{
std::cout << "bind unix socket to: " << opt.file << std::endl;
}
unlink(opt.file.c_str()); unlink(opt.file.c_str());
struct sockaddr_un adr; struct sockaddr_un adr;
memset(&adr, 0, sizeof(adr)); memset(&adr, 0, sizeof(adr));
adr.sun_family = AF_UNIX; adr.sun_family = AF_UNIX;
strncpy(adr.sun_path, opt.file.c_str(), sizeof(adr.sun_path)); strncpy(adr.sun_path, opt.file.c_str(), sizeof(adr.sun_path));
@ -264,7 +320,10 @@ namespace xerxes
SocketErr("unknown"); SocketErr("unknown");
} }
SocketErr::SocketErr(std::string err){ SocketErr::SocketErr(std::string err){
if(!be_quiet)
{
std::cerr << "Socket Exception: " << err << std::endl; std::cerr << "Socket Exception: " << err << std::endl;
perror("ERRNO"); perror("ERRNO");
} }
} }
}

View File

@ -7,8 +7,7 @@
*/ */
/** /**
* TODO : Unix Socket testing * TODO : Closing of dockets does not work korrect
* TODO : Do some Error handling
*/ */
#include <iostream> #include <iostream>
@ -22,7 +21,8 @@
#include <vector> #include <vector>
int be_quiet = 0;
int be_debug = 0;
int int
main(int argc, char* argv[]) main(int argc, char* argv[])
@ -36,6 +36,8 @@ main(int argc, char* argv[])
po::options_description desc("Allowed options"); po::options_description desc("Allowed options");
desc.add_options() desc.add_options()
("help", "produce help message") ("help", "produce help message")
("quiet", "be quiet")
("debug", "write a lot of stupid debug messages")
("src", po::value<SocketOption>(), "Source") ("src", po::value<SocketOption>(), "Source")
("dst", po::value<SocketOption>(), "Destination") ("dst", po::value<SocketOption>(), "Destination")
; ;
@ -50,6 +52,15 @@ main(int argc, char* argv[])
return 1; return 1;
} }
if (vm.count("quiet"))
{
be_quiet = 1;
}
if (vm.count("debug"))
{
be_debug = 1;
}
if (vm.count("src")) if (vm.count("src"))
{ {
@ -59,6 +70,8 @@ main(int argc, char* argv[])
if (vm.count("dst")) if (vm.count("dst"))
{ {
SocketOption sock = vm["dst"].as<SocketOption>(); SocketOption sock = vm["dst"].as<SocketOption>();
if(!be_quiet)
{
if(sock.type == TCP) if(sock.type == TCP)
{ {
cout << "TCP Destination is " << sock.hostname << "," << sock.port << ".\n"; cout << "TCP Destination is " << sock.hostname << "," << sock.port << ".\n";
@ -68,6 +81,7 @@ main(int argc, char* argv[])
cout << "UNIX Destination is " << sock.file << ".\n"; cout << "UNIX Destination is " << sock.file << ".\n";
} }
} }
}
else else
{ {
cout << "Destination was not set.\n"; cout << "Destination was not set.\n";
@ -78,13 +92,19 @@ main(int argc, char* argv[])
Socket &lstn = *(src.gen_socket()); Socket &lstn = *(src.gen_socket());
if(src.type == TCP) if(src.type == TCP)
{
if(!be_quiet)
{ {
cout << "TCP Source is " << src.hostname << "," << src.port << ".\n"; cout << "TCP Source is " << src.hostname << "," << src.port << ".\n";
}
bind_inet(lstn, src); bind_inet(lstn, src);
} }
else else
{
if(!be_quiet)
{ {
cout << "UNIX Source is " << src.file << ".\n"; cout << "UNIX Source is " << src.file << ".\n";
}
bind_unix(lstn, src); bind_unix(lstn, src);
} }
@ -103,11 +123,24 @@ main(int argc, char* argv[])
{ {
int num = epoll_wait(epoll.fd, events.get(), max_events, -1); int num = epoll_wait(epoll.fd, events.get(), max_events, -1);
if(be_debug)
{
cout << "epoll events available (" << num << ")" << endl;
}
for(int i = 0; i < num; ++i) for(int i = 0; i < num; ++i)
{ {
if(be_debug)
{
cout << "process event " << i << endl;
}
if((events[i].data.fd == -1) if((events[i].data.fd == -1)
&& (events[i].events & EPOLLIN)) && (events[i].events & EPOLLIN))
{ {
if(be_debug)
{
cout << "EPOLLIN on listening socket .. create a new connection" << endl;
}
boost::shared_ptr<Socket> target; boost::shared_ptr<Socket> target;
boost::shared_ptr<Socket> source; boost::shared_ptr<Socket> source;
try try
@ -156,23 +189,32 @@ main(int argc, char* argv[])
sockets.erase(source->fd); sockets.erase(source->fd);
continue; continue;
} }
if(be_debug)
{
cout << "new connection created" << endl;
}
} }
else else
{ {
if(sockets[events[i].data.fd] == 0) if(sockets[events[i].data.fd] == 0)
{ {
// cerr << "already closed, ignore" << endl; if(be_debug)
{
cout << "fd " << events[i].data.fd << " already closed, ignore event " << i << endl;
}
continue; continue;
} }
//lookup
boost::shared_ptr<Socket> target(sockets[events[i].data.fd]); boost::shared_ptr<Socket> target(sockets[events[i].data.fd]);
boost::shared_ptr<Socket> source(sockets[epoll.events[target->fd]->data.fd]); boost::shared_ptr<Socket> source(sockets[epoll.events[target->fd]->data.fd]);
if((events[i].events & EPOLLIN ) if((events[i].events & EPOLLIN )
|| (events[i].events & EPOLLPRI)) || (events[i].events & EPOLLPRI))
{ {
// read -> write if(be_debug)
cout << "writer: "<< source->fd << endl; {
cout << "reader: "<< target->fd << endl; cout << "EPOLLIN or EPOLLPRI event from fd " << source->fd << " target is fd " << target->fd << endl;
}
try try
{ {
int len = recv(*source, buffer, 0); int len = recv(*source, buffer, 0);
@ -181,21 +223,24 @@ main(int argc, char* argv[])
} }
catch (SocketErr e) catch (SocketErr e)
{ {
// hangup if(be_debug)
// cout << "Socket Error, closing " << target->fd << " and " << source->fd << endl; {
cout << "Socket Error, closing socket" << target->fd << " and " << source->fd << endl;
}
epoll.del(target->fd); epoll.del(target->fd);
epoll.del(source->fd); epoll.del(source->fd);
sockets.erase(target->fd); sockets.erase(target->fd);
sockets.erase(source->fd); sockets.erase(source->fd);
// cerr << "closed" << endl;
continue; continue;
} }
} }
if((events[i].events & EPOLLERR) if((events[i].events & EPOLLERR)
|| (events[i].events & EPOLLHUP)) || (events[i].events & EPOLLHUP))
{ {
// hangup if(be_debug)
// cout << target->fd << " closed by " << source->fd << endl; {
cout << "EPOLLERR or EPOLLHUP event from fd " << source->fd << " target is fd " << target->fd << " close both" << endl;
}
epoll.del(target->fd); epoll.del(target->fd);
epoll.del(source->fd); epoll.del(source->fd);
sockets.erase(target->fd); sockets.erase(target->fd);