00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "stdafx.h"
00037 #include "UDPSocketAsync.h"
00038
00039 #include "ErrorHandlerMacros.h"
00040
00041 #ifdef _MEMORY_DEBUG
00042 #define new DEBUG_NEW
00043 #define malloc DEBUG_MALLOC
00044 static char THIS_FILE[] = __FILE__;
00045 #endif
00046
00047 KOMODIA_NAMESPACE_START
00048
00049 #define CUDPSocketAsync_Class "CUDPSocketAsync"
00050
00051 CUDPSocketAsync::CUDPSocketAsync(BOOL bRawSocket) : CUDPSocket(bRawSocket),
00052 CAsyncSocket()
00053 {
00054 try
00055 {
00056
00057 SetName(CUDPSocketAsync_Class);
00058 }
00059 ERROR_HANDLER("CUDPSocketAsync")
00060 }
00061
00062 CUDPSocketAsync::~CUDPSocketAsync()
00063 {
00064 try
00065 {
00066
00067 Close();
00068 }
00069 ERROR_HANDLER("~CUDPSocketAsync")
00070 }
00071
00072 BOOL CUDPSocketAsync::Listen()
00073 {
00074 try
00075 {
00076
00077 if (!CheckSocketValid())
00078 return FALSE;
00079
00080
00081 int iResult;
00082 iResult=InternalWSAAsyncSelect(WM_SOCKET_GENERAL,
00083 FD_READ);
00084
00085
00086 if (iResult)
00087 {
00088
00089 SetLastError("Listen");
00090
00091
00092 return FALSE;
00093 }
00094 else
00095 return TRUE;
00096 }
00097 ERROR_HANDLER_RETURN("Listen",FALSE)
00098 }
00099
00100 BOOL CUDPSocketAsync::SetAsync()
00101 {
00102 try
00103 {
00104
00105 if (!CheckSocketValid())
00106 return FALSE;
00107
00108
00109 int iResult;
00110 iResult=InternalWSAAsyncSelect(WM_SOCKET_GENERAL,
00111 FD_WRITE | FD_READ);
00112
00113
00114 if (iResult)
00115 {
00116
00117 SetLastError("SetAsync");
00118
00119
00120 return FALSE;
00121 }
00122 else
00123 return TRUE;
00124 }
00125 ERROR_HANDLER_RETURN("SetAsync",FALSE)
00126 }
00127
00128 BOOL CUDPSocketAsync::OnSocketConnect(int iErrorCode)
00129 {
00130
00131 return SetAsync();
00132 }
00133
00134 void CUDPSocketAsync::Accepted()
00135 {
00136 try
00137 {
00138
00139 AddSocketToList();
00140
00141
00142 SetAsync();
00143 }
00144 ERROR_HANDLER("CTCPSocketAsync")
00145 }
00146
00147 SOCKET CUDPSocketAsync::GetAsyncHandle()const
00148 {
00149 return GetHandle();
00150 }
00151
00152 BOOL CUDPSocketAsync::Create()
00153 {
00154 try
00155 {
00156 if (!CUDPSocket::Create())
00157 return FALSE;
00158 else
00159 {
00160
00161 AddSocketToList();
00162
00163
00164 return TRUE;
00165 }
00166 }
00167 ERROR_HANDLER_RETURN("Create",FALSE)
00168 }
00169
00170 BOOL CUDPSocketAsync::Close()
00171 {
00172 try
00173 {
00174
00175 if (!ValidSocket())
00176 return FALSE;
00177
00178
00179 CAsyncSocket::SocketClosing();
00180
00181
00182 RemoveSocketFromList();
00183
00184
00185 return CUDPSocket::Close();
00186 }
00187 ERROR_HANDLER_RETURN("Close",FALSE)
00188 }
00189
00190 BOOL CUDPSocketAsync::Send(IP aDestinationAddress,
00191 unsigned short usDestinationPort,
00192 const char* pBuffer,
00193 unsigned short usBufferLength)
00194 {
00195 try
00196 {
00197
00198 return CUDPSocket::Send(aDestinationAddress,
00199 usDestinationPort,
00200 pBuffer,
00201 usBufferLength);
00202 }
00203 ERROR_HANDLER_RETURN("Send",GetErrorCode())
00204 }
00205
00206 BOOL CUDPSocketAsync::Send(const std::string& rDestinationAddress,
00207 unsigned short usDestinationPort,
00208 const char* pBuffer,
00209 unsigned short usBufferLength)
00210 {
00211 try
00212 {
00213
00214 return CUDPSocket::Send(rDestinationAddress,
00215 usDestinationPort,
00216 pBuffer,
00217 usBufferLength);
00218 }
00219 ERROR_HANDLER_RETURN("Send",GetErrorCode())
00220 }
00221
00222 BOOL CUDPSocketAsync::Send(unsigned short usSourcePort,
00223 IP aDestinationAddress,
00224 unsigned short usDestinationPort,
00225 const char* pBuffer,
00226 unsigned short usBufferLength)
00227 {
00228 try
00229 {
00230
00231 int iResult;
00232 iResult=CUDPSocket::Send(usSourcePort,
00233 aDestinationAddress,
00234 usDestinationPort,
00235 pBuffer,
00236 usBufferLength);
00237
00238
00239 if (iResult==GetErrorCode() &&
00240 GetSystemLastError()==WSAEWOULDBLOCK)
00241 return 0;
00242 else
00243 return iResult;
00244 }
00245 ERROR_HANDLER_RETURN("Send",GetErrorCode())
00246 }
00247
00248 BOOL CUDPSocketAsync::Send(unsigned short usSourcePort,
00249 const std::string& rDestinationAddress,
00250 unsigned short usDestinationPort,
00251 const char* pBuffer,
00252 unsigned short usBufferLength)
00253 {
00254 try
00255 {
00256
00257 return CUDPSocket::Send(usSourcePort,
00258 rDestinationAddress,
00259 usDestinationPort,
00260 pBuffer,
00261 usBufferLength);
00262 }
00263 ERROR_HANDLER_RETURN("Send",GetErrorCode())
00264 }
00265
00266 KOMODIA_NAMESPACE_END