diff options
author | Luo Jinghua <sunmoon1997@gmail.com> | 2011-11-19 20:58:15 +0800 |
---|---|---|
committer | Luo Jinghua <sunmoon1997@gmail.com> | 2011-11-19 20:58:15 +0800 |
commit | 7dfaab23ccb4f2d6ec025cd3c133cc1a7fc2ea04 (patch) | |
tree | aee9dc1180567aeccab56df2ca89e027f1489154 | |
parent | 739782259fb31d1b56fa6a688857a21566374e1a (diff) |
SexyAppFramework: Added a simple socket wrapper
-rw-r--r-- | osframework/source/SexyAppFramework/SexySocket.cpp | 384 | ||||
-rw-r--r-- | osframework/source/SexyAppFramework/SexySocket.h | 270 |
2 files changed, 654 insertions, 0 deletions
diff --git a/osframework/source/SexyAppFramework/SexySocket.cpp b/osframework/source/SexyAppFramework/SexySocket.cpp new file mode 100644 index 0000000..28f81ce --- /dev/null +++ b/osframework/source/SexyAppFramework/SexySocket.cpp @@ -0,0 +1,384 @@ +#include "SexySocket.h" + +#ifdef WIN32 +#include <winsock.h> // For socket(), connect(), send(), and recv() +#define socklen_t int +typedef char raw_type; // Type used for raw data on this platform + +#define errno WSAGetLastError() +#define EINTR WSAEINTR +#define EINPROGRESS WSAEINPROGRESS +#define EWOULDBLOCK WSAEWOULDBLOCK +#define EAFNOSUPPORT WSAEAFNOSUPPORT +#else +#include <sys/types.h> // For data types +#include <sys/socket.h> // For socket(), connect(), send(), and recv() +#include <netdb.h> // For gethostbyname() +#include <arpa/inet.h> // For inet_addr() +#include <unistd.h> // For close() +#include <netinet/in.h> // For sockaddr_in +typedef void raw_type; // Type used for raw data on this platform + +#include <errno.h> // For errno +#endif + +#include <string.h> +#include <stdlib.h> + +using namespace std; + +#ifdef WIN32 +static bool initialized = false; +#endif + +using namespace Sexy; + +// Function to fill in address structure given an address and port +static bool fillAddr(const string &address, unsigned short port, + sockaddr_in &addr) +{ + memset(&addr, 0, sizeof(addr)); // Zero out address structure + addr.sin_family = AF_INET; // Internet address + + hostent *host; // Resolve name + host = gethostbyname(address.c_str()); + if (!host) + return false; + addr.sin_addr.s_addr = *((unsigned long *) host->h_addr_list[0]); + addr.sin_port = htons(port); // Assign port in network byte order +} + +// Socket Code + +Socket::Socket(int type, int protocol) +{ +#ifdef WIN32 + if (!initialized) + { + WORD wVersionRequested; + WSADATA wsaData; + + wVersionRequested = MAKEWORD(2, 0); // Request WinSock v2.0 + WSAStartup(wVersionRequested, &wsaData); + initialized = true; + } +#endif + + // Make a new socket + mSock = socket(PF_INET, type, protocol); + mHasError = mSock >=0; +} + +Socket::Socket(int sockDesc) +{ + this->mSock = sockDesc; +} + +Socket::~Socket() { +#ifdef WIN32 + ::closesocket(mSock); +#else + ::close(mSock); +#endif + mSock = -1; +} + +bool Socket::hasError() +{ + return mHasError; +} + +string Socket::getLocalAddress() +{ + sockaddr_in addr; + unsigned int addr_len = sizeof(addr); + + if (getsockname(mSock, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) + return ""; + return inet_ntoa(addr.sin_addr); +} + +unsigned Socket::getLocalPort() +{ + sockaddr_in addr; + unsigned int addr_len = sizeof(addr); + + if (getsockname(mSock, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) + return -1; + return ntohs(addr.sin_port); +} + +bool Socket::setLocalPort(unsigned short localPort) +{ + // Bind the socket to its port + sockaddr_in localAddr; + memset(&localAddr, 0, sizeof(localAddr)); + localAddr.sin_family = AF_INET; + localAddr.sin_addr.s_addr = htonl(INADDR_ANY); + localAddr.sin_port = htons(localPort); + + if (bind(mSock, (sockaddr *) &localAddr, sizeof(sockaddr_in)) < 0) + return false; + + return true; +} + +bool Socket::setLocalAddressAndPort(const string &localAddress, + unsigned short localPort) +{ + // Get the address of the requested host + sockaddr_in localAddr; + fillAddr(localAddress, localPort, localAddr); + + if (bind(mSock, (sockaddr *) &localAddr, sizeof(sockaddr_in)) < 0) + return false; + + return true; +} + +void Socket::cleanup() +{ +#ifdef WIN32 + if (initialized) + { + WSACleanup(); + initialized = false; + } +#endif +} + +unsigned short Socket::resolveService(const string &service, + const string &protocol) +{ + struct servent *serv; /* Structure containing service information */ + + serv = getservbyname(service.c_str(), protocol.c_str()); + if (serv == NULL) + return atoi(service.c_str()); /* Service is port number */ + return ntohs(serv->s_port); /* Found port (network byte order) by name */ +} + +// CommunicatingSocket Code + +CommunicatingSocket::CommunicatingSocket(int type, int protocol) + : Socket(type, protocol) +{ +} + +CommunicatingSocket::CommunicatingSocket(int newConnSD) : Socket(newConnSD) +{ +} + +bool CommunicatingSocket::connect(const string &foreignAddress, + unsigned short foreignPort) +{ + // Get the address of the requested host + sockaddr_in destAddr; + fillAddr(foreignAddress, foreignPort, destAddr); + + // Try to connect to the given port + if (::connect(mSock, (sockaddr *) &destAddr, sizeof(destAddr)) < 0) + return false; + return true; +} + +bool CommunicatingSocket::send(const void *buffer, int bufferLen) +{ + if (::send(mSock, (raw_type *) buffer, bufferLen, 0) < 0) + return false; + return true; +} + +int CommunicatingSocket::recv(void *buffer, int bufferLen) +{ + int ret; + ret = ::recv(mSock, (raw_type *) buffer, bufferLen, 0); + if (ret < 0) + return -1; + return ret; +} + +string CommunicatingSocket::getForeignAddress() +{ + sockaddr_in addr; + unsigned int addr_len = sizeof(addr); + + if (getpeername(mSock, (sockaddr *) &addr,(socklen_t *) &addr_len) < 0) + return ""; + return inet_ntoa(addr.sin_addr); +} + +unsigned CommunicatingSocket::getForeignPort() +{ + sockaddr_in addr; + unsigned int addr_len = sizeof(addr); + + if (getpeername(mSock, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) + return -1; + return ntohs(addr.sin_port); +} + +// TCPSocket Code + +TCPSocket::TCPSocket() : CommunicatingSocket(SOCK_STREAM, IPPROTO_TCP) +{ +} + +TCPSocket::TCPSocket(const string &foreignAddress, unsigned short foreignPort) + : CommunicatingSocket(SOCK_STREAM, IPPROTO_TCP) +{ + connect(foreignAddress, foreignPort); +} + +TCPSocket::TCPSocket(int newConnSD) : CommunicatingSocket(newConnSD) +{ +} + +// TCPServerSocket Code + +TCPServerSocket::TCPServerSocket(unsigned short localPort, int queueLen) + : Socket(SOCK_STREAM, IPPROTO_TCP) +{ + setLocalPort(localPort); + setListen(queueLen); +} + +TCPServerSocket::TCPServerSocket(const string &localAddress, + unsigned short localPort, int queueLen) + : Socket(SOCK_STREAM, IPPROTO_TCP) +{ + setLocalAddressAndPort(localAddress, localPort); + setListen(queueLen); +} + +TCPSocket *TCPServerSocket::accept() +{ + int newConnSD; + + newConnSD = ::accept(mSock, NULL, 0); + if (newConnSD < 0) + return 0; + return new TCPSocket(newConnSD); +} + +bool TCPServerSocket::setListen(int queueLen) +{ + if (listen(mSock, queueLen) < 0) + return false; + return true; +} + +// UDPSocket Code + +UDPSocket::UDPSocket() : + CommunicatingSocket(SOCK_DGRAM, IPPROTO_UDP) +{ + setBroadcast(); +} + +UDPSocket::UDPSocket(unsigned short localPort) : + CommunicatingSocket(SOCK_DGRAM, IPPROTO_UDP) +{ + setLocalPort(localPort); + setBroadcast(); +} + +UDPSocket::UDPSocket(const string &localAddress, unsigned short localPort) + : CommunicatingSocket(SOCK_DGRAM, IPPROTO_UDP) +{ + setLocalAddressAndPort(localAddress, localPort); + setBroadcast(); +} + +void UDPSocket::setBroadcast() +{ + // If this fails, we'll hear about it when we try to send. This will allow + // system that cannot broadcast to continue if they don't plan to broadcast + int broadcastPermission = 1; + setsockopt(mSock, SOL_SOCKET, SO_BROADCAST, + (raw_type *) &broadcastPermission, sizeof(broadcastPermission)); +} + +bool UDPSocket::disconnect() +{ + sockaddr_in nullAddr; + memset(&nullAddr, 0, sizeof(nullAddr)); + nullAddr.sin_family = AF_UNSPEC; + + // Try to disconnect + if (::connect(mSock, (sockaddr *) &nullAddr, sizeof(nullAddr)) < 0) { + if (errno != EAFNOSUPPORT) + return false; + } + + return true; +} + +bool UDPSocket::sendTo(const void *buffer, int bufferLen, + const string &foreignAddress, unsigned short foreignPort) +{ + sockaddr_in destAddr; + fillAddr(foreignAddress, foreignPort, destAddr); + + // Write out the whole buffer as a single message. + if (sendto(mSock, (raw_type *) buffer, bufferLen, 0, + (sockaddr *) &destAddr, sizeof(destAddr)) != bufferLen) + return false; + + return true; +} + +int UDPSocket::recvFrom(void *buffer, int bufferLen, string &sourceAddress, + unsigned short &sourcePort) +{ + sockaddr_in clntAddr; + socklen_t addrLen = sizeof(clntAddr); + int ret; + + ret = recvfrom(mSock, (raw_type *) buffer, bufferLen, 0, + (sockaddr *) &clntAddr, (socklen_t *) &addrLen); + if (ret < 0) + return -1; + + sourceAddress = inet_ntoa(clntAddr.sin_addr); + sourcePort = ntohs(clntAddr.sin_port); + + return ret; +} + +bool UDPSocket::setMulticastTTL(unsigned char multicastTTL) +{ + if (setsockopt(mSock, IPPROTO_IP, IP_MULTICAST_TTL, + (raw_type *) &multicastTTL, sizeof(multicastTTL)) < 0) + return false; + + return true; +} + +bool UDPSocket::joinGroup(const string &multicastGroup) +{ + struct ip_mreq multicastRequest; + + multicastRequest.imr_multiaddr.s_addr = inet_addr(multicastGroup.c_str()); + multicastRequest.imr_interface.s_addr = htonl(INADDR_ANY); + if (setsockopt(mSock, IPPROTO_IP, IP_ADD_MEMBERSHIP, + (raw_type *) &multicastRequest, + sizeof(multicastRequest)) < 0) + return false; + + return true; +} + +bool UDPSocket::leaveGroup(const string &multicastGroup) +{ + struct ip_mreq multicastRequest; + + multicastRequest.imr_multiaddr.s_addr = inet_addr(multicastGroup.c_str()); + multicastRequest.imr_interface.s_addr = htonl(INADDR_ANY); + if (setsockopt(mSock, IPPROTO_IP, IP_DROP_MEMBERSHIP, + (raw_type *) &multicastRequest, + sizeof(multicastRequest)) < 0) + return false; + + return true; +} diff --git a/osframework/source/SexyAppFramework/SexySocket.h b/osframework/source/SexyAppFramework/SexySocket.h new file mode 100644 index 0000000..de13449 --- /dev/null +++ b/osframework/source/SexyAppFramework/SexySocket.h @@ -0,0 +1,270 @@ +#ifndef __SEXY_SOCKET_H__ +#define __SEXY_SOCKET_H__ + +#include <string> // For string + +using namespace std; + +namespace Sexy { + +/** + * Base class representing basic communication endpoint + */ + class Socket { + public: + /** + * Close and deallocate this socket + */ + ~Socket(); + + /** + * Check the socket whether is in error state + * @return true if the socket is in error state + */ + bool hasError(); + + /** + * Get the local address + * @return local address of socket + */ + string getLocalAddress(); + + /** + * Get the local port + * @return local port of socket + */ + unsigned getLocalPort(); + + /** + * Set the local port to the specified port and the local address + * to any interface + * @param localPort local port + */ + bool setLocalPort(unsigned short localPort); + + /** + * Set the local port to the specified port and the local address + * to the specified address. If you omit the port, a random port + * will be selected. + * @param localAddress local address + * @param localPort local port + */ + bool setLocalAddressAndPort(const string &localAddress, + unsigned short localPort = 0); + + /** + * If WinSock, unload the WinSock DLLs; otherwise do nothing. We ignore + * this in our sample client code but include it in the library for + * completeness. If you are running on Windows and you are concerned + * about DLL resource consumption, call this after you are done with all + * Socket instances. If you execute this on Windows while some instance of + * Socket exists, you are toast. For portability of client code, this is + * an empty function on non-Windows platforms so you can always include it. + */ + static void cleanup(); + + /** + * Resolve the specified service for the specified protocol to the + * corresponding port number in host byte order + * @param service service to resolve (e.g., "http") + * @param protocol protocol of service to resolve. Default is "tcp". + */ + static unsigned short resolveService(const string &service, + const string &protocol = "tcp"); + + private: + // Prevent the user from trying to use value semantics on this object + Socket(const Socket &sock); + void operator=(const Socket &sock); + + protected: + int mSock; // Socket descriptor + bool mHasError; + Socket(int type, int protocol); + Socket(int sock); + }; + + /** + * Socket which is able to connect, send, and receive + */ + class CommunicatingSocket : public Socket { + public: + /** + * Establish a socket connection with the given foreign + * address and port + * @param foreignAddress foreign address (IP address or name) + * @param foreignPort foreign port + */ + bool connect(const string &foreignAddress, unsigned short foreignPort); + + /** + * Write the given buffer to this socket. Call connect() before + * calling send() + * @param buffer buffer to be written + * @param bufferLen number of bytes from buffer to be written + */ + bool send(const void *buffer, int bufferLen); + + /** + * Read into the given buffer up to bufferLen bytes data from this + * socket. Call connect() before calling recv() + * @param buffer buffer to receive the data + * @param bufferLen maximum number of bytes to read into buffer + * @return number of bytes read, 0 for EOF, and -1 for error + */ + int recv(void *buffer, int bufferLen); + + /** + * Get the foreign address. Call connect() before calling recv() + * @return foreign address + */ + string getForeignAddress(); + + /** + * Get the foreign port. Call connect() before calling recv() + * @return foreign port + */ + unsigned getForeignPort(); + + protected: + CommunicatingSocket(int type, int protocol); + CommunicatingSocket(int newConnSD); + }; + + /** + * TCP socket for communication with other TCP sockets + */ + class TCPSocket : public CommunicatingSocket { + public: + /** + * Construct a TCP socket with no connection + */ + TCPSocket(); + + /** + * Construct a TCP socket with a connection to the given foreign address + * and port + * @param foreignAddress foreign address (IP address or name) + * @param foreignPort foreign port + */ + TCPSocket(const string &foreignAddress, unsigned short foreignPort); + + private: + // Access for TCPServerSocket::accept() connection creation + friend class TCPServerSocket; + TCPSocket(int newConnSD); + }; + + /** + * TCP socket class for servers + */ + class TCPServerSocket : public Socket { + public: + /** + * Construct a TCP socket for use with a server, accepting connections + * on the specified port on any interface + * @param localPort local port of server socket, a value of zero will + * give a system-assigned unused port + * @param queueLen maximum queue length for outstanding + * connection requests (default 5) + */ + TCPServerSocket(unsigned short localPort, int queueLen = 5); + + /** + * Construct a TCP socket for use with a server, accepting connections + * on the specified port on the interface specified by the given address + * @param localAddress local interface (address) of server socket + * @param localPort local port of server socket + * @param queueLen maximum queue length for outstanding + * connection requests (default 5) + */ + TCPServerSocket(const string &localAddress, unsigned short localPort, + int queueLen = 5); + + /** + * Blocks until a new connection is established on this socket or error + * @return new connection socket + */ + TCPSocket *accept(); + + private: + bool setListen(int queueLen); + }; + + /** + * UDP socket class + */ + class UDPSocket : public CommunicatingSocket { + public: + /** + * Construct a UDP socket + */ + UDPSocket(); + + /** + * Construct a UDP socket with the given local port + * @param localPort local port + */ + UDPSocket(unsigned short localPort); + + /** + * Construct a UDP socket with the given local port and address + * @param localAddress local address + * @param localPort local port + */ + UDPSocket(const string &localAddress, unsigned short localPort); + + /** + * Unset foreign address and port + * @return true if disassociation is successful + */ + bool disconnect(); + + /** + * Send the given buffer as a UDP datagram to the + * specified address/port + * @param buffer buffer to be written + * @param bufferLen number of bytes to write + * @param foreignAddress address (IP address or name) to send to + * @param foreignPort port number to send to + * @return true if send is successful + */ + bool sendTo(const void *buffer, int bufferLen, const string &foreignAddress, + unsigned short foreignPort); + + /** + * Read read up to bufferLen bytes data from this socket. The given buffer + * is where the data will be placed + * @param buffer buffer to receive data + * @param bufferLen maximum number of bytes to receive + * @param sourceAddress address of datagram source + * @param sourcePort port of data source + * @return number of bytes received and -1 for error + */ + int recvFrom(void *buffer, int bufferLen, string &sourceAddress, + unsigned short &sourcePort); + + /** + * Set the multicast TTL + * @param multicastTTL multicast TTL + */ + bool setMulticastTTL(unsigned char multicastTTL); + + /** + * Join the specified multicast group + * @param multicastGroup multicast group address to join + */ + bool joinGroup(const string &multicastGroup); + + /** + * Leave the specified multicast group + * @param multicastGroup multicast group address to leave + */ + bool leaveGroup(const string &multicastGroup); + + private: + void setBroadcast(); + }; + +} +#endif |