ICMPSocketAsync.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 "ICMPSocketAsync.h" 00038 00039 #include "ErrorHandlerMacros.h" 00040 #include "Array_ptr.h" 00041 00042 #ifdef _MEMORY_DEBUG 00043 #define new DEBUG_NEW 00044 #define malloc DEBUG_MALLOC 00045 static char THIS_FILE[] = __FILE__; 00046 #endif 00047 00048 KOMODIA_NAMESPACE_START 00049 00050 #define CICMPSocketAsync_Class "CICMPSocketAsync" 00051 00052 CICMPSocketAsync::CICMPSocketAsync(BOOL bRawSocket) : CICMPSocket(bRawSocket), 00053 CAsyncSocket() 00054 { 00055 try 00056 { 00057 //Set our name 00058 SetName(CICMPSocketAsync_Class); 00059 } 00060 ERROR_HANDLER("CICMPSocketAsync") 00061 } 00062 00063 CICMPSocketAsync::~CICMPSocketAsync() 00064 { 00065 try 00066 { 00067 //We need to close it here 00068 Close(); 00069 } 00070 ERROR_HANDLER("~CICMPSocketAsync") 00071 } 00072 00073 BOOL CICMPSocketAsync::SetAsync() 00074 { 00075 try 00076 { 00077 //Set event to read / write / close / oob 00078 int iResult; 00079 iResult=InternalWSAAsyncSelect(WM_SOCKET_GENERAL, 00080 FD_WRITE | FD_READ); 00081 00082 //Is it OK? 00083 if (iResult) 00084 { 00085 //Set socket error 00086 SetLastError("SetAsync"); 00087 00088 //Exit 00089 return FALSE; 00090 } 00091 00092 //Done 00093 return TRUE; 00094 } 00095 ERROR_HANDLER_RETURN("SetAsync",FALSE) 00096 } 00097 00098 BOOL CICMPSocketAsync::OnSocketReceive(int iErrorCode) 00099 { 00100 try 00101 { 00102 //Here we receive the data 00103 if (!iErrorCode) 00104 { 00105 //Buffer, can be up to 65535 00106 char* pBuffer; 00107 pBuffer=new char[65535]; 00108 00109 //Protect the data 00110 CArray_ptr<char> pProtection(pBuffer); 00111 00112 //Read the data 00113 int iRead; 00114 iRead=Receive(pBuffer,65535); 00115 00116 //Process result 00117 BOOL bResult=FALSE; 00118 00119 //Only if not an error 00120 if (iRead!=GetErrorCode()) 00121 bResult=ProccessICMP(pBuffer); 00122 00123 //Exit 00124 return bResult; 00125 } 00126 else 00127 return FALSE; 00128 } 00129 ERROR_HANDLER_RETURN("OnSocketReceive",FALSE) 00130 } 00131 00132 BOOL CICMPSocketAsync::Create() 00133 { 00134 try 00135 { 00136 //Try to create as an ICMP socket 00137 if (!CICMPSocket::Create()) 00138 { 00139 //Report it 00140 ReportError("Create","Failed to create ICMP socket!"); 00141 00142 //Exit 00143 return FALSE; 00144 } 00145 00146 //Add the socket to the asynch list 00147 AddSocketToList(); 00148 00149 //Set async status 00150 return SetAsync(); 00151 } 00152 ERROR_HANDLER_RETURN("Create",FALSE) 00153 } 00154 00155 SOCKET CICMPSocketAsync::GetAsyncHandle() const 00156 { 00157 return GetHandle(); 00158 } 00159 00160 BOOL CICMPSocketAsync::Close() 00161 { 00162 try 00163 { 00164 //Quit if not ok 00165 if (!ValidSocket()) 00166 return FALSE; 00167 00168 //Kill the timer 00169 CAsyncSocket::SocketClosing(); 00170 00171 //Remove from socket list 00172 RemoveSocketFromList(); 00173 00174 return CICMPSocket::Close(); 00175 } 00176 ERROR_HANDLER_RETURN("Close",FALSE) 00177 } 00178 00179 KOMODIA_NAMESPACE_END |