2022-06-03 13:25:32 +08:00
|
|
|
|
#include "SrtSession.hpp"
|
|
|
|
|
#include "Packet.hpp"
|
2022-06-03 16:47:53 +08:00
|
|
|
|
#include "SrtTransportImp.hpp"
|
2022-06-03 13:25:32 +08:00
|
|
|
|
|
|
|
|
|
#include "Common/config.h"
|
|
|
|
|
|
|
|
|
|
namespace SRT {
|
|
|
|
|
using namespace mediakit;
|
|
|
|
|
|
|
|
|
|
SrtSession::SrtSession(const Socket::Ptr &sock)
|
2022-11-19 09:33:10 +08:00
|
|
|
|
: Session(sock) {
|
2022-06-03 13:25:32 +08:00
|
|
|
|
socklen_t addr_len = sizeof(_peer_addr);
|
2022-06-07 09:52:20 +08:00
|
|
|
|
memset(&_peer_addr, 0, addr_len);
|
|
|
|
|
// TraceL<<"before addr len "<<addr_len;
|
2022-06-03 13:25:32 +08:00
|
|
|
|
getpeername(sock->rawFD(), (struct sockaddr *)&_peer_addr, &addr_len);
|
2022-06-07 09:52:20 +08:00
|
|
|
|
// TraceL<<"after addr len "<<addr_len<<" family "<<_peer_addr.ss_family;
|
2022-06-03 13:25:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-07 09:52:20 +08:00
|
|
|
|
void SrtSession::attachServer(const toolkit::Server &server) {
|
|
|
|
|
SockUtil::setRecvBuf(getSock()->rawFD(), 1024 * 1024);
|
2022-06-03 18:05:24 +08:00
|
|
|
|
}
|
2022-06-07 09:52:20 +08:00
|
|
|
|
|
2024-03-02 18:25:32 +08:00
|
|
|
|
extern SrtTransport::Ptr querySrtTransport(uint8_t *data, size_t size, const EventPoller::Ptr& poller);
|
|
|
|
|
|
|
|
|
|
EventPoller::Ptr SrtSession::queryPoller(const Buffer::Ptr &buffer) {
|
|
|
|
|
auto transport = querySrtTransport((uint8_t *)buffer->data(), buffer->size(), nullptr);
|
|
|
|
|
return transport ? transport->getPoller() : nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-03 13:25:32 +08:00
|
|
|
|
void SrtSession::onRecv(const Buffer::Ptr &buffer) {
|
2022-06-07 09:52:20 +08:00
|
|
|
|
uint8_t *data = (uint8_t *)buffer->data();
|
2022-06-03 13:25:32 +08:00
|
|
|
|
size_t size = buffer->size();
|
|
|
|
|
|
|
|
|
|
if (_find_transport) {
|
|
|
|
|
//只允许寻找一次transport
|
|
|
|
|
_find_transport = false;
|
2024-03-02 18:25:32 +08:00
|
|
|
|
_transport = querySrtTransport(data, size, getPoller());
|
2022-06-07 09:52:20 +08:00
|
|
|
|
if (_transport) {
|
2023-04-28 22:03:16 +08:00
|
|
|
|
_transport->setSession(static_pointer_cast<Session>(shared_from_this()));
|
2022-06-03 13:25:32 +08:00
|
|
|
|
}
|
|
|
|
|
InfoP(this);
|
|
|
|
|
}
|
|
|
|
|
_ticker.resetTime();
|
|
|
|
|
|
2022-06-07 09:52:20 +08:00
|
|
|
|
if (_transport) {
|
|
|
|
|
_transport->inputSockData(data, size, &_peer_addr);
|
|
|
|
|
} else {
|
|
|
|
|
// WarnL<< "ingore data";
|
2022-06-03 13:25:32 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SrtSession::onError(const SockException &err) {
|
|
|
|
|
// udp链接超时,但是srt链接不一定超时,因为可能存在udp链接迁移的情况
|
|
|
|
|
//在udp链接迁移时,新的SrtSession对象将接管SrtSession对象的生命周期
|
|
|
|
|
//本SrtSession对象将在超时后自动销毁
|
2023-04-23 00:10:18 +08:00
|
|
|
|
WarnP(this) << err;
|
2022-06-03 13:25:32 +08:00
|
|
|
|
|
|
|
|
|
if (!_transport) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-06-07 09:52:20 +08:00
|
|
|
|
|
2022-06-03 13:25:32 +08:00
|
|
|
|
// 防止互相引用导致不释放
|
|
|
|
|
auto transport = std::move(_transport);
|
2022-06-07 09:52:20 +08:00
|
|
|
|
getPoller()->async(
|
2022-09-20 00:39:42 +08:00
|
|
|
|
[transport] {
|
2022-06-07 09:52:20 +08:00
|
|
|
|
//延时减引用,防止使用transport对象时,销毁对象
|
2022-09-20 00:39:42 +08:00
|
|
|
|
//transport->onShutdown(err);
|
2022-06-07 09:52:20 +08:00
|
|
|
|
},
|
|
|
|
|
false);
|
2022-06-03 13:25:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SrtSession::onManager() {
|
|
|
|
|
GET_CONFIG(float, timeoutSec, kTimeOutSec);
|
2022-06-07 09:52:20 +08:00
|
|
|
|
if (_ticker.elapsedTime() > timeoutSec * 1000) {
|
2022-06-03 13:25:32 +08:00
|
|
|
|
shutdown(SockException(Err_timeout, "srt connection timeout"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace SRT
|