2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG (www.casparcg.com).
\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
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
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
19 * Author: Nicklas P Andersson
\r
23 // AsyncEventServer.h: interface for the AsyncServer class.
\r
24 //////////////////////////////////////////////////////////////////////
\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
33 #include <common/memory/safe_ptr.h>
\r
38 #include <functional>
\r
40 #include "..\util\thread.h"
\r
42 #include "ProtocolStrategy.h"
\r
44 #include <tbb\mutex.h>
\r
50 typedef std::shared_ptr<SocketInfo> SocketInfoPtr;
\r
52 typedef std::function<void(caspar::IO::SocketInfoPtr)> ClientDisconnectEvent;
\r
54 class AsyncEventServer : public IRunnable
\r
56 static long instanceCount_;
\r
59 AsyncEventServer(const AsyncEventServer&);
\r
60 AsyncEventServer& operator=(const AsyncEventServer&);
\r
63 explicit AsyncEventServer(const safe_ptr<IProtocolStrategy>& pProtocol, int port);
\r
64 ~AsyncEventServer();
\r
67 void SetProtocolStrategy(safe_ptr<IProtocolStrategy> pPS) {
\r
68 pProtocolStrategy_ = pPS;
\r
73 void SetClientDisconnectHandler(ClientDisconnectEvent handler);
\r
76 Thread listenThread_;
\r
77 void Run(HANDLE stopEvent);
\r
78 bool OnUnhandledException(const std::exception&) throw();
\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
86 SocketInfoPtr pListenSocketInfo_;
\r
87 safe_ptr<IProtocolStrategy> pProtocolStrategy_;
\r
90 friend class SocketInfo;
\r
91 void DoSend(SocketInfo&);
\r
92 void DisconnectClient(SocketInfo&);
\r
94 void LogSocketError(const TCHAR* pStr, int socketError = 0);
\r
96 class SocketInfoCollection
\r
98 SocketInfoCollection(const SocketInfoCollection&);
\r
99 SocketInfoCollection& operator=(const SocketInfoCollection&);
\r
101 typedef std::map<HANDLE, SocketInfoPtr> SocketInfoMap;
\r
102 typedef std::vector<HANDLE> HandleVector;
\r
105 SocketInfoCollection();
\r
106 ~SocketInfoCollection();
\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
113 bool FindSocketInfo(HANDLE, SocketInfoPtr& pResult);
\r
118 void ClearDirty() {
\r
122 std::size_t Size() {
\r
123 return waitEvents_.size();
\r
127 ClientDisconnectEvent onSocketInfoRemoved;
\r
130 SocketInfoMap socketInfoMap_;
\r
131 HandleVector waitEvents_;
\r
135 SocketInfoCollection socketInfoCollection_;
\r
138 typedef std::tr1::shared_ptr<AsyncEventServer> AsyncEventServerPtr;
\r
141 } //namespace caspar
\r
143 #endif // !defined(AFX_ASYNCEVENTSERVER_H__0BFA29CB_BE4C_46A0_9CAE_E233ED27A8EC__INCLUDED_)
\r