[Assorted-commits] SF.net SVN: assorted: [745] cpp-commons/trunk/src/commons
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-05-08 19:29:52
|
Revision: 745 http://assorted.svn.sourceforge.net/assorted/?rev=745&view=rev Author: yangzhang Date: 2008-05-08 12:29:55 -0700 (Thu, 08 May 2008) Log Message: ----------- added support for state threads (ST) Added Paths: ----------- cpp-commons/trunk/src/commons/st/ cpp-commons/trunk/src/commons/st/st.h Added: cpp-commons/trunk/src/commons/st/st.h =================================================================== --- cpp-commons/trunk/src/commons/st/st.h (rev 0) +++ cpp-commons/trunk/src/commons/st/st.h 2008-05-08 19:29:55 UTC (rev 745) @@ -0,0 +1,118 @@ +#ifndef COMMONS_ST_ST_H +#define COMMONS_ST_ST_H + +#include <st.h> +#include <stx.h> +#include <commons/sockets.h> +#include <commons/boost/delegates.h> +#include <boost/function.hpp> + +namespace commons +{ + using namespace boost; + + enum { default_stack_size = 65536 }; + + /** + * RAII to st_netfd_close() a netfd. + */ + class st_closing + { + public: + st_closing(st_netfd_t x) : attached(true), x(x) {} + ~st_closing() { if (attached) st_netfd_close(x); } + void detach() { attached = false; } + private: + bool attached; + st_netfd_t x; + }; + + /** + * non-copyable. + * \todo remove? this seems to be bad if it closes the file! + */ + class stfd + { + public: + stfd(st_netfd_t fd) : fd_(fd), sclose(fd) {} + const st_netfd_t fd() const { return fd_; } + private: + const st_netfd_t fd_; + st_closing sclose; + }; + + /** + * Run a function in pthread. + * \return The new pthread_t on success, 0 on failure. + * \todo Is it safe to treat the pthread_t as a pointer? + */ + st_thread_t + st_spawn(const function0<void>& f) + { + return st_thread_create(&run_function0_null, + (void*) new function0<void>(f), + true, + default_stack_size); + } + + /** + * Connect to a TCP socket address. + * \param[in] host Either an IP address or hostname. + * \param[in] port The destination port. + * \param[in] timeout The timeout for each of the DNS lookup and the connect + * operation. + * \todo Create variants that take or return the sockaddr. + */ + st_netfd_t + st_tcp_connect(char *host, int port, st_utime_t timeout) + { + // Create remote socket address. + struct sockaddr_in sa; + bzero(&sa, sizeof sa); + sa.sin_family = AF_INET; + sa.sin_port = htons(port); + + // First try to parse as IP address. Note: inet_addr() is obsolete. + if (inet_aton(host, &sa.sin_addr) != 0) { + // Then look up by hostname. + check0x(stx_dns_getaddr(host, &sa.sin_addr, timeout)); + } + + // Create the socket. + int sfd = checknneg(socket(PF_INET, SOCK_STREAM, 0)); + st_netfd_t nfd = st_netfd_open_socket(sfd); + + // Connect. + try { + check0x(st_connect(nfd, (struct sockaddr*) &sa, sizeof sa, timeout)); + return nfd; + } catch (...) { + st_netfd_close(nfd); + throw; + } + } + + /** + * Create a listener st_netfd_t. + * \param[in] port The port to listen on. + * \return The st_netfd_t. + */ + st_netfd_t + st_tcp_listen(int port) + { + int sfd = tcp_listen(port); + try { + // Create a net file descriptor around a listener socket. + st_netfd_t nfd = st_netfd_open_socket(sfd); + checkpass(nfd); + //st_netfd_t nfd = checkpass(st_netfd_open_socket(sfd)); + return nfd; + } catch (...) { + close(sfd); + throw; + } + } + +} + +#endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |