| 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/udpprotocol.h" |
|---|
| 22 | #include "netio/netio.h" |
|---|
| 23 | |
|---|
| 24 | UDPProtocol::UDPProtocol() |
|---|
| 25 | : BaseProtocol(PT_UDP) { |
|---|
| 26 | _decodedBytesCount = 0; |
|---|
| 27 | _pCarrier = NULL; |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | UDPProtocol::~UDPProtocol() { |
|---|
| 31 | if (_pCarrier != NULL) { |
|---|
| 32 | IOHandler *pCarrier = _pCarrier; |
|---|
| 33 | _pCarrier = NULL; |
|---|
| 34 | pCarrier->SetProtocol(NULL); |
|---|
| 35 | delete pCarrier; |
|---|
| 36 | } |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | bool UDPProtocol::Initialize(Variant ¶meters) { |
|---|
| 40 | return true; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | IOHandler *UDPProtocol::GetIOHandler() { |
|---|
| 44 | return _pCarrier; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | void UDPProtocol::SetIOHandler(IOHandler *pIOHandler) { |
|---|
| 48 | if (pIOHandler != NULL) { |
|---|
| 49 | if (pIOHandler->GetType() != IOHT_UDP_CARRIER) { |
|---|
| 50 | ASSERT("This protocol accepts only UDP carrier"); |
|---|
| 51 | } |
|---|
| 52 | } |
|---|
| 53 | _pCarrier = pIOHandler; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | bool UDPProtocol::AllowFarProtocol(uint64_t type) { |
|---|
| 57 | WARN("This protocol doesn't accept any far protocol"); |
|---|
| 58 | return false; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | bool UDPProtocol::AllowNearProtocol(uint64_t type) { |
|---|
| 62 | return true; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | IOBuffer * UDPProtocol::GetInputBuffer() { |
|---|
| 66 | return &_inputBuffer; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | bool UDPProtocol::SignalInputData(int32_t recvAmount) { |
|---|
| 70 | ASSERT("Operation not supported"); |
|---|
| 71 | return false; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | bool UDPProtocol::SignalInputData(int32_t recvAmount, sockaddr_in *pPeerAddress) { |
|---|
| 75 | _decodedBytesCount += recvAmount; |
|---|
| 76 | return _pNearProtocol->SignalInputData(_inputBuffer, pPeerAddress); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | bool UDPProtocol::SignalInputData(IOBuffer & /* ignored */) { |
|---|
| 80 | ASSERT("OPERATION NOT SUPPORTED"); |
|---|
| 81 | return false; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | bool UDPProtocol::EnqueueForOutbound() { |
|---|
| 85 | if (_pCarrier == NULL) { |
|---|
| 86 | ASSERT("TCPProtocol has no carrier"); |
|---|
| 87 | return false; |
|---|
| 88 | } |
|---|
| 89 | return _pCarrier->SignalOutputData(); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | uint64_t UDPProtocol::GetDecodedBytesCount() { |
|---|
| 93 | return _decodedBytesCount; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | |
|---|