]> git.sesse.net Git - casparcg/blob - protocol/util/AsyncEventServer.h
2.0.2: Is now locked, only bug fixes should be added. New features go into 2.1.0.
[casparcg] / protocol / util / AsyncEventServer.h
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\r
6 * CasparCG is free software: you can redistribute it and/or modify\r
7 * it under the terms of the GNU General Public License as published by\r
8 * the Free Software Foundation, either version 3 of the License, or\r
9 * (at your option) any later version.\r
10 *\r
11 * CasparCG is distributed in the hope that it will be useful,\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 * GNU General Public License for more details.\r
15 *\r
16 * You should have received a copy of the GNU General Public License\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 * Author: Nicklas P Andersson\r
20 */\r
21 \r
22  \r
23 // AsyncEventServer.h: interface for the AsyncServer class.\r
24 //////////////////////////////////////////////////////////////////////\r
25 \r
26 #if !defined(AFX_ASYNCEVENTSERVER_H__0BFA29CB_BE4C_46A0_9CAE_E233ED27A8EC__INCLUDED_)\r
27 #define AFX_ASYNCEVENTSERVER_H__0BFA29CB_BE4C_46A0_9CAE_E233ED27A8EC__INCLUDED_\r
28 \r
29 #if _MSC_VER > 1000\r
30 #pragma once\r
31 #endif\r
32 \r
33 #include <common/memory/safe_ptr.h>\r
34 \r
35 #include <string>\r
36 #include <map>\r
37 #include <vector>\r
38 #include <functional>\r
39 \r
40 #include "..\util\thread.h"\r
41 \r
42 #include "ProtocolStrategy.h"\r
43 \r
44 #include <tbb\mutex.h>\r
45 \r
46 namespace caspar {\r
47 namespace IO {\r
48 \r
49 class SocketInfo;\r
50 typedef std::shared_ptr<SocketInfo> SocketInfoPtr;\r
51 \r
52 typedef std::function<void(caspar::IO::SocketInfoPtr)> ClientDisconnectEvent;\r
53 \r
54 class AsyncEventServer : public IRunnable\r
55 {\r
56         static long instanceCount_;\r
57 \r
58         AsyncEventServer();\r
59         AsyncEventServer(const AsyncEventServer&);\r
60         AsyncEventServer& operator=(const AsyncEventServer&);\r
61 \r
62 public:\r
63         explicit AsyncEventServer(const safe_ptr<IProtocolStrategy>& pProtocol, int port);\r
64         ~AsyncEventServer();\r
65 \r
66         bool Start();\r
67         void SetProtocolStrategy(safe_ptr<IProtocolStrategy> pPS) {\r
68                 pProtocolStrategy_ = pPS;\r
69         }\r
70 \r
71         void Stop();\r
72 \r
73         void SetClientDisconnectHandler(ClientDisconnectEvent handler);\r
74         \r
75 private:\r
76         Thread  listenThread_;\r
77         void Run(HANDLE stopEvent);\r
78         bool OnUnhandledException(const std::exception&) throw();\r
79 \r
80         bool OnAccept(SocketInfoPtr&);\r
81         bool OnRead(SocketInfoPtr&);\r
82         void OnWrite(SocketInfoPtr&);\r
83         void OnClose(SocketInfoPtr&);\r
84         void OnError(HANDLE waitEvent, int errorCode);\r
85 \r
86         SocketInfoPtr           pListenSocketInfo_;\r
87         safe_ptr<IProtocolStrategy>     pProtocolStrategy_;\r
88         int                                     port_;\r
89 \r
90         friend class SocketInfo;\r
91         void DoSend(SocketInfo&);\r
92         void DisconnectClient(SocketInfo&);\r
93 \r
94         void LogSocketError(const TCHAR* pStr, int socketError = 0);\r
95 \r
96         class SocketInfoCollection\r
97         {\r
98                 SocketInfoCollection(const SocketInfoCollection&);\r
99                 SocketInfoCollection& operator=(const SocketInfoCollection&);\r
100 \r
101                 typedef std::map<HANDLE, SocketInfoPtr> SocketInfoMap;\r
102                 typedef std::vector<HANDLE> HandleVector;\r
103 \r
104         public:\r
105                 SocketInfoCollection();\r
106                 ~SocketInfoCollection();\r
107 \r
108                 bool AddSocketInfo(SocketInfoPtr& pSocketInfo);\r
109                 void RemoveSocketInfo(SocketInfoPtr& pSocketInfo);\r
110                 void RemoveSocketInfo(HANDLE);\r
111                 void CopyCollectionToArray(HANDLE*, int maxCount);\r
112 \r
113                 bool FindSocketInfo(HANDLE, SocketInfoPtr& pResult);\r
114 \r
115                 bool IsDirty() {\r
116                         return bDirty_;\r
117                 }\r
118                 void ClearDirty() {\r
119                         bDirty_ = false;\r
120                 }\r
121 \r
122                 std::size_t Size() {\r
123                         return waitEvents_.size();\r
124                 }\r
125                 void Clear();\r
126 \r
127                 ClientDisconnectEvent onSocketInfoRemoved;\r
128 \r
129         private:\r
130                 SocketInfoMap socketInfoMap_;\r
131                 HandleVector waitEvents_;\r
132                 bool bDirty_;\r
133                 tbb::mutex mutex_;\r
134         };\r
135         SocketInfoCollection socketInfoCollection_;\r
136         tbb::mutex mutex_;\r
137 };\r
138 typedef std::tr1::shared_ptr<AsyncEventServer> AsyncEventServerPtr;\r
139 \r
140 }       //namespace IO\r
141 }       //namespace caspar\r
142 \r
143 #endif // !defined(AFX_ASYNCEVENTSERVER_H__0BFA29CB_BE4C_46A0_9CAE_E233ED27A8EC__INCLUDED_)\r