|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
-
- inline int Networking::getLastError() {
- return WSAGetLastError();
- }
-
- inline int Networking::setNonBlocking(hSocket socket) {
- unsigned long nonblocking = 1;
- int result = 0;
- if(0 != ioctlsocket(socket, FIONBIO, &nonblocking)) {
- result = -1;
- }
- return result;
- }
-
- inline int Networking::closeSocket(hSocket socket) {
- return closesocket(socket);
- }
-
- inline bool Networking::WouldBlock(int ErrorCode) {
- return (WSAEWOULDBLOCK == ErrorCode);
- }
-
- inline bool Networking::InProgress(int ErrorCode) {
- return(
- WSAEINPROGRESS == ErrorCode ||
- WSAEALREADY == ErrorCode ||
- WSAEWOULDBLOCK == ErrorCode ||
- WSAEINVAL == ErrorCode
- );
- }
-
- inline bool Networking::IsConnected(int ErrorCode) {
- return(WSAEISCONN == ErrorCode);
- }
-
- #else
-
-
-
- inline int Networking::getLastError() {
- return errno;
- }
-
- inline int Networking::setNonBlocking(hSocket socket) {
- int flags, result;
- flags = fcntl(socket, F_GETFL, 0);
- result = fcntl(socket, F_SETFL, flags | O_NONBLOCK);
- return result;
- }
-
- inline int Networking::closeSocket(hSocket socket) {
- return close(socket);
- }
-
- inline bool Networking::WouldBlock(int ErrorCode) {
- return (EWOULDBLOCK == ErrorCode);
- }
-
- inline bool Networking::InProgress(int ErrorCode) {
- return(
- EINPROGRESS == ErrorCode ||
- EALREADY == ErrorCode
- );
- }
-
- inline bool Networking::IsConnected(int ErrorCode) {
- return(EISCONN == ErrorCode);
- }
-
- #endif
-
-
-
-
-
-
-
- inline IP4Address::IP4Address():IP(0){}
- inline IP4Address::IP4Address(const unsigned long int newIP):IP(newIP){}
- inline IP4Address::IP4Address(const IP4Address& newIP):IP(newIP.IP){}
-
- inline IP4Address::IP4Address(const char* newIP) { (*this) = newIP; }
- inline IP4Address::IP4Address(const string& newIP) { (*this) = newIP; }
-
- inline IP4Address&
- IP4Address::operator=(const unsigned long int Right) {
- IP = Right;
- return *this;
- }
-
- inline IP4Address& IP4Address::operator=(const char* Right) {
- IP = ntohl(inet_addr(Right));
- return *this;
- }
-
- inline IP4Address& IP4Address::operator=(const string& Right) {
- IP = ntohl(inet_addr(Right.c_str()));
- return *this;
- }
-
- inline bool IP4Address::operator<(const IP4Address Right) const {
- return (IP < Right.IP);
- }
-
- inline bool IP4Address::operator>(const IP4Address Right) const {
- return (IP > Right.IP);
- }
-
- inline bool IP4Address::operator==(const IP4Address Right) const {
- return (IP == Right.IP);
- }
-
- inline bool IP4Address::operator!=(const IP4Address Right) const {
- return (IP != Right.IP);
- }
-
- inline bool IP4Address::operator<=(const IP4Address Right) const {
- return (IP <= Right.IP);
- }
-
- inline bool IP4Address::operator>=(const IP4Address Right) const {
- return (IP >= Right.IP);
- }
-
-
-
- inline void SocketAddress::clear() {
- memset(&Address, 0, sizeof(Address));
- Address.sin_family = AF_INET;
- Address.sin_addr.s_addr = htonl(INADDR_ANY);
- Address.sin_port = 0;
- }
-
- inline SocketAddress::SocketAddress() {
- clear();
- }
-
- inline struct sockaddr_in* SocketAddress::getPtr_sockaddr_in() {
- return &Address;
- }
-
- inline struct sockaddr* SocketAddress::getPtr_sockaddr() {
- return (struct sockaddr*) &Address;
- }
-
-
- inline socklen_t SocketAddress::getAddressSize() {
- return sizeof(Address);
- }
-
- inline void SocketAddress::setAddress(unsigned long ipAddress) {
- Address.sin_addr.s_addr = htonl(ipAddress);
- }
-
- inline void SocketAddress::setAddress(char* ipString) {
- Address.sin_addr.s_addr = inet_addr(ipString);
- }
-
- inline unsigned long SocketAddress::getAddress() {
- return ntohl(Address.sin_addr.s_addr);
- }
-
- inline void SocketAddress::setPort(unsigned short port) {
- Address.sin_port = htons(port);
- }
-
- inline void SocketAddress::setPort(char* port) {
- setPort(atoi(port));
- }
-
- inline unsigned short SocketAddress::getPort() {
- return ntohs(Address.sin_port);
- }
-
- inline const char* SocketAddress::getPort(char* str) {
- if(NULL == str) {
- str = PortStringBuffer;
- }
- sprintf(str,"%d",getPort());
- return str;
- }
-
-
-
- inline Socket::Socket() :
- Handle(INVALID_SOCKET), OpenSucceeded(false) {
- }
-
- inline Socket::~Socket() {
- if(INVALID_SOCKET != Handle) {
- Network.closeSocket(Handle);
- }
- }
-
- inline void Socket::close() {
- if(INVALID_SOCKET != Handle) {
- if(Network.closeSocket(Handle)) {
- LastError = Network.getLastError();
- if(!Network.WouldBlock(LastError)) {
- throw Networking::ControlError(
- Network.DescriptiveError(
- "Socket::close()", LastError));
- }
- } else {
- LastError = 0;
- }
- Handle = INVALID_SOCKET;
- NonBlocking = false;
- OpenSucceeded = false;
- }
- }
-
- inline hSocket Socket::getHandle() {
- return Handle;
- }
-
- inline bool Socket::isNonBlocking() {
- return NonBlocking;
- }
-
- inline void Socket::makeNonBlocking() {
- if(0 > Network.setNonBlocking(Handle)) {
- LastError = Network.getLastError();
- NonBlocking = false;
- throw Networking::ControlError(
- Network.DescriptiveError(
- "Socket::makeNonBlocking()", LastError));
- } else {
- NonBlocking = true;
- }
- }
-
- inline bool Socket::isReuseAddress() { return ReuseAddress; }
- inline bool Socket::isReuseAddress(bool set) { return (ReuseAddress = set); }
-
- inline bool Socket::isOpen() {
- return(
- INVALID_SOCKET != Handle &&
- true == OpenSucceeded
- );
- }
-
- inline int Socket::getLastError() {
- return LastError;
- }
-
-
-
- inline TCPClient::TCPClient(TCPListener& L, hSocket H, SocketAddress& A) :
- MyListener(L) {
- Handle = H;
- RemoteAddress = A;
- ReadPointer = ReadBuffer;
- DataLength = 0;
- OpenSucceeded = true;
- }
-
- inline TCPClient::~TCPClient() {
- try{ if(isOpen()) close(); } catch(...) {}
- }
-
- inline void TCPClient::open() {
- throw Networking::NotSupportedError(
- Network.DescriptiveError(
- "TCPClient::open()", LastError));
- }
-
- inline bool TCPClient::ReadBufferIsEmpty() {
- return (0 >= DataLength);
- }
-
- inline void TCPClient::fillReadBuffer() {
- LastError = 0;
- ReadPointer = ReadBuffer;
- DataLength = recv(Handle, ReadBuffer, sizeof(ReadBuffer), MSG_NOSIGNAL);
-
- if(0 >= DataLength) {
- LastError = Network.getLastError();
- DataLength = 0;
- if(Network.WouldBlock(LastError)) {
- return;
- } else {
- throw Networking::SocketReadError(
- Network.DescriptiveError(
- "TCPClient::fillReadBuffer()", LastError));
- }
- }
- }
-
- inline bool TCPClient::isNonBlocking() {
- return Socket::isNonBlocking();
- }
-
- inline unsigned long TCPClient::getRemoteIP() {
- return RemoteAddress.getAddress();
- }
-
- inline const char* TCPClient::getRemoteIP(char* str) {
- return RemoteAddress.getAddress(str);
- }
-
- inline unsigned short TCPClient::getRemotePort() {
- return RemoteAddress.getPort();
- }
-
- inline const char* TCPClient::getRemotePort(char* str) {
- return RemoteAddress.getPort(str);
- }
-
-
-
- inline TCPHost::~TCPHost() {
- try{ if(isOpen()) close(); } catch(...) {}
- }
-
- inline bool TCPHost::ReadBufferIsEmpty() {
- return (0 >= DataLength);
- }
-
- inline void TCPHost::fillReadBuffer() {
- LastError = 0;
- ReadPointer = ReadBuffer;
- DataLength = recv(Handle, ReadBuffer, sizeof(ReadBuffer), MSG_NOSIGNAL);
-
- if(0 >= DataLength) {
- LastError = Network.getLastError();
- DataLength = 0;
- if(Network.WouldBlock(LastError)) {
- return;
- } else {
- throw Networking::SocketReadError(
- Network.DescriptiveError(
- "TCPHost::fillReadBuffer()", LastError));
- }
- }
- }
-
- inline bool TCPHost::isNonBlocking() {
- return Socket::isNonBlocking();
- }
-
-
-
- inline TCPListener::~TCPListener() {
- try{ close(); } catch(...) {}
- }
|