// Put the list of net media in a parameter list void SDPMedia::putMedia(NamedList& msg, bool putPort) { msg.addParam("media" + suffix(),"yes"); msg.addParam("formats" + suffix(),formats()); msg.addParam("transport" + suffix(),transport()); if (mappings()) msg.addParam("rtp_mapping" + suffix(),mappings()); if (isAudio()) msg.addParam("rtp_rfc2833",rfc2833()); if (putPort) msg.addParam("rtp_port" + suffix(),remotePort()); if (remoteCrypto()) msg.addParam("crypto" + suffix(),remoteCrypto()); // must handle encryption differently const char* enc = getValue("encryption"); if (enc) msg.addParam("encryption" + suffix(),enc); clearParam("encryption"); unsigned int n = length(); for (unsigned int i = 0; i < n; i++) { const NamedString* param = getParam(i); if (param) msg.addParam("sdp" + suffix() + "_" + param->name(),*param); } }
Socket::Socket(struct ev_loop* loop, int fd, int af) : loop_(loop), watcher_(loop), timer_(loop), startedAt_(ev_now(loop)), lastActivityAt_(ev_now(loop)), fd_(fd), addressFamily_(af), secure_(false), state_(Operational), mode_(None), tcpCork_(false), remoteIP_(), remotePort_(0), localIP_(), localPort_(), callback_(nullptr), callbackData_(0) { #ifndef NDEBUG setLogging(false); static std::atomic<unsigned long long> id(0); setLoggingPrefix("Socket(%d, %s:%d)", ++id, remoteIP().c_str(), remotePort()); #endif TRACE("created. fd:%d, local(%s:%d)", fd_, localIP().c_str(), localPort()); watcher_.set<Socket, &Socket::io>(this); timer_.set<Socket, &Socket::timeout>(this); }
std::string Socket::remote() const { char buf[512]; size_t n; switch (addressFamily_) { case AF_INET: n = snprintf(buf, sizeof(buf), "%s:%d", remoteIP().c_str(), remotePort()); break; case AF_INET6: n = snprintf(buf, sizeof(buf), "[%s]:%d", remoteIP().c_str(), remotePort()); break; default: n = snprintf(buf, sizeof(buf), "%s", remoteIP().c_str()); break; } return std::string(buf, n); }
// Put the list of net media in a parameter list void SDPMedia::putMedia(NamedList& msg, bool putPort) { msg.addParam("media" + suffix(),"yes"); msg.addParam("formats" + suffix(),formats()); msg.addParam("transport" + suffix(),transport()); if (mappings()) msg.addParam("rtp_mapping" + suffix(),mappings()); if (isAudio()) msg.addParam("rtp_rfc2833",rfc2833()); if (putPort) msg.addParam("rtp_port" + suffix(),remotePort()); if (remoteCrypto()) msg.addParam("crypto" + suffix(),remoteCrypto()); // must handle encryption differently const char* enc = m_rAttrs.getValue("encryption"); if (enc) msg.addParam("encryption" + suffix(),enc); putNamedList(msg, m_rAttrs, "sdp" + suffix() + "_"); putNamedList(msg, m_fmtps, "fmtp_"); }
/** start first async operation for this HttpConnection. * * This is done by simply registering the underlying socket to the the I/O service * to watch for available input. * * \note This method must be invoked right after the object construction. * * \see stop() */ void HttpConnection::start(ServerSocket* listener, Socket* client, const HttpWorker::ConnectionHandle& handle) { handle_ = handle; listener_ = listener; socket_ = client; socket_->setReadyCallback<HttpConnection, &HttpConnection::io>(this); sink_.setSocket(socket_); #if defined(TCP_NODELAY) if (worker_->server().tcpNoDelay()) socket_->setTcpNoDelay(true); #endif #if !defined(NDEBUG) setLoggingPrefix("HttpConnection[%d,%llu|%s:%d]", worker_->id(), id_, remoteIP().c_str(), remotePort()); #endif TRACE("starting (fd=%d)", socket_->handle()); worker_->server_.onConnectionOpen(this); if (isAborted()) { // The connection got directly closed (aborted) upon connection instance creation (e.g. within the onConnectionOpen-callback), // so delete the object right away. close(); return; } request_ = new HttpRequest(*this); ref(); if (socket_->state() == Socket::Handshake) { TRACE("start: handshake."); socket_->handshake<HttpConnection, &HttpConnection::handshakeComplete>(this); } else { #if defined(TCP_DEFER_ACCEPT) && defined(WITH_TCP_DEFER_ACCEPT) TRACE("start: processing input"); // it is ensured, that we have data pending, so directly start reading if (readSome()) process(); else close(); TRACE("start: processing input done"); #else TRACE("start: watchInput."); // client connected, but we do not yet know if we have data pending watchInput(worker_->server_.maxReadIdle()); #endif } unref(); }
// PD_TRACE_DECLARE_FUNCTION ( SDB__NETEVNHND__RDCALLBK, "_netEventHandler::_readCallback" ) void _netEventHandler::_readCallback( const boost::system::error_code & error ) { PD_TRACE_ENTRY ( SDB__NETEVNHND__RDCALLBK ) ; if ( error ) { if ( error.value() == boost::system::errc::operation_canceled || error.value() == boost::system::errc::no_such_file_or_directory ) { PD_LOG ( PDINFO, "connection aborted, node:%d, %d, %d", _id.columns.groupID, _id.columns.nodeID, _id.columns.serviceID ) ; } else { PD_LOG ( PDERROR, "Error received, node:%d, %d, %d, err=%d", _id.columns.groupID, _id.columns.nodeID, _id.columns.serviceID, error.value() ) ; } goto error_close ; } if ( NET_EVENT_HANDLER_STATE_HEADER == _state ) { if ( ( UINT32 )MSG_SYSTEM_INFO_LEN == (UINT32)_header.messageLength ) { if ( SDB_OK != _allocateBuf( sizeof(MsgSysInfoRequest) )) { goto error_close ; } _hasRecvMsg = TRUE ; ossMemcpy( _buf, &_header, sizeof( MsgSysInfoRequest ) ) ; _frame->handleMsg( shared_from_this() ) ; _state = NET_EVENT_HANDLER_STATE_HEADER ; asyncRead() ; goto done ; } else if ( sizeof(_MsgHeader) > (UINT32)_header.messageLength || SDB_MAX_MSG_LENGTH < (UINT32)_header.messageLength ) { PD_LOG( PDERROR, "Error header[len: %d, opCode: (%d)%d, TID:%d] " "received, node:%d, %d, %d", _header.messageLength, IS_REPLY_TYPE(_header.opCode) ? 1 : 0, GET_REQUEST_TYPE(_header.opCode), _header.TID, _id.columns.groupID, _id.columns.nodeID, _id.columns.serviceID ) ; goto error_close ; } else { if ( FALSE == _hasRecvMsg ) { _hasRecvMsg = TRUE ; _state = NET_EVENT_HANDLER_STATE_HEADER_LAST ; asyncRead() ; _state = NET_EVENT_HANDLER_STATE_HEADER ; goto done ; } PD_LOG( PDDEBUG, "msg header: [len:%d], [opCode: [%d]%d], " "[TID:%d], [groupID:%d], [nodeID:%d], " "[ADDR:%s], [PORT:%d]", _header.messageLength, IS_REPLY_TYPE(_header.opCode)?1:0, GET_REQUEST_TYPE(_header.opCode), _header.TID, _header.routeID.columns.groupID, _header.routeID.columns.nodeID, remoteAddr().c_str(), remotePort() ) ; if ( MSG_INVALID_ROUTEID == _id.value ) { if ( MSG_INVALID_ROUTEID != _header.routeID.value ) { _id = _header.routeID ; _frame->_addRoute( shared_from_this() ) ; } } } if ( (UINT32)sizeof(_MsgHeader) == (UINT32)_header.messageLength ) { if ( SDB_OK != _allocateBuf( sizeof(_MsgHeader) )) { goto error_close ; } ossMemcpy( _buf, &_header, sizeof( _MsgHeader ) ) ; _frame->handleMsg( shared_from_this() ) ; _state = NET_EVENT_HANDLER_STATE_HEADER ; asyncRead() ; goto done ; } #if defined (_LINUX) try { boost::asio::detail::socket_option::boolean<IPPROTO_TCP, TCP_QUICKACK> quickack( TRUE ) ; _sock.set_option( quickack ) ; } catch ( boost::system::system_error &e ) { PD_LOG ( PDERROR, "Failed to quick ack: %s", e.what() ) ; } #endif // _LINUX _state = NET_EVENT_HANDLER_STATE_BODY ; asyncRead() ; } else { _frame->handleMsg( shared_from_this() ) ; _state = NET_EVENT_HANDLER_STATE_HEADER ; asyncRead() ; } done: PD_TRACE_EXIT ( SDB__NETEVNHND__RDCALLBK ) ; return ; error_close: if ( _isConnected ) { close() ; } _frame->handleClose( shared_from_this(), _id ) ; _frame->_erase( handle() ) ; goto done ; }