From 7b540f022699e6e2b2e115d13526e12060a5fc9f Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@duempel.org>
Date: Sun, 5 Jan 2014 01:28:36 +0100
Subject: [PATCH] event/MultiSocketMonitor: add method ReplaceSocketList()

Move code from AlsaMixerPlugin.
---
 src/event/MultiSocketMonitor.cxx | 30 ++++++++++++++++++++++++++++++
 src/event/MultiSocketMonitor.hxx | 12 ++++++++++++
 src/mixer/AlsaMixerPlugin.cxx    | 19 +------------------
 3 files changed, 43 insertions(+), 18 deletions(-)

diff --git a/src/event/MultiSocketMonitor.cxx b/src/event/MultiSocketMonitor.cxx
index 39b6f3e0d..b5114cfb3 100644
--- a/src/event/MultiSocketMonitor.cxx
+++ b/src/event/MultiSocketMonitor.cxx
@@ -20,6 +20,10 @@
 #include "config.h"
 #include "MultiSocketMonitor.hxx"
 
+#ifndef WIN32
+#include <poll.h>
+#endif
+
 MultiSocketMonitor::MultiSocketMonitor(EventLoop &_loop)
 	:IdleMonitor(_loop), TimeoutMonitor(_loop), ready(false) {
 }
@@ -29,6 +33,32 @@ MultiSocketMonitor::~MultiSocketMonitor()
 	// TODO
 }
 
+#ifndef WIN32
+
+void
+MultiSocketMonitor::ReplaceSocketList(pollfd *pfds, unsigned n)
+{
+	pollfd *const end = pfds + n;
+
+	UpdateSocketList([pfds, end](int fd) -> unsigned {
+			auto i = std::find_if(pfds, end, [fd](const struct pollfd &pfd){
+					return pfd.fd == fd;
+				});
+			if (i == end)
+				return 0;
+
+			auto events = i->events;
+			i->events = 0;
+			return events;
+		});
+
+	for (auto i = pfds; i != end; ++i)
+		if (i->events != 0)
+			AddSocket(i->fd, i->events);
+}
+
+#endif
+
 void
 MultiSocketMonitor::Prepare()
 {
diff --git a/src/event/MultiSocketMonitor.hxx b/src/event/MultiSocketMonitor.hxx
index d1cac880a..6c76d667f 100644
--- a/src/event/MultiSocketMonitor.hxx
+++ b/src/event/MultiSocketMonitor.hxx
@@ -39,6 +39,10 @@
 #endif
 #endif
 
+#ifndef WIN32
+struct pollfd;
+#endif
+
 class EventLoop;
 
 /**
@@ -135,6 +139,14 @@ public:
 		}
 	}
 
+#ifndef WIN32
+	/**
+	 * Replace the socket list with the given file descriptors.
+	 * The given pollfd array will be modified by this method.
+	 */
+	void ReplaceSocketList(pollfd *pfds, unsigned n);
+#endif
+
 protected:
 	/**
 	 * @return timeout [ms] or -1 for no timeout
diff --git a/src/mixer/AlsaMixerPlugin.cxx b/src/mixer/AlsaMixerPlugin.cxx
index 2691f9406..f0d9b8007 100644
--- a/src/mixer/AlsaMixerPlugin.cxx
+++ b/src/mixer/AlsaMixerPlugin.cxx
@@ -104,24 +104,7 @@ AlsaMixerMonitor::PrepareSockets()
 	if (count < 0)
 		count = 0;
 
-	struct pollfd *end = pfds + count;
-
-	UpdateSocketList([pfds, end](int fd) -> unsigned {
-			auto i = std::find_if(pfds, end, [fd](const struct pollfd &pfd){
-					return pfd.fd == fd;
-				});
-			if (i == end)
-				return 0;
-
-			auto events = i->events;
-			i->events = 0;
-			return events;
-		});
-
-	for (auto i = pfds; i != end; ++i)
-		if (i->events != 0)
-			AddSocket(i->fd, i->events);
-
+	ReplaceSocketList(pfds, count);
 	return -1;
 }