00001 /*************************************************************************** 00002 * Copyright (C) 2004-2006 by Ilya A. Volynets-Evenbakh * 00003 * ilya@total-knowledge.com * 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU General Public License as published by * 00007 * the Free Software Foundation; either version 2 of the License, or * 00008 * (at your option) any later version. * 00009 * * 00010 * This program is distributed in the hope that it will be useful, * 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00013 * GNU General Public License for more details. * 00014 * * 00015 * You should have received a copy of the GNU General Public License * 00016 * along with this program; if not, write to the * 00017 * Free Software Foundation, Inc., * 00018 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00019 ***************************************************************************/ 00020 #include "connection.h" 00021 #include "requestlistener.h" 00022 #include <sptk4/CGuard.h> 00023 00024 namespace container { 00025 00026 Connection::Connection(int sock) 00027 : sockbuf(sock) 00028 , m_limit(-1) 00029 { 00030 } 00031 00032 00033 Connection::~Connection() 00034 { 00035 try { 00036 overflow(eof); 00037 } catch (...) { 00038 } 00039 try { 00040 shutdown(shut_readwrite); 00041 } catch (...) { 00042 } 00043 } 00044 00045 sockbuf::int_type Connection::underflow() 00046 { 00047 static sptk::CWaiter lock; 00048 sptk::CGuard guard(lock); 00049 if(m_limit==0) { 00050 return eof; 00051 } 00052 sockbuf::int_type ret = sockbuf::underflow(); 00053 checkLimit(); 00054 return ret; 00055 } 00056 00057 void Connection::setLimit(long lim) 00058 { 00059 m_limit=lim; 00060 checkLimit(); 00061 } 00062 00063 void Connection::checkLimit() 00064 { 00065 if(m_limit!=-1) { 00066 m_limit-=egptr()-gptr(); 00067 if(m_limit<0) { 00068 setg(eback(), gptr(), egptr()+m_limit); 00069 m_limit=0; //Next underflow will fail. This may read a bit more data, but not too much 00070 } 00071 } 00072 } 00073 00074 }