1
// Process server for soft ioc
2
// David H. Thompson 8/29/2003
3
// Ralph Lange 04/22/2008
4
// GNU Public License (GPLv3) applies - see www.gnu.org
10
#include <sys/socket.h>
11
#include <netinet/in.h>
13
#include <netinet/in.h>
14
#include <arpa/inet.h>
20
class acceptItem : public connectionItem
23
acceptItem ( int port, bool local, bool readonly );
25
int Send ( const char *, int );
28
virtual ~acceptItem();
31
// service and calls clientFactory when clients are accepted
32
connectionItem * acceptFactory ( int port, bool local, bool readonly )
34
PRINTF("Creating new acceptItem %d\n", port);
35
return new acceptItem(port, local, readonly);
38
acceptItem::~acceptItem()
40
if (_ioHandle>=0) close(_ioHandle);
41
PRINTF("~acceptItem()\n");
45
// Accept item constructor
46
// This opens a socket, binds it to the decided port,
47
// and sets it to listen mode
48
acceptItem::acceptItem ( int port, bool local, bool readonly )
51
struct sockaddr_in addr;
56
_ioHandle = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP );
57
assert( _ioHandle>0 );
59
setsockopt( _ioHandle, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval) );
61
addr.sin_family = AF_INET;
62
addr.sin_port = htons(port);
64
inet_aton( "127.0.0.1", &addr.sin_addr );
66
addr.sin_addr.s_addr = htonl( INADDR_ANY );
68
bindStatus = bind( _ioHandle, (struct sockaddr *) &addr, sizeof(addr) );
71
PRINTF( "Bind: %s\n", strerror(errno) );
76
PRINTF( "Bind returned %d\n", bindStatus );
82
// OnPoll is called after a poll returns non-zero in events
83
bool acceptItem::OnPoll()
87
socklen_t len = sizeof(addr);
89
if ( _pfd==NULL || _pfd->revents==0 ) return false;
91
// Otherwise process the revents and return true
93
if ( _pfd->revents & (POLLIN|POLLPRI) )
95
newSocket = accept( _ioHandle, &addr, &len );
96
PRINTF( "acceptItem: Accepted connection on handle %d\n", newSocket );
97
AddConnection( clientFactory(newSocket, _readonly) );
99
if ( _pfd->revents & (POLLHUP|POLLERR) )
101
PRINTF( "acceptItem: Got hangup or error\n ");
103
if ( _pfd->revents & POLLNVAL )
106
_markedForDeletion = true;
111
// Send characters to client
112
int acceptItem::Send ( const char * buf, int count)
114
// Makes no sense to send to the listening socket