From: <at...@us...> - 2007-08-21 08:30:16
|
Revision: 479 http://cadcdev.svn.sourceforge.net/cadcdev/?rev=479&view=rev Author: atani Date: 2007-08-21 01:30:14 -0700 (Tue, 21 Aug 2007) Log Message: ----------- various cleanups and fixes for TCPServerSocket Modified Paths: -------------- tiki/examples/net/basic/src/main.cpp tiki/include/Tiki/net/buffer.h tiki/include/Tiki/net/socket.h tiki/include/Tiki/net/tcpserversocket.h tiki/include/Tiki/net/tcpsocket.h tiki/osx/include/Tiki/platnet.h tiki/osx/src/platnet.cpp tiki/src/net/address.cpp tiki/src/net/socket.cpp tiki/src/net/tcpserversocket.cpp tiki/src/net/tcpsocket.cpp tiki/win32/include/Tiki/platnet.h tiki/win32/src/platnet.cpp tiki/win32/tiki_vc80.sln Added Paths: ----------- tiki/include/Tiki/net.h Removed Paths: ------------- tiki/include/Tiki/net/net.h Modified: tiki/examples/net/basic/src/main.cpp =================================================================== --- tiki/examples/net/basic/src/main.cpp 2007-08-21 04:55:20 UTC (rev 478) +++ tiki/examples/net/basic/src/main.cpp 2007-08-21 08:30:14 UTC (rev 479) @@ -9,7 +9,7 @@ #include <Tiki/tiki.h> #include <Tiki/refcnt.h> #include <Tiki/tikitime.h> -#include <Tiki/net/net.h> +#include <Tiki/net.h> using namespace Tiki; using namespace Tiki::Debug; Modified: tiki/include/Tiki/net/buffer.h =================================================================== --- tiki/include/Tiki/net/buffer.h 2007-08-21 04:55:20 UTC (rev 478) +++ tiki/include/Tiki/net/buffer.h 2007-08-21 08:30:14 UTC (rev 479) @@ -29,6 +29,11 @@ memcpy(m_data, data, len); } + void reset() { + memset(m_data, 0, m_dataLen); + m_usedDataLen = 0; + } + uint8 *getData() const { return m_data; } Deleted: tiki/include/Tiki/net/net.h =================================================================== --- tiki/include/Tiki/net/net.h 2007-08-21 04:55:20 UTC (rev 478) +++ tiki/include/Tiki/net/net.h 2007-08-21 08:30:14 UTC (rev 479) @@ -1,31 +0,0 @@ -/* - Tiki - - net.h - - Copyright (C)2007 Atani Software -*/ -#ifndef __TIKI_NET_H -#define __TIKI_NET_H - -#include "Tiki/platnet.h" - -namespace Tiki { - -namespace Net { - -void init(); -void shutdown(); - -} // namespace Net - -} // namespace Tiki - -#endif // __TIKI_NET_H - -#include <Tiki/net/address.h> -#include <Tiki/net/buffer.h> -#include <Tiki/net/socket.h> -#include <Tiki/net/tcpsocket.h> -#include <Tiki/net/tcpserversocket.h> -#include <Tiki/net/udpsocket.h> Modified: tiki/include/Tiki/net/socket.h =================================================================== --- tiki/include/Tiki/net/socket.h 2007-08-21 04:55:20 UTC (rev 478) +++ tiki/include/Tiki/net/socket.h 2007-08-21 08:30:14 UTC (rev 479) @@ -37,12 +37,17 @@ return m_localAddress; } + void setLocalAddress(RefPtr<Address> address) + { + m_localAddress = address; + } + bool isNonBlocking() { return m_blocking; } - void setNonBlocking(bool blocking) + virtual void setNonBlocking(bool blocking) { m_blocking = blocking; } @@ -65,6 +70,8 @@ virtual void close() = 0; + virtual bool isOpen() = 0; + private: RefPtr<Address> m_peerAddress; RefPtr<Address> m_localAddress; Modified: tiki/include/Tiki/net/tcpserversocket.h =================================================================== --- tiki/include/Tiki/net/tcpserversocket.h 2007-08-21 04:55:20 UTC (rev 478) +++ tiki/include/Tiki/net/tcpserversocket.h 2007-08-21 08:30:14 UTC (rev 479) @@ -22,7 +22,7 @@ TCPServerSocket() : TCPSocket() {}; TCPServerSocket(RefPtr<Address> address) : TCPSocket(address) {}; - void bind(); + void bind(size_t maxwaiting = 10); RefPtr<TCPSocket> accept(); }; Modified: tiki/include/Tiki/net/tcpsocket.h =================================================================== --- tiki/include/Tiki/net/tcpsocket.h 2007-08-21 04:55:20 UTC (rev 478) +++ tiki/include/Tiki/net/tcpsocket.h 2007-08-21 08:30:14 UTC (rev 479) @@ -30,13 +30,22 @@ SOCKET_ERROR = -1 }; #endif - TCPSocket() : Socket() {}; - TCPSocket(RefPtr<Address> address) : Socket(address) {}; + TCPSocket() : Socket(), m_open(false) {}; + TCPSocket(RefPtr<Address> address) : Socket(address), m_open(false) {}; +#if TIKI_PLAT == TIKI_WIN32 + TCPSocket(RefPtr<Address> address, SOCKET socket) : Socket(address), m_socket(socket), m_open(true) {setNonBlocking(false);}; +#else + TCPSocket(RefPtr<Address> address, int socket) : Socket(address), m_socket(socket) {setNonBlocking(false);}; +#endif virtual void send(RefPtr<Buffer> data); virtual void recv(RefPtr<Buffer> data); + virtual bool isOpen() { + return m_open; + } + uint32 getPort() { return m_port; @@ -50,11 +59,12 @@ virtual void close(); - private: + virtual void setNonBlocking(bool blocking); + + protected: uint32 m_port; -#if TIKI_PLAT == TIKI_OSX - ; -#elif TIKI_PLAT == TIKI_WIN32 + bool m_open; +#if TIKI_PLAT == TIKI_WIN32 SOCKET m_socket; #else int m_socket; Copied: tiki/include/Tiki/net.h (from rev 476, tiki/include/Tiki/net/net.h) =================================================================== --- tiki/include/Tiki/net.h (rev 0) +++ tiki/include/Tiki/net.h 2007-08-21 08:30:14 UTC (rev 479) @@ -0,0 +1,30 @@ +/* + Tiki + + net.h + + Copyright (C)2007 Atani Software +*/ +#ifndef __TIKI_NET_H +#define __TIKI_NET_H + +namespace Tiki { + +namespace Net { + +void init(); +void shutdown(); + +} // namespace Net + +} // namespace Tiki + +#endif // __TIKI_NET_H + +#include "Tiki/platnet.h" +#include "Tiki/net/address.h" +#include "Tiki/net/buffer.h" +#include "Tiki/net/socket.h" +#include "Tiki/net/tcpsocket.h" +#include "Tiki/net/tcpserversocket.h" +#include "Tiki/net/udpsocket.h" Modified: tiki/osx/include/Tiki/platnet.h =================================================================== --- tiki/osx/include/Tiki/platnet.h 2007-08-21 04:55:20 UTC (rev 478) +++ tiki/osx/include/Tiki/platnet.h 2007-08-21 08:30:14 UTC (rev 479) @@ -8,26 +8,16 @@ #ifndef TIKI_PLATFORM_NET_H #define TIKI_PLATFORM_NET_H -#include <OpenTransport.h> -#include <OpenTptInternet.h> -#include <Events.h> -namespace Tiki { +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <fcntl.h> +#include <netinet/in.h> +#include <netinet/tcp.h> +#include <netdb.h> +#include <sys/socket.h> +#include <sys/time.h> -namespace Net { - -enum DNS_STATUS { - NOT_READY = 0, - READY, - RESOLVED, - ERROR -}; - -extern DNS_STATUS g_tikiDNSStatus; -extern InetSvcRef g_tikiDNSRef; - -}; // namespace Net - -}; // namespace Tiki - #endif // TIKI_PLATFORM_NET_H Modified: tiki/osx/src/platnet.cpp =================================================================== --- tiki/osx/src/platnet.cpp 2007-08-21 04:55:20 UTC (rev 478) +++ tiki/osx/src/platnet.cpp 2007-08-21 08:30:14 UTC (rev 479) @@ -13,70 +13,12 @@ namespace Net { -DNS_STATUS g_tikiDNSStatus; -InetSvcRef g_tikiDNSRef; - -static pascal void dnsNotifier(void *context, OTEventCode code, OTResult result, void *cookie) { - switch(code) { - case T_OPENCOMPLETE: - if(result == kOTNoError) { - g_tikiDNSStatus = READY; - g_tikiDNSRef = (InetSvcRef)cookie; - } - else { - g_tikiDNSStatus = ERROR; - Tiki::Debug::printf("T_DNRSTRINGTOADDRCOMPLETE event returned an error\n"); - } - break; - case T_DNRSTRINGTOADDRCOMPLETE: - g_tikiDNSStatus = RESOLVED; - break; - default: - if(result != kOTNoError) { - g_tikiDNSStatus = ERROR; - Tiki::Debug::printf("Error in DNS Resolver\n"); - } - } -} - -void startDNS() { - OSStatus status; - - status = OTAsyncOpenInternetServices( - kDefaultInternetServicesPath, 0, dnsNotifier, NULL); - if(status != noErr) { - Tiki::Debug::printf("Unable to initialize DNS Handler\n"); - } -} - -void stopDNS() { - if(g_dnsRef != 0) { - OTCloseProvider(g_tikiDNSRef); - g_tikiDNSStatus = NOT_READY; - g_tikiDNSRef = 0; - } -} - void init() { - OSStatus status; - - g_tikiDNSStatus = NOT_READY; - g_tikiDNSRef = 0; - - status = InitOpenTransport(); - if(status == noErr) { - StartDNS(); - } - else { - Tiki::Debug::printf("Unable to initialize OpenTransport\n"); - } } void shutdown() { - stopDNS(); } +}; // namespace Net -} - -}; +}; // namespace Tiki Modified: tiki/src/net/address.cpp =================================================================== --- tiki/src/net/address.cpp 2007-08-21 04:55:20 UTC (rev 478) +++ tiki/src/net/address.cpp 2007-08-21 08:30:14 UTC (rev 479) @@ -8,7 +8,7 @@ #include "pch.h" #include "Tiki/tiki.h" -#include "Tiki/net/net.h" +#include "Tiki/net.h" #include <sstream> @@ -31,28 +31,11 @@ m_ip != AddressAny && m_ip != AddressNone ) { -#if TIKI_PLAT == TIKI_OSX - InetHost ip; - InetDomainName domainName; - OSStatus status; - - domainName[0] = '\0'; - ip = m_ip; - status = OTInetAddressToName(g_tikiDNSRef, ip, domainName); - if(status == kOTNoError) { - while(g_tikiDNSStatus != RESOLVED) - { - ; - } - return domainName; - } -#else struct hostent *hp = gethostbyaddr((char *)&m_ip, 4, AF_INET); if(hp != NULL) { m_hostname = string(hp->h_name); } -#endif } return m_hostname; @@ -69,22 +52,11 @@ if(m_ip == AddressUnknown) { Tiki::Debug::printf("resolving host: %s\n", m_hostname.c_str()); -#if TIKI_PLAT == TIKI_OSX - InetHostInfo host; - if(OTInetStringToAddress(g_tikiDNSRef, m_hostname.c_str(), &info) == noErr) { - while( g_tikiDNSStatus != RESOLVED) - { - WaitNextEvent(everyEvent, 0, 1, NULL); - } - return info.addrs[0]; - } -#else struct hostent *hp = gethostbyname(m_hostname.c_str()); if(hp != NULL) { memcpy(&m_ip, hp->h_addr, hp->h_length); } -#endif } return m_ip; } @@ -102,7 +74,6 @@ uint32 ipnums = getIPAddress(); uint8 *ip = (uint8 *)(&ipnums); - //Tiki::Debug::printf("host ip: %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]); std::ostringstream address; Modified: tiki/src/net/socket.cpp =================================================================== --- tiki/src/net/socket.cpp 2007-08-21 04:55:20 UTC (rev 478) +++ tiki/src/net/socket.cpp 2007-08-21 08:30:14 UTC (rev 479) @@ -8,7 +8,7 @@ #include "pch.h" #include "Tiki/tiki.h" -#include "Tiki/net/net.h" +#include "Tiki/net.h" namespace Tiki { Modified: tiki/src/net/tcpserversocket.cpp =================================================================== --- tiki/src/net/tcpserversocket.cpp 2007-08-21 04:55:20 UTC (rev 478) +++ tiki/src/net/tcpserversocket.cpp 2007-08-21 08:30:14 UTC (rev 479) @@ -1,33 +1,77 @@ -/* - Tiki +/* + Tiki + + tcpserversocket.cpp + + Copyright (C)2007 Atani Software +*/ + +#include "pch.h" +#include "Tiki/tiki.h" +#include "Tiki/net.h" + +namespace Tiki { + +namespace Net { + +namespace TCP { + +void TCPServerSocket::bind(size_t maxwaiting) +{ + struct sockaddr_in sock_addr; + + memset(&sock_addr, 0, sizeof(sock_addr)); + sock_addr.sin_family = AF_INET; + sock_addr.sin_addr.s_addr = INADDR_ANY; + sock_addr.sin_port = htons(getPort()); - tcpserversocket.cpp - - Copyright (C)2007 Atani Software -*/ - -#include "pch.h" -#include "Tiki/tiki.h" -#include "Tiki/net/net.h" - -namespace Tiki { - -namespace Net { - -namespace TCP { - -void TCPServerSocket::bind() -{ - -} - -RefPtr<TCPSocket> TCPServerSocket::accept() -{ - return NULL; -} - -} // namespace TCP - -} // namespace Net - -} // namespace Tiki + if(isNonBlocking()) { + Tiki::Debug::printf("setting NO BLOCKING mode\n"); +#if TIKI_PLAT == TIKI_WIN32 + unsigned long mode = 1; + ioctlsocket(m_socket, FIONBIO, &mode); +#else + fcntl(m_socket, F_SETFL, O_NONBLOCK); +#endif + } + + + /* Bind the socket for listening */ + if ( ::bind(m_socket, (struct sockaddr *)&sock_addr, + sizeof(sock_addr)) == SOCKET_ERROR ) { + Tiki::Debug::printf("Couldn't bind to local port\n"); + close(); + return; + } + if ( listen(m_socket, (int)maxwaiting) == SOCKET_ERROR ) { + Tiki::Debug::printf("Couldn't listen to local port\n"); + close(); + return; + } +} + +RefPtr<TCPSocket> TCPServerSocket::accept() +{ + struct sockaddr_in sock_addr; +#if TIKI_PLAT == TIKI_WIN32 + SOCKET socket; +#else + int socket; +#endif + + size_t addrlen = sizeof(struct sockaddr_in); + socket = ::accept(m_socket, (sockaddr *)&sock_addr, (int *)&addrlen); + + if(socket != INVALID_SOCKET) { + RefPtr<Address> remote = new Address(); + remote->setIPAddress(sock_addr.sin_addr.s_addr); + return new TCPSocket(remote, socket); + } + return NULL; +} + +} // namespace TCP + +} // namespace Net + +} // namespace Tiki Modified: tiki/src/net/tcpsocket.cpp =================================================================== --- tiki/src/net/tcpsocket.cpp 2007-08-21 04:55:20 UTC (rev 478) +++ tiki/src/net/tcpsocket.cpp 2007-08-21 08:30:14 UTC (rev 479) @@ -8,7 +8,7 @@ #include "pch.h" #include "Tiki/tiki.h" -#include "Tiki/net/net.h" +#include "Tiki/net.h" #include <errno.h> @@ -19,8 +19,6 @@ namespace TCP { void TCPSocket::send(const RefPtr<Buffer> buffer) { -#if TIKI_PLAT == TIKI_OSX -#else uint8 *data = buffer->getData(); size_t dataLen = buffer->getUsedDataLen(); int len; @@ -38,39 +36,47 @@ } } while(dataLen > 0 && (len > 0 || errno == EINTR)); Tiki::Debug::printf("errno: %d [%x]\n", errno, errno); -#endif } void TCPSocket::recv(RefPtr<Buffer> data) { -#if TIKI_PLAT == TIKI_OSX -#else uint8 *tmp = new uint8[data->getDataLen()]; memset(tmp, 0, data->getDataLen()); int recvlen = 0; errno = 0; + Tiki::Debug::printf("receiving %d bytes\n", data->getDataLen()); do { - Tiki::Debug::printf("receiving %d bytes\n", data->getDataLen()); recvlen = ::recv(m_socket, (char *)tmp, (int)data->getDataLen(), 0); - } while(errno == EINTR && (recvlen < 0 && !isNonBlocking())); +#if TIKI_PLAT == TIKI_WIN32 + } while(recvlen == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK); +#else + } while(errno == EINTR); +#endif Tiki::Debug::printf("received %d bytes\nerrno %d\n", recvlen, errno); if(recvlen > 0) { data->setData(tmp, recvlen); } -#endif + else + { + close(); + } + delete [] tmp; } void TCPSocket::open() { -#if TIKI_PLAT == TIKI_OSX -#else struct sockaddr_in sock_addr; m_socket = socket(AF_INET, SOCK_STREAM, 0); if(m_socket == INVALID_SOCKET) { Tiki::Debug::printf("Error opening socket\n"); + m_open = false; return; } + int yes = 1; + Tiki::Debug::printf("setting TCP_NODELAY\n"); + setsockopt(m_socket, IPPROTO_TCP, TCP_NODELAY, (char *)&yes, sizeof(yes)); + #if TIKI_PLAT != TIKI_WIN32 if(isReuse()) { Tiki::Debug::printf("setting reuse flag\n"); @@ -78,9 +84,10 @@ } #endif - if(!(getLocalAddress()->getIPAddress() == Address::AddressAny || - getLocalAddress()->getIPAddress() == Address::AddressNone || - getLocalAddress()->getIPAddress() == Address::AddressBroadcast)) { + if(getPeerAddress() != NULL && + !(getPeerAddress()->getIPAddress() == Address::AddressAny || + getPeerAddress()->getIPAddress() == Address::AddressNone || + getPeerAddress()->getIPAddress() == Address::AddressBroadcast)) { // connecting to remote address memset(&sock_addr, 0, sizeof(struct sockaddr_in)); sock_addr.sin_family = AF_INET; @@ -94,36 +101,44 @@ } Tiki::Debug::printf("Connected..\n"); } - - Tiki::Debug::printf("setting TCP_NODELAY\n"); - setsockopt(m_socket, IPPROTO_TCP, TCP_NODELAY, (char *)&yes, sizeof(yes)); - - if(isNonBlocking()) { - Tiki::Debug::printf("setting NO BLOCKING mode\n"); -#if TIKI_PLAT == TIKI_WIN32 - unsigned long mode = 1; - ioctlsocket(m_socket, FIONBIO, &mode); -#else - fcntl(m_socket, F_SETFL, O_NONBLOCK); -#endif - } -#endif + m_open = true; } void TCPSocket::close() { + m_open = false; Tiki::Debug::printf("closing socket\n"); -#if TIKI_PLAT == TIKI_OSX -#elif TIKI_PLAT == TIKI_WIN32 +#if TIKI_PLAT == TIKI_WIN32 if(m_socket != INVALID_SOCKET) { closesocket(m_socket); } #else if(m_socket != INVALID_SOCKET) { ::close(m_socket); - } + } #endif } +void TCPSocket::setNonBlocking(bool blocking) +{ + Tiki::Debug::printf("%sabling blocking\n", blocking ? "dis" : "en" ); + Socket::setNonBlocking(blocking); +#if TIKI_PLAT == TIKI_WIN32 + unsigned long mode = (!blocking); + ioctlsocket(m_socket, FIONBIO, &mode); +#else + int flags = fcntl(m_socket, F_GETFL, 0); + if(!blocking) + { + fcntl(m_socket, F_SETFL, flags & ~O_NONBLOCK); + } + else + { + fcntl(m_socket, F_SETFL, flags & ~O_NONBLOCK); + } +#endif + +} + } // namespace TCP } // namespace Net Modified: tiki/win32/include/Tiki/platnet.h =================================================================== --- tiki/win32/include/Tiki/platnet.h 2007-08-21 04:55:20 UTC (rev 478) +++ tiki/win32/include/Tiki/platnet.h 2007-08-21 08:30:14 UTC (rev 479) @@ -1,3 +1,14 @@ +/* + Tiki + + platnet.h + + Copyright (C)2007 Atani Software +*/ + +#ifndef TIKI_PLATFORM_NET_H +#define TIKI_PLATFORM_NET_H + #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -3,3 +14,5 @@ #include <fcntl.h> -#include <winsock2.h> \ No newline at end of file +#include <winsock2.h> + +#endif // TIKI_PLATFORM_NET_H Modified: tiki/win32/src/platnet.cpp =================================================================== --- tiki/win32/src/platnet.cpp 2007-08-21 04:55:20 UTC (rev 478) +++ tiki/win32/src/platnet.cpp 2007-08-21 08:30:14 UTC (rev 479) @@ -8,7 +8,7 @@ #include "pch.h" #include "Tiki/tiki.h" -#include "Tiki/net/net.h" +#include "Tiki/net.h" namespace Tiki { Modified: tiki/win32/tiki_vc80.sln =================================================================== --- tiki/win32/tiki_vc80.sln 2007-08-21 04:55:20 UTC (rev 478) +++ tiki/win32/tiki_vc80.sln 2007-08-21 08:30:14 UTC (rev 479) @@ -18,6 +18,16 @@ {F2816CAC-B560-4ED9-8A73-9635F832943C} = {F2816CAC-B560-4ED9-8A73-9635F832943C} EndProjectSection EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ChatClient", "..\examples\net\chat\chat.vcproj", "{7B823C96-860C-4578-95DD-1087A45AF1AA}" + ProjectSection(ProjectDependencies) = postProject + {F2816CAC-B560-4ED9-8A73-9635F832943C} = {F2816CAC-B560-4ED9-8A73-9635F832943C} + EndProjectSection +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ChatServer", "..\examples\net\chatd\chatd.vcproj", "{7B823C96-860C-4578-95EE-1087A45AF1AA}" + ProjectSection(ProjectDependencies) = postProject + {F2816CAC-B560-4ED9-8A73-9635F832943C} = {F2816CAC-B560-4ED9-8A73-9635F832943C} + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 @@ -40,6 +50,14 @@ {7B823C96-860C-4578-95CC-1087A45AF1AA}.Debug|Win32.Build.0 = Debug|Win32 {7B823C96-860C-4578-95CC-1087A45AF1AA}.Release|Win32.ActiveCfg = Release|Win32 {7B823C96-860C-4578-95CC-1087A45AF1AA}.Release|Win32.Build.0 = Release|Win32 + {7B823C96-860C-4578-95DD-1087A45AF1AA}.Debug|Win32.ActiveCfg = Debug|Win32 + {7B823C96-860C-4578-95DD-1087A45AF1AA}.Debug|Win32.Build.0 = Debug|Win32 + {7B823C96-860C-4578-95DD-1087A45AF1AA}.Release|Win32.ActiveCfg = Release|Win32 + {7B823C96-860C-4578-95DD-1087A45AF1AA}.Release|Win32.Build.0 = Release|Win32 + {7B823C96-860C-4578-95EE-1087A45AF1AA}.Debug|Win32.ActiveCfg = Debug|Win32 + {7B823C96-860C-4578-95EE-1087A45AF1AA}.Debug|Win32.Build.0 = Debug|Win32 + {7B823C96-860C-4578-95EE-1087A45AF1AA}.Release|Win32.ActiveCfg = Release|Win32 + {7B823C96-860C-4578-95EE-1087A45AF1AA}.Release|Win32.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |