UDPServer.h 1.58 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
3 4 5
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
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
 */

#ifndef RTSP_UDPSERVER_H_
#define RTSP_UDPSERVER_H_
xiongzilaing committed
13 14

#include <stdint.h>
15 16
#include <mutex>
#include <memory>
xiongzilaing committed
17 18
#include <unordered_map>
#include <unordered_set>
xzl committed
19
#include "Util/util.h"
xiongzilaing committed
20 21 22
#include "Util/logger.h"
#include "Network/Socket.h"

xzl committed
23
using namespace std;
xiongziliang committed
24
using namespace toolkit;
xiongzilaing committed
25

xiongziliang committed
26
namespace mediakit {
xzl committed
27

28
class UDPServer : public std::enable_shared_from_this<UDPServer> {
xzl committed
29
public:
30 31 32
    typedef function< bool(int intervaled, const Buffer::Ptr &buffer, struct sockaddr *peer_addr)> onRecvData;
    ~UDPServer();
    static UDPServer &Instance();
33 34 35 36
    Socket::Ptr getSock(SocketHelper &helper, const char *local_ip, int interleaved, uint16_t local_port = 0);
    void listenPeer(const char *peer_ip, void *obj, const onRecvData &cb);
    void stopListenPeer(const char *peer_ip, void *obj);

xzl committed
37
private:
38
    UDPServer();
39
    void onRecv(int interleaved, const Buffer::Ptr &buf, struct sockaddr *peer_addr);
40
    void onErr(const string &strKey,const SockException &err);
xzl committed
41

42 43 44 45 46
private:
    mutex _mtx_udp_sock;
    mutex _mtx_on_recv;
    unordered_map<string, Socket::Ptr> _udp_sock_map;
    unordered_map<string, unordered_map<void *, onRecvData> > _on_recv_map;
xzl committed
47 48
};

xiongziliang committed
49
} /* namespace mediakit */
xzl committed
50 51

#endif /* RTSP_UDPSERVER_H_ */