1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-06-16 07:07:13 +02:00

Use standard types and headers.

This commit is contained in:
Sandu Liviu Catalin
2020-04-27 10:25:29 +03:00
parent 1e82d7b747
commit a6ce7811ba
9 changed files with 120 additions and 168 deletions

View File

@ -68,20 +68,20 @@ public:
/// @param pAddr specifies the destination address to connect.
/// @param nPort specifies the destination port.
/// @return true if successful connection made, otherwise false.
virtual bool Open(const char *pAddr, uint16 nPort);
virtual bool Open(const char *pAddr, uint16_t nPort);
private:
/// Utility function used to create a TCP connection, called from Open().
/// @return true if successful connection made, otherwise false.
bool ConnectTCP(const char *pAddr, uint16 nPort);
bool ConnectTCP(const char *pAddr, uint16_t nPort);
/// Utility function used to create a UDP connection, called from Open().
/// @return true if successful connection made, otherwise false.
bool ConnectUDP(const char *pAddr, uint16 nPort);
bool ConnectUDP(const char *pAddr, uint16_t nPort);
/// Utility function used to create a RAW connection, called from Open().
/// @return true if successful connection made, otherwise false.
bool ConnectRAW(const char *pAddr, uint16 nPort);
bool ConnectRAW(const char *pAddr, uint16_t nPort);
private:
struct hostent *m_pHE;

View File

@ -44,6 +44,9 @@
#ifndef __HOST_H__
#define __HOST_H__
#include <stddef.h>
#include <stdint.h>
#include <assert.h>
#include <limits.h>
#ifdef __cplusplus
@ -62,12 +65,6 @@ extern "C"
#endif
#if defined(_LINUX) || defined(_DARWIN)
typedef unsigned char uint8;
typedef char int8;
typedef unsigned short uint16;
typedef short int16;
typedef unsigned int uint32;
typedef int int32;
typedef int SOCKET;
#endif
@ -76,57 +73,12 @@ extern "C"
void *iov_base;
size_t iov_len;
};
typedef unsigned char uint8;
typedef char int8;
typedef unsigned short uint16;
typedef short int16;
typedef unsigned int uint32;
typedef int int32;
#endif
#ifdef _WIN32
typedef int socklen_t;
#endif
#if defined(_WIN32)
typedef unsigned long long int uint64;
typedef long long int int64;
#elif (__WORDSIZE == 32)
__extension__
typedef long long int int64;
__extension__
typedef unsigned long long int uint64;
#elif (__WORDSIZE == 64)
typedef unsigned long int uint64;
typedef long int int64;
#endif
#ifdef _WIN32
#ifndef UINT8_MAX
#define UINT8_MAX (UCHAR_MAX)
#endif
#ifndef UINT16_MAX
#define UINT16_MAX (USHRT_MAX)
#endif
#ifndef UINT32_MAX
#define UINT32_MAX (ULONG_MAX)
#endif
#if __WORDSIZE == 64
#define SIZE_MAX (18446744073709551615UL)
#else
#ifndef SIZE_MAX
#define SIZE_MAX (4294967295U)
#endif
#endif
#endif
#if defined(_WIN32)
#define ssize_t size_t
#endif
#ifndef TRUE
#define TRUE 1
#endif
@ -140,8 +92,8 @@ extern "C"
#define htonll(x) (x)
#define ntohll(x) (x)
#else
#define htonll(x) ((((uint64)htonl(x)) << 32) + htonl(x >> 32))
#define ntohll(x) ((((uint64)ntohl(x)) << 32) + ntohl(x >> 32))
#define htonll(x) ((((uint64_t)htonl(x)) << 32) + htonl(x >> 32))
#define ntohll(x) ((((uint64_t)ntohl(x)) << 32) + ntohl(x >> 32))
#endif
#endif
@ -161,7 +113,7 @@ extern "C"
#define RECV(a,b,c,d) recv(a, (char *)b, c, d)
#define RECVFROM(a,b,c,d,e,f) recvfrom(a, (char *)b, c, d, (sockaddr *)e, (int *)f)
#define RECV_FLAGS MSG_WAITALL
#define SELECT(a,b,c,d,e) select((int32)a,b,c,d,e)
#define SELECT(a,b,c,d,e) select((int32_t)a,b,c,d,e)
#define SEND(a,b,c,d) send(a, (const char *)b, (int)c, d)
#define SENDTO(a,b,c,d,e,f) sendto(a, (const char *)b, (int)c, d, e, f)
#define SEND_FLAGS 0
@ -185,8 +137,8 @@ extern "C"
#define RECVFROM(a,b,c,d,e,f) recvfrom(a, (char *)b, c, d, (sockaddr *)e, f)
#define RECV_FLAGS MSG_WAITALL
#define SELECT(a,b,c,d,e) select(a,b,c,d,e)
#define SEND(a,b,c,d) send(a, (const int8 *)b, c, d)
#define SENDTO(a,b,c,d,e,f) sendto(a, (const int8 *)b, c, d, e, f)
#define SEND(a,b,c,d) send(a, (const int8_t *)b, c, d)
#define SENDTO(a,b,c,d,e,f) sendto(a, (const int8_t *)b, c, d, e, f)
#define SEND_FLAGS 0
#define SENDFILE(a,b,c,d) sendfile(a, b, c, d)
#define SET_SOCKET_ERROR(x,y) errno=y

View File

@ -82,7 +82,7 @@ public:
/// condiitions will be set: CPassiveSocket::SocketAddressInUse, CPassiveSocket::SocketProtocolError,
/// CPassiveSocket::SocketInvalidSocket. The following socket errors are for Linux/Unix
/// derived systems only: CPassiveSocket::SocketInvalidSocketBuffer
bool BindMulticast(const char *pInterface, const char *pGroup, uint16 nPort);
bool BindMulticast(const char *pInterface, const char *pGroup, uint16_t nPort);
/// Create a listening socket at local ip address 'x.x.x.x' or 'localhost'
/// if pAddr is NULL on port nPort.
@ -95,7 +95,7 @@ public:
/// conditions will be set: CPassiveSocket::SocketAddressInUse, CPassiveSocket::SocketProtocolError,
/// CPassiveSocket::SocketInvalidSocket. The following socket errors are for Linux/Unix
/// derived systems only: CPassiveSocket::SocketInvalidSocketBuffer
virtual bool Listen(const char *pAddr, uint16 nPort, int32 nConnectionBacklog = 30000);
virtual bool Listen(const char *pAddr, uint16_t nPort, int32_t nConnectionBacklog = 30000);
/// Attempts to send a block of data on an established connection.
/// @param pBuf block of data to be sent.
@ -109,7 +109,7 @@ public:
/// CPassiveSocket::SocketProtocolError, CPassiveSocket::SocketNotconnected
/// <br>\b Note: This function is used only for a socket of type
/// CSimpleSocket::SocketTypeUdp
virtual int32 Send(const uint8 *pBuf, size_t bytesToSend);
virtual int32_t Send(const uint8_t *pBuf, size_t bytesToSend);
private:
struct ip_mreq m_stMulticastRequest; /// group address for multicast

View File

@ -192,7 +192,7 @@ public:
/// @param nTimeoutSec timeout in seconds for select.
/// @param nTimeoutUSec timeout in micro seconds for select.
/// @return true if socket has data ready, or false if not ready or timed out.
virtual bool Select(int32 nTimeoutSec, int32 nTimeoutUSec);
virtual bool Select(int32_t nTimeoutSec, int32_t nTimeoutUSec);
/// Does the current instance of the socket object contain a valid socket
/// descriptor.
@ -221,7 +221,7 @@ public:
/// @return number of bytes actually received.
/// @return of zero means the connection has been shutdown on the other side.
/// @return of -1 means that an error has occurred.
virtual int32 Receive(int32 nMaxBytes = 1, uint8 * pBuffer = 0);
virtual int32_t Receive(int32_t nMaxBytes = 1, uint8_t * pBuffer = 0);
/// Attempts to send a block of data on an established connection.
/// @param pBuf block of data to be sent.
@ -229,7 +229,7 @@ public:
/// @return number of bytes actually sent.
/// @return of zero means the connection has been shutdown on the other side.
/// @return of -1 means that an error has occurred.
virtual int32 Send(const uint8 *pBuf, size_t bytesToSend);
virtual int32_t Send(const uint8_t *pBuf, size_t bytesToSend);
/// Attempts to send at most nNumItem blocks described by sendVector
/// to the socket descriptor associated with the socket object.
@ -239,7 +239,7 @@ public:
/// @return number of bytes actually sent, return of zero means the
/// connection has been shutdown on the other side, and a return of -1
/// means that an error has occurred.
virtual int32 Send(const struct iovec *sendVector, int32 nNumItems);
virtual int32_t Send(const struct iovec *sendVector, int32_t nNumItems);
/// Copies data between one file descriptor and another.
/// On some systems this copying is done within the kernel, and thus is
@ -253,7 +253,7 @@ public:
/// @param pOffset from which to start reading data from input file.
/// @param nCount number of bytes to copy between file descriptors.
/// @return number of bytes written to the out socket descriptor.
virtual int32 SendFile(int32 nOutFd, int32 nInFd, off_t *pOffset, int32 nCount);
virtual int32_t SendFile(int32_t nOutFd, int32_t nInFd, off_t *pOffset, int32_t nCount);
/// Returns blocking/non-blocking state of socket.
/// @return true if the socket is non-blocking, else return false.
@ -273,21 +273,21 @@ public:
/// pointer when finished. This memory is managed internally by the CSocket
/// class.
/// @return pointer to data if valid, else returns NULL.
uint8 *GetData(void) {
uint8_t *GetData(void) {
return m_pBuffer;
};
/// Returns the number of bytes received on the last call to
/// CSocket::Receive().
/// @return number of bytes received.
int32 GetBytesReceived(void) {
int32_t GetBytesReceived(void) {
return m_nBytesReceived;
};
/// Returns the number of bytes sent on the last call to
/// CSocket::Send().
/// @return number of bytes sent.
int32 GetBytesSent(void) {
int32_t GetBytesSent(void) {
return m_nBytesSent;
};
@ -306,7 +306,7 @@ public:
/// @param bEnable true to enable option false to disable option.
/// @param nTime time in seconds to linger.
/// @return true if option successfully set
bool SetOptionLinger(bool bEnable, uint16 nTime);
bool SetOptionLinger(bool bEnable, uint16_t nTime);
/// Tells the kernel that even if this port is busy (in the TIME_WAIT state),
/// go ahead and reuse it anyway. If it is busy, but with another state,
@ -317,14 +317,14 @@ public:
/// Gets the timeout value that specifies the maximum number of seconds a
/// call to CSimpleSocket::Open waits until it completes.
/// @return the length of time in seconds
int32 GetConnectTimeoutSec(void) {
int32_t GetConnectTimeoutSec(void) {
return m_stConnectTimeout.tv_sec;
};
/// Gets the timeout value that specifies the maximum number of microseconds
/// a call to CSimpleSocket::Open waits until it completes.
/// @return the length of time in microseconds
int32 GetConnectTimeoutUSec(void) {
int32_t GetConnectTimeoutUSec(void) {
return m_stConnectTimeout.tv_usec;
};
@ -338,7 +338,7 @@ public:
/// @param nConnectTimeoutSec of timeout in seconds.
/// @param nConnectTimeoutUsec of timeout in microseconds.
/// @return true if socket connection timeout was successfully set.
void SetConnectTimeout(int32 nConnectTimeoutSec, int32 nConnectTimeoutUsec = 0)
void SetConnectTimeout(int32_t nConnectTimeoutSec, int32_t nConnectTimeoutUsec = 0)
{
m_stConnectTimeout.tv_sec = nConnectTimeoutSec;
m_stConnectTimeout.tv_usec = nConnectTimeoutUsec;
@ -347,14 +347,14 @@ public:
/// Gets the timeout value that specifies the maximum number of seconds a
/// a call to CSimpleSocket::Receive waits until it completes.
/// @return the length of time in seconds
int32 GetReceiveTimeoutSec(void) {
int32_t GetReceiveTimeoutSec(void) {
return m_stRecvTimeout.tv_sec;
};
/// Gets the timeout value that specifies the maximum number of microseconds
/// a call to CSimpleSocket::Receive waits until it completes.
/// @return the length of time in microseconds
int32 GetReceiveTimeoutUSec(void) {
int32_t GetReceiveTimeoutUSec(void) {
return m_stRecvTimeout.tv_usec;
};
@ -368,14 +368,14 @@ public:
/// @param nRecvTimeoutSec of timeout in seconds.
/// @param nRecvTimeoutUsec of timeout in microseconds.
/// @return true if socket timeout was successfully set.
bool SetReceiveTimeout(int32 nRecvTimeoutSec, int32 nRecvTimeoutUsec = 0);
bool SetReceiveTimeout(int32_t nRecvTimeoutSec, int32_t nRecvTimeoutUsec = 0);
/// Enable/disable multicast for a socket. This options is only valid for
/// socket descriptors of type CSimpleSocket::SocketTypeUdp.
/// @return true if multicast was enabled or false if socket type is not
/// CSimpleSocket::SocketTypeUdp and the error will be set to
/// CSimpleSocket::SocketProtocolError
bool SetMulticast(bool bEnable, uint8 multicastTTL = 1);
bool SetMulticast(bool bEnable, uint8_t multicastTTL = 1);
/// Return true if socket is multicast or false is socket is unicast
/// @return true if multicast is enabled
@ -390,21 +390,21 @@ public:
/// Gets the timeout value that specifies the maximum number of seconds a
/// a call to CSimpleSocket::Send waits until it completes.
/// @return the length of time in seconds
int32 GetSendTimeoutSec(void) {
int32_t GetSendTimeoutSec(void) {
return m_stSendTimeout.tv_sec;
};
/// Gets the timeout value that specifies the maximum number of microseconds
/// a call to CSimpleSocket::Send waits until it completes.
/// @return the length of time in microseconds
int32 GetSendTimeoutUSec(void) {
int32_t GetSendTimeoutUSec(void) {
return m_stSendTimeout.tv_usec;
};
/// Gets the timeout value that specifies the maximum amount of time a call
/// to CSimpleSocket::Send waits until it completes.
/// @return the length of time in seconds
bool SetSendTimeout(int32 nSendTimeoutSec, int32 nSendTimeoutUsec = 0);
bool SetSendTimeout(int32_t nSendTimeoutSec, int32_t nSendTimeoutUsec = 0);
/// Returns the last error that occured for the instace of the CSimpleSocket
/// instance. This method should be called immediately to retrieve the
@ -416,13 +416,13 @@ public:
/// Get the total time the of the last operation in milliseconds.
/// @return number of milliseconds of last operation.
uint32 GetTotalTimeMs() {
uint32_t GetTotalTimeMs() {
return m_timer.GetMilliSeconds();
};
/// Get the total time the of the last operation in microseconds.
/// @return number of microseconds or last operation.
uint32 GetTotalTimeUsec() {
uint32_t GetTotalTimeUsec() {
return m_timer.GetMicroSeconds();
};
@ -457,7 +457,7 @@ public:
/// Returns the port number on which the client is connected.
/// @return client port number.
uint16 GetClientPort() {
uint16_t GetClientPort() {
return m_stClientSockaddr.sin_port;
};
@ -469,35 +469,35 @@ public:
/// Returns the port number on which the server is connected.
/// @return server port number.
uint16 GetServerPort() {
uint16_t GetServerPort() {
return ntohs(m_stServerSockaddr.sin_port);
};
/// Get the TCP receive buffer window size for the current socket object.
/// <br><br>\b NOTE: Linux will set the receive buffer to twice the value passed.
/// @return zero on failure else the number of bytes of the TCP receive buffer window size if successful.
uint32 GetReceiveWindowSize() {
uint32_t GetReceiveWindowSize() {
return GetWindowSize(SO_RCVBUF);
};
/// Get the TCP send buffer window size for the current socket object.
/// <br><br>\b NOTE: Linux will set the send buffer to twice the value passed.
/// @return zero on failure else the number of bytes of the TCP receive buffer window size if successful.
uint32 GetSendWindowSize() {
uint32_t GetSendWindowSize() {
return GetWindowSize(SO_SNDBUF);
};
/// Set the TCP receive buffer window size for the current socket object.
/// <br><br>\b NOTE: Linux will set the receive buffer to twice the value passed.
/// @return zero on failure else the number of bytes of the TCP send buffer window size if successful.
uint32 SetReceiveWindowSize(uint32 nWindowSize) {
uint32_t SetReceiveWindowSize(uint32_t nWindowSize) {
return SetWindowSize(SO_RCVBUF, nWindowSize);
};
/// Set the TCP send buffer window size for the current socket object.
/// <br><br>\b NOTE: Linux will set the send buffer to twice the value passed.
/// @return zero on failure else the number of bytes of the TCP send buffer window size if successful.
uint32 SetSendWindowSize(uint32 nWindowSize) {
uint32_t SetSendWindowSize(uint32_t nWindowSize) {
return SetWindowSize(SO_SNDBUF, nWindowSize);
};
@ -526,11 +526,11 @@ protected:
private:
/// Generic function used to get the send/receive window size
/// @return zero on failure else the number of bytes of the TCP window size if successful.
uint32 GetWindowSize(uint32 nOptionName);
uint32_t GetWindowSize(uint32_t nOptionName);
/// Generic function used to set the send/receive window size
/// @return zero on failure else the number of bytes of the TCP window size if successful.
uint32 SetWindowSize(uint32 nOptionName, uint32 nWindowSize);
uint32_t SetWindowSize(uint32_t nOptionName, uint32_t nWindowSize);
/// Attempts to send at most nNumItem blocks described by sendVector
@ -542,7 +542,7 @@ private:
/// @return number of bytes actually sent, return of zero means the
/// connection has been shutdown on the other side, and a return of -1
/// means that an error has occurred.
int32 Writev(const struct iovec *pVector, size_t nCount);
int32_t Writev(const struct iovec *pVector, size_t nCount);
/// Flush the socket descriptor owned by the object.
/// @return true data was successfully sent, else return false;
@ -553,13 +553,13 @@ private:
protected:
SOCKET m_socket; /// socket handle
CSocketError m_socketErrno; /// number of last error
uint8 *m_pBuffer; /// internal send/receive buffer
int32 m_nBufferSize; /// size of internal send/receive buffer
int32 m_nSocketDomain; /// socket type PF_INET, PF_INET6
uint8_t *m_pBuffer; /// internal send/receive buffer
int32_t m_nBufferSize; /// size of internal send/receive buffer
int32_t m_nSocketDomain; /// socket type PF_INET, PF_INET6
CSocketType m_nSocketType; /// socket type - UDP, TCP or RAW
int32 m_nBytesReceived; /// number of bytes received
int32 m_nBytesSent; /// number of bytes sent
uint32 m_nFlags; /// socket flags
int32_t m_nBytesReceived; /// number of bytes received
int32_t m_nBytesSent; /// number of bytes sent
uint32_t m_nFlags; /// socket flags
bool m_bIsBlocking; /// is socket blocking
bool m_bIsMulticast; /// is the UDP socket multicast;
struct timeval m_stConnectTimeout; /// connection timeout

View File

@ -90,11 +90,11 @@ public:
struct timeval GetEndTime() { return m_endTime; };
void SetEndTime() { GET_CLOCK_COUNT(&m_endTime); };
uint32 GetMilliSeconds() { return (CalcTotalUSec() / MILLISECONDS_CONVERSION); };
uint32 GetMicroSeconds() { return (CalcTotalUSec()); };
uint32 GetSeconds() { return (CalcTotalUSec() / MICROSECONDS_CONVERSION); };
uint32_t GetMilliSeconds() { return (CalcTotalUSec() / MILLISECONDS_CONVERSION); };
uint32_t GetMicroSeconds() { return (CalcTotalUSec()); };
uint32_t GetSeconds() { return (CalcTotalUSec() / MICROSECONDS_CONVERSION); };
uint32 GetCurrentTime()
uint32_t GetCurrentTime()
{
struct timeval tmpTime;
GET_CLOCK_COUNT(&tmpTime);
@ -102,7 +102,7 @@ public:
};
private:
uint32 CalcTotalUSec() { return (((m_endTime.tv_sec - m_startTime.tv_sec) * MICROSECONDS_CONVERSION) +
uint32_t CalcTotalUSec() { return (((m_endTime.tv_sec - m_startTime.tv_sec) * MICROSECONDS_CONVERSION) +
(m_endTime.tv_usec - m_startTime.tv_usec)); };