source: trunk/sources/thelib/src/protocols/protocolmanager.cpp @ 745

Revision 745, 3.4 KB checked in by shiretu, 3 months ago (diff)

-- imported EvoStream? patches and fixes

Line 
1/*
2 *  Copyright (c) 2010,
3 *  Gavriloaie Eugen-Andrei (shiretu@gmail.com)
4 *
5 *  This file is part of crtmpserver.
6 *  crtmpserver is free software: you can redistribute it and/or modify
7 *  it under the terms of the GNU General Public License as published by
8 *  the Free Software Foundation, either version 3 of the License, or
9 *  (at your option) any later version.
10 *
11 *  crtmpserver is distributed in the hope that it will be useful,
12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 *  GNU General Public License for more details.
15 *
16 *  You should have received a copy of the GNU General Public License
17 *  along with crtmpserver.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20
21#include "protocols/protocolmanager.h"
22#include "protocols/baseprotocol.h"
23#include "netio/netio.h"
24
25map<uint32_t, BaseProtocol *> ProtocolManager::_activeProtocols;
26map<uint32_t, BaseProtocol *> ProtocolManager::_deadProtocols;
27
28void ProtocolManager::RegisterProtocol(BaseProtocol *pProtocol) {
29        if (MAP_HAS1(_activeProtocols, pProtocol->GetId()))
30                return;
31        if (MAP_HAS1(_deadProtocols, pProtocol->GetId()))
32                return;
33        _activeProtocols[pProtocol->GetId()] = pProtocol;
34}
35
36void ProtocolManager::UnRegisterProtocol(BaseProtocol *pProtocol) {
37        if (MAP_HAS1(_activeProtocols, pProtocol->GetId()))
38                _activeProtocols.erase(pProtocol->GetId());
39        if (MAP_HAS1(_deadProtocols, pProtocol->GetId()))
40                _deadProtocols.erase(pProtocol->GetId());
41}
42
43void ProtocolManager::EnqueueForDelete(BaseProtocol *pProtocol) {
44        if (pProtocol->GetNearProtocol() == NULL) {
45                FINEST("Enqueue for delete for protocol %s", STR(*pProtocol));
46        }
47        pProtocol->SetApplication(NULL);
48        if (MAP_HAS1(_activeProtocols, pProtocol->GetId()))
49                _activeProtocols.erase(pProtocol->GetId());
50        if (!MAP_HAS1(_deadProtocols, pProtocol->GetId()))
51                _deadProtocols[pProtocol->GetId()] = pProtocol;
52}
53
54uint32_t ProtocolManager::CleanupDeadProtocols() {
55        uint32_t result = 0;
56        while (_deadProtocols.size() > 0) {
57                BaseProtocol *pBaseProtocol = MAP_VAL(_deadProtocols.begin());
58                delete pBaseProtocol;
59                result++;
60        }
61        return result;
62}
63
64void ProtocolManager::Shutdown() {
65        while (_activeProtocols.size() > 0) {
66                EnqueueForDelete(MAP_VAL(_activeProtocols.begin()));
67        }
68}
69
70BaseProtocol * ProtocolManager::GetProtocol(uint32_t id,
71                bool includeDeadProtocols) {
72        if (!includeDeadProtocols && MAP_HAS1(_deadProtocols, id))
73                return NULL;
74        if (MAP_HAS1(_activeProtocols, id))
75                return _activeProtocols[id];
76        if (MAP_HAS1(_deadProtocols, id))
77                return _deadProtocols[id];
78        return NULL;
79}
80
81const map<uint32_t, BaseProtocol *> & ProtocolManager::GetActiveProtocols() {
82        return _activeProtocols;
83}
84
85void ProtocolManager::GetActiveProtocols(map<uint32_t, BaseProtocol *> &result,
86                protocolManagerFilter_f filter) {
87        result.clear();
88        if (filter == NULL) {
89                result = _activeProtocols;
90                return;
91        }
92
93        FOR_MAP(_activeProtocols, uint32_t, BaseProtocol *, i) {
94                if (!filter(MAP_VAL(i)))
95                        continue;
96                result[MAP_VAL(i)->GetId()] = MAP_VAL(i);
97        }
98}
99
100bool protocolManagerNetworkedProtocolsFilter(BaseProtocol *pProtocol) {
101        IOHandler *pIOHandler = pProtocol->GetIOHandler();
102        if ((pIOHandler == NULL)
103                        || ((pIOHandler->GetType() != IOHT_TCP_CARRIER)
104                        && (pIOHandler->GetType() != IOHT_UDP_CARRIER)))
105                return false;
106        return true;
107}
108
109bool protocolManagerNearProtocolsFilter(BaseProtocol *pProtocol) {
110        return pProtocol->GetNearProtocol() == NULL;
111}
Note: See TracBrowser for help on using the repository browser.