diff options
| author | Jørgen P. Tjernø <[email protected]> | 2013-12-02 19:31:46 -0800 |
|---|---|---|
| committer | Jørgen P. Tjernø <[email protected]> | 2013-12-02 19:46:31 -0800 |
| commit | f56bb35301836e56582a575a75864392a0177875 (patch) | |
| tree | de61ddd39de3e7df52759711950b4c288592f0dc /sp/src/public/blockingudpsocket.cpp | |
| parent | Mark some more files as text. (diff) | |
| download | source-sdk-2013-f56bb35301836e56582a575a75864392a0177875.tar.xz source-sdk-2013-f56bb35301836e56582a575a75864392a0177875.zip | |
Fix line endings. WHAMMY.
Diffstat (limited to 'sp/src/public/blockingudpsocket.cpp')
| -rw-r--r-- | sp/src/public/blockingudpsocket.cpp | 300 |
1 files changed, 150 insertions, 150 deletions
diff --git a/sp/src/public/blockingudpsocket.cpp b/sp/src/public/blockingudpsocket.cpp index 01db58fc..aefd54f7 100644 --- a/sp/src/public/blockingudpsocket.cpp +++ b/sp/src/public/blockingudpsocket.cpp @@ -1,150 +1,150 @@ -//========= Copyright Valve Corporation, All rights reserved. ============//
-//
-// Purpose:
-//
-//=============================================================================//
-
-#if defined(_WIN32) && !defined(_X360)
-#include <winsock.h>
-#elif POSIX
-#define INVALID_SOCKET -1
-#define SOCKET_ERROR -1
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <unistd.h>
-#define closesocket close
-#endif
-
-#include "blockingudpsocket.h"
-#include "tier0/vcrmode.h"
-
-class CBlockingUDPSocket::CImpl
-{
-public:
- struct sockaddr_in m_SocketIP;
- fd_set m_FDSet;
-};
-
-CBlockingUDPSocket::CBlockingUDPSocket() :
- m_cserIP(),
- m_Socket( 0 ),
- m_pImpl( new CImpl )
-{
- CreateSocket();
-}
-
-CBlockingUDPSocket::~CBlockingUDPSocket()
-{
- delete m_pImpl;
- closesocket( static_cast<unsigned int>( m_Socket ));
-}
-
-bool CBlockingUDPSocket::CreateSocket (void)
-{
- struct sockaddr_in address;
-
- m_Socket = socket( PF_INET, SOCK_DGRAM, IPPROTO_UDP );
- if ( m_Socket == INVALID_SOCKET )
- {
- return false;
- }
-
- address = m_pImpl->m_SocketIP;
-
- if ( SOCKET_ERROR == bind( m_Socket, ( struct sockaddr * )&address, sizeof( address ) ) )
- {
- return false;
- }
-
-#ifdef _WIN32
- if ( m_pImpl->m_SocketIP.sin_addr.S_un.S_addr == INADDR_ANY )
- {
- m_pImpl->m_SocketIP.sin_addr.S_un.S_addr = 0L;
- }
-#elif POSIX
- if ( m_pImpl->m_SocketIP.sin_addr.s_addr == INADDR_ANY )
- {
- m_pImpl->m_SocketIP.sin_addr.s_addr = 0L;
- }
-#endif
-
- return true;
-}
-
-bool CBlockingUDPSocket::WaitForMessage( float timeOutInSeconds )
-{
- struct timeval tv;
-
- FD_ZERO( &m_pImpl->m_FDSet );
- FD_SET( m_Socket, &m_pImpl->m_FDSet );//lint !e717
-
- tv.tv_sec = (int)timeOutInSeconds;
- float remainder = timeOutInSeconds - (int)timeOutInSeconds;
- tv.tv_usec = (int)( remainder * 1000000 + 0.5f ); /* micro seconds */
-
- if ( SOCKET_ERROR == select( ( int )m_Socket + 1, &m_pImpl->m_FDSet, NULL, NULL, &tv ) )
- {
- return false;
- }
-
- if ( FD_ISSET( m_Socket, &m_pImpl->m_FDSet) )
- {
- return true;
- }
-
- // Timed out
- return false;
-}
-
-unsigned int CBlockingUDPSocket::ReceiveSocketMessage( struct sockaddr_in *packet_from, unsigned char *buf, size_t bufsize )
-{
- memset( packet_from, 0, sizeof( *packet_from ) );
-
- struct sockaddr fromaddress;
- int fromlen = sizeof( fromaddress );
-
- int packet_length = VCRHook_recvfrom
- (
- m_Socket,
- (char *)buf,
- (int)bufsize,
- 0,
- &fromaddress,
- &fromlen
- );
-
- if ( SOCKET_ERROR == packet_length )
- {
- return 0;
- }
-
- // In case it's parsed as a string
- buf[ packet_length ] = 0;
-
- // Copy over the receive address
- *packet_from = *( struct sockaddr_in * )&fromaddress;
-
- return ( unsigned int )packet_length;
-}
-
-bool CBlockingUDPSocket::SendSocketMessage( const struct sockaddr_in & rRecipient, const unsigned char *buf, size_t bufsize )
-{
- // Send data
- int bytesSent = sendto
- (
- m_Socket,
- (const char *)buf,
- (int)bufsize,
- 0,
- reinterpret_cast< const sockaddr * >( &rRecipient ),
- sizeof( rRecipient )
- );
-
- if ( SOCKET_ERROR == bytesSent )
- {
- return false;
- }
-
- return true;
-}
+//========= Copyright Valve Corporation, All rights reserved. ============// +// +// Purpose: +// +//=============================================================================// + +#if defined(_WIN32) && !defined(_X360) +#include <winsock.h> +#elif POSIX +#define INVALID_SOCKET -1 +#define SOCKET_ERROR -1 +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <unistd.h> +#define closesocket close +#endif + +#include "blockingudpsocket.h" +#include "tier0/vcrmode.h" + +class CBlockingUDPSocket::CImpl +{ +public: + struct sockaddr_in m_SocketIP; + fd_set m_FDSet; +}; + +CBlockingUDPSocket::CBlockingUDPSocket() : + m_cserIP(), + m_Socket( 0 ), + m_pImpl( new CImpl ) +{ + CreateSocket(); +} + +CBlockingUDPSocket::~CBlockingUDPSocket() +{ + delete m_pImpl; + closesocket( static_cast<unsigned int>( m_Socket )); +} + +bool CBlockingUDPSocket::CreateSocket (void) +{ + struct sockaddr_in address; + + m_Socket = socket( PF_INET, SOCK_DGRAM, IPPROTO_UDP ); + if ( m_Socket == INVALID_SOCKET ) + { + return false; + } + + address = m_pImpl->m_SocketIP; + + if ( SOCKET_ERROR == bind( m_Socket, ( struct sockaddr * )&address, sizeof( address ) ) ) + { + return false; + } + +#ifdef _WIN32 + if ( m_pImpl->m_SocketIP.sin_addr.S_un.S_addr == INADDR_ANY ) + { + m_pImpl->m_SocketIP.sin_addr.S_un.S_addr = 0L; + } +#elif POSIX + if ( m_pImpl->m_SocketIP.sin_addr.s_addr == INADDR_ANY ) + { + m_pImpl->m_SocketIP.sin_addr.s_addr = 0L; + } +#endif + + return true; +} + +bool CBlockingUDPSocket::WaitForMessage( float timeOutInSeconds ) +{ + struct timeval tv; + + FD_ZERO( &m_pImpl->m_FDSet ); + FD_SET( m_Socket, &m_pImpl->m_FDSet );//lint !e717 + + tv.tv_sec = (int)timeOutInSeconds; + float remainder = timeOutInSeconds - (int)timeOutInSeconds; + tv.tv_usec = (int)( remainder * 1000000 + 0.5f ); /* micro seconds */ + + if ( SOCKET_ERROR == select( ( int )m_Socket + 1, &m_pImpl->m_FDSet, NULL, NULL, &tv ) ) + { + return false; + } + + if ( FD_ISSET( m_Socket, &m_pImpl->m_FDSet) ) + { + return true; + } + + // Timed out + return false; +} + +unsigned int CBlockingUDPSocket::ReceiveSocketMessage( struct sockaddr_in *packet_from, unsigned char *buf, size_t bufsize ) +{ + memset( packet_from, 0, sizeof( *packet_from ) ); + + struct sockaddr fromaddress; + int fromlen = sizeof( fromaddress ); + + int packet_length = VCRHook_recvfrom + ( + m_Socket, + (char *)buf, + (int)bufsize, + 0, + &fromaddress, + &fromlen + ); + + if ( SOCKET_ERROR == packet_length ) + { + return 0; + } + + // In case it's parsed as a string + buf[ packet_length ] = 0; + + // Copy over the receive address + *packet_from = *( struct sockaddr_in * )&fromaddress; + + return ( unsigned int )packet_length; +} + +bool CBlockingUDPSocket::SendSocketMessage( const struct sockaddr_in & rRecipient, const unsigned char *buf, size_t bufsize ) +{ + // Send data + int bytesSent = sendto + ( + m_Socket, + (const char *)buf, + (int)bufsize, + 0, + reinterpret_cast< const sockaddr * >( &rRecipient ), + sizeof( rRecipient ) + ); + + if ( SOCKET_ERROR == bytesSent ) + { + return false; + } + + return true; +} |