[Assorted-commits] SF.net SVN: assorted: [744] cpp-commons/trunk/src/commons
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-05-08 19:29:49
|
Revision: 744 http://assorted.svn.sourceforge.net/assorted/?rev=744&view=rev Author: yangzhang Date: 2008-05-08 12:29:35 -0700 (Thu, 08 May 2008) Log Message: ----------- added support for libtask Added Paths: ----------- cpp-commons/trunk/src/commons/task/ cpp-commons/trunk/src/commons/task/net.h cpp-commons/trunk/src/commons/task/task.h Added: cpp-commons/trunk/src/commons/task/net.h =================================================================== --- cpp-commons/trunk/src/commons/task/net.h (rev 0) +++ cpp-commons/trunk/src/commons/task/net.h 2008-05-08 19:29:35 UTC (rev 744) @@ -0,0 +1,97 @@ +#ifndef COMMONS_TASK_NET_H +#define COMMONS_TASK_NET_H + +#include <task.h> + +namespace commons +{ + + /** + * A simple file-descriptor. + */ + class fd + { + private: + int f; + + public: + fd(int f) : f(f) {} + + /** + * Send data. + * \bug Unsafe demotion on \p len. + * \exception check_exception Write failed; not everything was written. + */ + void + write(char *data, size_t len) + { + check(fdwrite(f, data, len) == (int)len); + } + + /** + * Read data. + * \bug Unsafe demotion on \p len. Unsafe demotion on return val. + * \exception check_exception Read failed. + */ + int + read(char *data, size_t len) + { + int res = fdread(f, data, len); + check(res >= 0); + return res; + } + +// /** +// * Read data until \p len. +// * \bug Unsafe demotion on \p len. +// * \exception check_exception Read failed. +// */ +// int +// read(char *data, size_t len) +// { +// while (true) { +// int count = fdread(f, data, len); +// check(count > 0); +// len -= count; +// } +// } + + }; + + typedef fd socket; + + /** + * A TCP listener. + */ + class tcplistener + { + private: + int fd; + + public: + /** + * Bind to the given host and port and start listening. + * \param[in] host If NULL, then bind to all local addresses. + * \param[in] port The port to bind to. + * \exception check_exception Call to netannounce() failed. + */ + tcplistener(char *host, int port) : fd(-1) + { + fd = netannounce(TCP, host, port); + check(fd); + } + + /** + * Accept a connection, ignoring the remote socket address. + * \return The client connection socket. + */ + socket accept() + { + int cli = netaccept(fd, NULL, NULL); + return socket(cli); + } + }; + +} + +#endif Added: cpp-commons/trunk/src/commons/task/task.h =================================================================== --- cpp-commons/trunk/src/commons/task/task.h (rev 0) +++ cpp-commons/trunk/src/commons/task/task.h 2008-05-08 19:29:35 UTC (rev 744) @@ -0,0 +1,21 @@ +#ifndef COMMONS_TASK_H +#define COMMONS_TASK_H + +#include <commons/boost/delegates.h> + +namespace commons +{ + + const unsigned int default_stack = 32768; + + int + spawntask(const function0<void>& f) + { + return taskcreate(&run_function0, + (void*) new function0<void>(f), + default_stack); + } + +} + +#endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |