summaryrefslogtreecommitdiff
path: root/plugins/FavContacts/src/cserver.cpp
diff options
context:
space:
mode:
authorKirill Volinsky <mataes2007@gmail.com>2012-05-17 17:37:22 +0000
committerKirill Volinsky <mataes2007@gmail.com>2012-05-17 17:37:22 +0000
commit78d71d2cad6f243c6ff31d41380b8c5b58407de5 (patch)
treed0c05983b315352c5e66d23420da4b8fd8b5aff4 /plugins/FavContacts/src/cserver.cpp
parenta9e8daee448c229aa3f8ded0c5f5c0fe7aa42529 (diff)
added some plugins
git-svn-id: http://svn.miranda-ng.org/main/trunk@20 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'plugins/FavContacts/src/cserver.cpp')
-rw-r--r--plugins/FavContacts/src/cserver.cpp82
1 files changed, 82 insertions, 0 deletions
diff --git a/plugins/FavContacts/src/cserver.cpp b/plugins/FavContacts/src/cserver.cpp
new file mode 100644
index 0000000000..957d1a6048
--- /dev/null
+++ b/plugins/FavContacts/src/cserver.cpp
@@ -0,0 +1,82 @@
+#include <winsock2.h>
+#include <windows.h>
+#include <ws2tcpip.h>
+
+#include "csocket.h"
+#include "cserver.h"
+
+void CServer::Start(int port, IConnectionProcessorFactory *connectionProcessorFactory, bool background)
+{
+ m_socket = socket(AF_INET, SOCK_STREAM, 0);
+ if (m_socket == INVALID_SOCKET) return;
+
+ sockaddr_in addr = {0};
+ addr.sin_family = AF_INET;
+ addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ addr.sin_port = htons((WORD)port);
+ if (bind(m_socket, (sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR)
+ {
+ closesocket(m_socket);
+ m_socket = INVALID_SOCKET;
+ return;
+ }
+
+ listen(m_socket, SOMAXCONN);
+
+ m_connectionProcessorFactory = connectionProcessorFactory;
+
+ if (background)
+ {
+ CreateThread(NULL, 0,
+ GlobalConnectionAcceptThread,
+ this,
+ 0, NULL);
+ } else
+ {
+ ConnectionAcceptThread();
+ }
+}
+
+void CServer::Stop()
+{
+ shutdown(m_socket, SD_BOTH);
+ closesocket(m_socket);
+}
+
+DWORD CServer::ConnectionAcceptThread()
+{
+ while (1)
+ {
+ SOCKET s = accept(m_socket, NULL, NULL);
+ if (s == INVALID_SOCKET) break;
+
+ CreateThread(NULL, 0,
+ GlobalConnectionProcessThread,
+ new GlobalConnectionProcessThreadArgs(this, s),
+ 0, NULL);
+ }
+ return 0;
+}
+
+DWORD CServer::ConnectionProcessThread(SOCKET s)
+{
+ CSocket sock(s);
+ IConnectionProcessor *processor = m_connectionProcessorFactory->Create(&sock);
+ processor->ProcessConnection();
+ delete processor;
+ return 0;
+}
+
+DWORD WINAPI CServer::GlobalConnectionAcceptThread(void *arg)
+{
+ CServer *server = (CServer *)arg;
+ return server->ConnectionAcceptThread();
+}
+
+DWORD WINAPI CServer::GlobalConnectionProcessThread(void *arg)
+{
+ GlobalConnectionProcessThreadArgs *args = (GlobalConnectionProcessThreadArgs *)arg;
+ DWORD result = args->m_server->ConnectionProcessThread(args->m_socket);
+ delete args;
+ return result;
+}