// Socket.cpp: implementation of the GHLSocket class.
//
//////////////////////////////////////////////////////////////////////
#include "Socket.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
/******************************************************************************
Class Name : GHLSocket
Function Name : GHLSocket() Constructor
Description : ...............
History : 02/05/2001 Albert Created
******************************************************************************/
bool GHLSocket::bWSAStartupCalled = false;
GHLSocket::GHLSocket()
{
m_pLogger = NULL;
m_bSetOrigPort = false;
m_usOrigPort = 0;
// Initialize the class members
m_usPortNum = -1; // set the port number to an invalid port number to prevent mistake in
// the future.
m_npProtocol = TCP;
m_szRemoteHost = NULL;
m_szRemoteIP = NULL;
m_szIPAddress = NULL;
m_bConnected = FALSE;
m_Socket = 0;
m_dwHeaderLen = HEADER_LEN;
// LINGER stLINGER;
if ( !bWSAStartupCalled )
{
memset ( &m_wsData, 0, sizeof(m_wsData) );
memset ( &m_saSocket, 0, sizeof(SOCKADDR_IN) );
m_wWinsockVer = MAKEWORD ( 2, 0 );
WSAStartup ( m_wWinsockVer, &m_wsData );
if ( m_wsData.wVersion != m_wWinsockVer )
{
CHKLOGGER (m_pLogger->Log(1," GHLSocket::GHLSocket() Requested winsock version not available!"));
} // end of comparing winsock version
else
{
CHKLOGGER (m_pLogger->Log(3," GHLSocket::GHLSocket() Winsock version OK"));
}
bWSAStartupCalled = true;
}
// Creating Socket Event;
m_hSocketEvent = WSACreateEvent ();
if (WSA_INVALID_EVENT == m_hSocketEvent)
{
printf ("WSACreateEvent () Fails. WSAGetLastError returns [%ld]", WSAGetLastError ( ) );
// Handle Error
closesocket (m_Socket); // Close the socket.
CHKLOGGER (m_pLogger->Log(1,"GHLSocket::dwConnect() Creating Socket Event failed."));
}
m_bInit = TRUE;
// ***** SM FIRE *****
/* if (m_Socket != 0)
{
stLINGER.l_onoff = 1;
stLINGER.l_linger = 0;
setsockopt (m_Socket, SOL_SOCKET ,SO_LINGER,(const char *) &stLINGER,sizeof(LINGER));
}
*/
/* orig code
stLINGER.l_onoff = 1;
stLINGER.l_linger = 0;
setsockopt (m_Socket, SOL_SOCKET ,SO_LINGER,(const char *) &stLINGER,sizeof(LINGER));
*/
}
/******************************************************************************
Class Name : GHLSocket
Function Name : GHLSocket() Constructor
Parameter : SOCKET sk_Remote
Description : ...............
History : 02/05/2001 Albert Created
******************************************************************************/
GHLSocket::GHLSocket(SOCKET sk_Remote)
{
int iRetval = 0;
LINGER stLINGER;
m_pLogger = NULL;
m_bInit = FALSE;
if ( INVALID_SOCKET == sk_Remote )
return;
m_bSetOrigPort = false;
m_usOrigPort = 0;
m_wWinsockVer = MAKEWORD ( 2, 0 );
WSAStartup ( m_wWinsockVer, &m_wsData );
m_Socket = sk_Remote; // assign the remote socket to be used by the class directly.
m_npProtocol = TCP;
m_szIPAddress = NULL;
m_szRemoteHost = NULL;
m_szRemoteIP = NULL;
m_usPortNum = -1;
m_dwHeaderLen = 0;
iRetval = sizeof(SOCKADDR);
// get this and the remote socket address...
getsockname ( m_Socket, (SOCKADDR *) &m_saSocket, &iRetval);
getpeername ( m_Socket, (SOCKADDR *) &m_saRemote, &iRetval );
m_szRemoteIP = (char *) MemAlloc ( sizeof(char) * 25 );
m_szIPAddress = (char *) MemAlloc ( sizeof(char) * 25 );
sprintf ( m_szIPAddress, "%d.%d.%d.%d", m_saSocket.sin_addr.S_un.S_un_b.s_b1, m_saSocket.sin_addr.S_un.S_un_b.s_b2, m_saSocket.sin_addr.S_un.S_un_b.s_b3, m_saSocket.sin_addr.S_un.S_un_b.s_b4 );
sprintf ( m_szRemoteIP, "%d.%d.%d.%d", m_saRemote.sin_addr.S_un.S_un_b.s_b1, m_saRemote.sin_addr.S_un.S_un_b.s_b2, m_saRemote.sin_addr.S_un.S_un_b.s_b3, m_saRemote.sin_addr.S_un.S_un_b.s_b4 );
m_usPortNum = ntohs ( m_saSocket.sin_port );
// Creating Socket Event;
m_hSocketEvent = WSACreateEvent ();
if (WSA_INVALID_EVENT == m_hSocketEvent)
{
// Handle Error
closesocket (m_Socket); // Close the socket.
CHKLOGGER (m_pLogger->Log(1,"GHLSocket::GHLSocket() Creating Socket Event failed."));
}
stLINGER.l_onoff = 1;
stLINGER.l_linger = 0;
setsockopt (m_Socket, SOL_SOCKET ,SO_LINGER,(const char *) &stLINGER,sizeof(LINGER));
m_bInit = TRUE;
m_bConnected = TRUE;
}
/******************************************************************************
Class Name : GHLSocket
Function Name : ~GHLSocket()
Description : ...............
History : 02/05/2001 Albert Created
******************************************************************************/
GHLSocket::~GHLSocket()
{
int iRetval = 0;
if (m_szRemoteHost) MemFree (m_szRemoteHost);
if (m_szIPAddress) MemFree (m_szIPAddress);
if (m_szRemoteIP) MemFree (m_szRemoteIP);
WSACloseEvent (m_hSocketEvent); // Close the Socket Event
if ( 0 != m_Socket )
iRetval = closesocket ( m_Socket );
}
/******************************************************************************
Class Name : GHLSocket
Function Name : StartLogging()
Parameter : char * sz_LogFileName [in]
int i_LogLevel [in]
Return : true - The function has started successfully
false - The function has fail to start.
Description : Start the logging mechanism.
History : 02/05/2001 Albert Created
******************************************************************************/
bool GHLSocket::StartLogging(char *sz_LogFileName, int i_LogLevel)
{
if (m_pLogger) delete m_pLogger; // Delete off old logger
m_pLogger = new Logger (sz_LogFileName, i_LogLevel, LOGGER_TYPE_DAILY);
if (!m_pLogger)
return false;
CHKLOGGER (m_pLogger->LogMsg (3,""));
CHKLOGGER (m_pLogger->LogMsg (3,"================================"));
CHKLOGGER (m_pLogger->Log(3,"GHLSocket::StartLogging() Logging Mechanism started "));
return true;
}
/******************************************************************************
Class Name : GHLSocket
Function Name : dwConnect()
Parameter : none
Return : S_OK Success
E_PARAMETER_NOT_SET The two requested parameters are not set.
E_CREATE_SOCKET_FAILED Socket creation failed
E_CONNECT_SOCKET_FAILED Socket connection failed
Description : .............
History : 02/05/2001 Albert Created
******************************************************************************/
DWORD GHLSocket::dwConnect()
{
int iRetval = 0;
HOSTENT * phRemoteHost = NULL;
LINGER stLINGER;
BOOL bReuse = TRUE;
SOCKADDR_IN SocketAddress; // The socket address for the socket to be used.
// check if the required data have been filled in.
if ( (!m_szRemoteHost && !m_szIPAddress) || -1 == m_usPortNum )
return E_PARAMETER_NOT_SET;
// set the socket address structure.
if ( m_szRemoteHost )
{
phRemoteHost = gethostbyname ( m_szRemoteHost );
m_saSocket.sin_port = htons(m_usPortNum);
m_saSocket.sin_family = AF_INET;
memcpy ( &(m_saSocket.sin_addr), phRemoteHost->h_addr, sizeof(int) );
}
else if ( m_szIPAddress )
{
//phRemoteHost = gethostbyaddr ( (const char*) m_szIPAddress, strlen( (const char*) m_szIPAddress), TCP );
unsigned long ulDestSockAddr;
ulDestSockAddr = inet_addr (m_szIPAddress);
m_saSocket.sin_port = htons(m_usPortNum);
m_saSocket.sin_family = AF_INET;
memcpy ( &(m_saSocket.sin_addr), &ulDestSockAddr, sizeof(ulDestSockAddr) );
}
// create the socket now...
m_Socket = socket ( AF_INET, SOCK_STREAM, ( TCP == m_npProtocol ) ? IPPROTO_TCP : IPPROTO_UDP );
if ( 0 == m_Socket )
{
CHKLOGGER (m_pLogger->Log(1,"GHLSocket::dwConnect() Socket creation failed."));
#if defined GHLSOCKET_DEBUG_VERSIO
- 1
- 2
前往页