UDPServer.cpp 2.82 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
3
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
xiongziliang committed
5
 *
xiongziliang committed
6 7 8
 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
xzl committed
9 10 11 12
 */

#include "UDPServer.h"
#include "Util/TimeTicker.h"
13 14
#include "Util/onceToken.h"

xiongziliang committed
15
using namespace toolkit;
xiongzilaing committed
16

xiongziliang committed
17
namespace mediakit {
xzl committed
18

19 20
INSTANCE_IMP(UDPServer);
    
xzl committed
21 22 23 24
UDPServer::UDPServer() {
}

UDPServer::~UDPServer() {
25
    InfoL;
xzl committed
26 27
}

28 29 30 31 32 33 34
Socket::Ptr UDPServer::getSock(SocketHelper &helper, const char* local_ip, int interleaved, uint16_t local_port) {
    lock_guard<mutex> lck(_mtx_udp_sock);
    string key = StrPrinter << local_ip << ":" << interleaved << endl;
    auto it = _udp_sock_map.find(key);
    if (it == _udp_sock_map.end()) {
        Socket::Ptr sock = helper.createSocket();
        if (!sock->bindUdpSock(local_port, local_ip)) {
35 36 37
            //分配失败
            return nullptr;
        }
xzl committed
38

39 40 41 42 43
        sock->setOnErr(bind(&UDPServer::onErr, this, key, placeholders::_1));
        sock->setOnRead(bind(&UDPServer::onRecv, this, interleaved, placeholders::_1, placeholders::_2));
        _udp_sock_map[key] = sock;
        DebugL << local_ip << " " << sock->get_local_port() << " " << interleaved;
        return sock;
44 45
    }
    return it->second;
xzl committed
46 47
}

48 49 50 51
void UDPServer::listenPeer(const char* peer_ip, void* obj, const onRecvData &cb) {
    lock_guard<mutex> lck(_mtx_on_recv);
    auto &ref = _on_recv_map[peer_ip];
    ref.emplace(obj, cb);
xzl committed
52 53
}

54 55 56 57
void UDPServer::stopListenPeer(const char* peer_ip, void* obj) {
    lock_guard<mutex> lck(_mtx_on_recv);
    auto it0 = _on_recv_map.find(peer_ip);
    if (it0 == _on_recv_map.end()) {
58 59
        return;
    }
60 61 62 63
    auto &ref = it0->second;
    auto it1 = ref.find(obj);
    if (it1 != ref.end()) {
        ref.erase(it1);
64
    }
65 66
    if (ref.size() == 0) {
        _on_recv_map.erase(it0);
67
    }
xzl committed
68
}
69 70

void UDPServer::onErr(const string &key, const SockException &err) {
71
    WarnL << err.what();
72 73
    lock_guard<mutex> lck(_mtx_udp_sock);
    _udp_sock_map.erase(key);
xzl committed
74
}
75 76 77 78 79 80 81

void UDPServer::onRecv(int interleaved, const Buffer::Ptr &buf, struct sockaddr* peer_addr) {
    struct sockaddr_in *in = (struct sockaddr_in *) peer_addr;
    string peer_ip = SockUtil::inet_ntoa(in->sin_addr);
    lock_guard<mutex> lck(_mtx_on_recv);
    auto it0 = _on_recv_map.find(peer_ip);
    if (it0 == _on_recv_map.end()) {
82 83
        return;
    }
84 85 86 87 88
    auto &ref = it0->second;
    for (auto it1 = ref.begin(); it1 != ref.end(); ++it1) {
        auto &func = it1->second;
        if (!func(interleaved, buf, peer_addr)) {
            it1 = ref.erase(it1);
89 90
        }
    }
91 92
    if (ref.size() == 0) {
        _on_recv_map.erase(it0);
93
    }
xzl committed
94 95
}

xiongziliang committed
96
} /* namespace mediakit */
xzl committed
97 98