BlockedBuffer.cppGo to the documentation of this file.00001 /* 00002 * Copyright (c) 2000-2003 Barak Weichselbaum <barak@komodia.com> 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 00014 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 00015 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00016 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00017 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 00018 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00019 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00020 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00021 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00022 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00023 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00024 * SUCH DAMAGE. 00025 * 00026 * Contact info: 00027 * ------------- 00028 * 00029 * Site: http://www.komodia.com 00030 * Main contact: barak@komodia.com 00031 * For custom projects, 00032 * consulting, or other 00033 * paid services: sales@komodia.com 00034 */ 00035 00036 #include "stdafx.h" 00037 #include "BlockedBuffer.h" 00038 00039 #include "ErrorHandlerMacros.h" 00040 #include "OSManager.h" 00041 #include "GenericCriticalSection.h" 00042 00043 #include "AsyncSocket.h" 00044 00045 #ifdef _MEMORY_DEBUG 00046 #define new DEBUG_NEW 00047 #define malloc DEBUG_MALLOC 00048 static char THIS_FILE[] = __FILE__; 00049 #endif 00050 00051 KOMODIA_NAMESPACE_START 00052 00053 #define CBlockedBuffer_Class "CBlockedBuffer" 00054 00055 CBlockedBuffer::CBlockedBuffer(CAsyncSocket* pFather, 00056 int iMaxRetries) : CSpoofBase(), 00057 m_pFather(pFather), 00058 m_pCSection(NULL), 00059 m_iMaxRetries(iMaxRetries) 00060 { 00061 try 00062 { 00063 //Set our name 00064 SetName(CBlockedBuffer_Class); 00065 00066 //Create the CS 00067 m_pCSection=COSManager::CreateCriticalSection(); 00068 } 00069 ERROR_HANDLER("CBlockedBuffer") 00070 } 00071 00072 CBlockedBuffer::~CBlockedBuffer() 00073 { 00074 try 00075 { 00076 //Delete the CS 00077 delete m_pCSection; 00078 } 00079 ERROR_HANDLER("~CBlockedBuffer") 00080 } 00081 00082 void CBlockedBuffer::SendData() 00083 { 00084 try 00085 { 00086 //Lock 00087 CCriticalAutoRelease aRelease(m_pCSection); 00088 00089 //Do we have data ? 00090 if (!m_aData.empty()) 00091 //Try to send it 00092 if (m_pFather->SendBlockedBuffer(*m_aData.begin())) 00093 //We can remove it 00094 m_aData.pop_front(); 00095 else if (m_iMaxRetries && 00096 m_aData.begin()->GetNumberOfCalls()==m_iMaxRetries) 00097 { 00098 //We can't send it, remove it and indicate an error 00099 m_aData.pop_front(); 00100 00101 //Report it 00102 ReportError("SendData","Failed sending data for more then 5 times!"); 00103 } 00104 } 00105 ERROR_HANDLER("SendData") 00106 } 00107 00108 void CBlockedBuffer::AddRecord(const CBlockedData& rRecord) 00109 { 00110 try 00111 { 00112 //Lock the vector and add the data 00113 CCriticalAutoRelease aRelease(m_pCSection); 00114 00115 //Add it 00116 m_aData.push_back(rRecord); 00117 } 00118 ERROR_HANDLER("AddRecord") 00119 } 00120 00121 KOMODIA_NAMESPACE_END |