diff C++/SocketListener.h @ 170:fd138f2a9773

Add C++ portable sockets
author David Demelier <markand@malikania.fr>
date Tue, 10 Sep 2013 15:17:56 +0200
parents
children ce3e1c3d6fed
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/C++/SocketListener.h	Tue Sep 10 15:17:56 2013 +0200
@@ -0,0 +1,76 @@
+/*
+ * SocketListener.h -- portable select() wrapper
+ *
+ * Copyright (c) 2013, David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef _SOCKET_LISTENER_H_
+#define _SOCKET_LISTENER_H_
+
+#include <vector>
+
+#include "Socket.h"
+
+/**
+ * @class SocketTimeout
+ * @brief thrown when a timeout occured
+ */
+class SocketTimeout : public std::exception
+{
+public:
+	virtual const char * what(void) const throw();
+};
+
+class SocketListener
+{
+public:
+
+private:
+	std::vector<Socket> m_clients;
+
+public:
+	/**
+	 * Add a socket to listen to.
+	 *
+	 * @param s the socket
+	 */
+	void add(Socket &s);
+
+	/**
+	 * Remove a socket from the list.
+	 *
+	 * @param s the socket
+	 */
+	void remove(Socket &s);
+
+	/**
+	 * Remove every sockets in the listener.
+	 */
+	void clear();
+
+	/**
+	 * Wait for an event in the socket list. If both s and us are set to 0 then
+	 * it waits indefinitely.
+	 *
+	 * @param s the timeout in seconds
+	 * @param us the timeout in milliseconds
+	 * @return the socket ready
+	 * @throw SocketError on error
+	 * @throw SocketTimeout on timeout
+	 */
+	Socket &select(int s = 0, int us = 0);
+};
+
+#endif // !_SOCKET_LISTENER_H_