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

View File

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

View File

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