UDPServer.h 2.28 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * MIT License
xzl committed
3
 *
xiongziliang committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * Copyright (c) 2016 xiongziliang <771730766@qq.com>
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
xzl committed
25 26 27 28
 */

#ifndef RTSP_UDPSERVER_H_
#define RTSP_UDPSERVER_H_
xiongzilaing committed
29

xzl committed
30
#include <mutex>
xiongzilaing committed
31 32 33
#include <stdint.h>
#include <unordered_map>
#include <unordered_set>
xzl committed
34
#include "Util/util.h"
xiongzilaing committed
35 36 37
#include "Util/logger.h"
#include "Network/Socket.h"

xzl committed
38
using namespace std;
xiongziliang committed
39
using namespace toolkit;
xiongzilaing committed
40

xiongziliang committed
41
namespace mediakit {
xzl committed
42 43 44

class UDPServer {
public:
45
	typedef function< bool(int, const Buffer::Ptr &, struct sockaddr *)> onRecvData;
xzl committed
46 47
	UDPServer();
	virtual ~UDPServer();
48 49 50
	static UDPServer &Instance();
	static void Destory();

xiongziliang committed
51
	Socket::Ptr getSock(const char *strLocalIp, int iTrackIndex,uint16_t iLocalPort = 0);
xzl committed
52 53 54
	void listenPeer(const char *strPeerIp, void *pSelf, const onRecvData &cb);
	void stopListenPeer(const char *strPeerIp, void *pSelf);
private:
55
	void onRcvData(int iTrackId, const Buffer::Ptr &pBuf,struct sockaddr *pPeerAddr);
xzl committed
56
	void onErr(const string &strKey,const SockException &err);
57 58
	unordered_map<string, Socket::Ptr> _mapUpdSock;
	mutex _mtxUpdSock;
xzl committed
59

60 61
	unordered_map<string, unordered_map<void *, onRecvData> > _mapDataHandler;
	mutex _mtxDataHandler;
xzl committed
62 63
};

xiongziliang committed
64
} /* namespace mediakit */
xzl committed
65 66

#endif /* RTSP_UDPSERVER_H_ */