UDPServer.h 1.59 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 33 34 35
    typedef function< bool(int intervaled, const Buffer::Ptr &buffer, struct sockaddr *peer_addr)> onRecvData;
    ~UDPServer();
    static UDPServer &Instance();
    Socket::Ptr getSock(const EventPoller::Ptr &poller,const char *strLocalIp, int intervaled,uint16_t iLocalPort = 0);
    void listenPeer(const char *strPeerIp, void *pSelf, const onRecvData &cb);
    void stopListenPeer(const char *strPeerIp, void *pSelf);
xzl committed
36
private:
37 38 39 40 41
    UDPServer();
    void onRcvData(int intervaled, const Buffer::Ptr &pBuf,struct sockaddr *pPeerAddr);
    void onErr(const string &strKey,const SockException &err);
    unordered_map<string, Socket::Ptr> _mapUpdSock;
    mutex _mtxUpdSock;
xzl committed
42

43 44
    unordered_map<string, unordered_map<void *, onRecvData> > _mapDataHandler;
    mutex _mtxDataHandler;
xzl committed
45 46
};

xiongziliang committed
47
} /* namespace mediakit */
xzl committed
48 49

#endif /* RTSP_UDPSERVER_H_ */