/************************************************************************* * * HTCd - Copyright (C) 1998-2006 Henrik Rydberg * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #ifndef ISOCKETH #define ISOCKETH #include #include #include #include #include #include class isocket : public pipe_stream { public: isocket(const in_addr& ia,int port,int backlog); isocket(int f,const sockaddr_in& ia); ~isocket() { close(); } void close() { if(fd>=0) { ::close(fd); fd=-1; }} int getfd() const { return fd; } int family() const { return sa.sin_family; } int port() const { return ntohs(sa.sin_port); } in_addr address() const { return sa.sin_addr; } void reuse(int ok) { set(SOL_SOCKET,SO_REUSEADDR,ok); } void nodelay(int ok) { set(IPPROTO_TCP,TCP_NODELAY,ok); } void keepalive(int ok) { set(SOL_SOCKET,SO_KEEPALIVE,ok); } void sendbufsize(int s) { set(SOL_SOCKET,SO_SNDBUF,s); } void recvbufsize(int s) { set(SOL_SOCKET,SO_RCVBUF,s); } isocket* accept() throw(merror_t); int test() { return rd.poll(); } int getsome(char* s,int n) { return rd.read(s,n); } int gethead(char* s,int n) { return rd.readpara(s,n); } int getbody(char* s,int n) { return rd.readx(s,n); } protected: void setflag(int f) { fcntl(fd,F_SETFL,f); } int getflag() const { return fcntl(fd,F_GETFL,0); } void set(int lev,int opt,int ok) throw(merror_t) { if(setsockopt(fd,lev,opt,(char*)&ok,sizeof(ok))<0) THROW("isocket: Could not set option"); } private: sockaddr_in sa; mread rd; }; #endif