From: Christian P. <cp...@us...> - 2005-01-16 00:05:12
|
Update of /cvsroot/pclasses/pclasses2/src/Net In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24693/src/Net Modified Files: RTSPSocket.cpp Log Message: Some RTSP parts are rewritten. Index: RTSPSocket.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/Net/RTSPSocket.cpp,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- RTSPSocket.cpp 14 Jan 2005 14:56:01 -0000 1.1 +++ RTSPSocket.cpp 16 Jan 2005 00:05:00 -0000 1.2 @@ -28,13 +28,18 @@ extern Socket::Domain AddrFamily2Domain(int family); +RTSPRequest::RTSPRequest() +: _method(UNKNOWN), _url(), _urlValid(false), _protoVer("RTSP/1.0") +{ +} + RTSPRequest::RTSPRequest(Method method) -: _method(method), _url(), _urlValid(false) +: _method(method), _url(), _urlValid(false), _protoVer("RTSP/1.0") { } RTSPRequest::RTSPRequest(Method method, const IO::URL& url) -: _method(method), _url(url), _urlValid(true) +: _method(method), _url(url), _urlValid(true), _protoVer("RTSP/1.0") { } @@ -68,6 +73,16 @@ return _urlValid; } +void RTSPRequest::setProtocolVersion(const std::string& protoVer) +{ + _protoVer = protoVer; +} + +const std::string& RTSPRequest::protocolVersion() const throw() +{ + return _protoVer; +} + const RTSPRequestHeader& RTSPRequest::header() const throw() { return _header; @@ -78,8 +93,15 @@ return _header; } +RTSPResponse::RTSPResponse(const std::string& protoVer, + int code, const std::string& reason) +: _socket(0), _protoVer(protoVer), _statusCode(code), _reason(reason), + _bytesRead(0), _contentLength(0) +{ +} + RTSPResponse::RTSPResponse(RTSPSocket& socket, const std::string& protoVer, - int code, const std::string& reason) + int code, const std::string& reason) : _socket(&socket), _protoVer(protoVer), _statusCode(code), _reason(reason), _bytesRead(0), _contentLength(0) { @@ -208,9 +230,44 @@ Socket::open(d, Stream, 0); } -void RTSPSocket::sendRequest(RTSPRequest& req) throw(IO::IOError) +void RTSPSocket::sendHeader(const RTSPHeader& header) throw(IO::IOError) { - IO::IOStream reqs(*this); +} + +void RTSPSocket::readHeader(RTSPHeader& header) throw(IO::IOError) +{ + std::string line; + + // read request headers ... + while(1) + { + line = readLine(); + + // eof ? + if(line.empty()) + break; + + // response header terminated ? + if(line == "\r\n" || line == "\r" || line == "\n") + break; + + // split string and set header field/value ... + size_t pos = line.find(':'); + if(pos != std::string::npos && line.size() > 4) + { + std::string headName = line.substr(0, pos); + std::string headVal = line.substr(pos+2, line.size()-(pos+4)); + + std::cerr << headName << '=' << headVal << std::endl; + + header.set(headName, headVal); + } + } +} + +void RTSPSocket::sendRequest(const RTSPRequest& req) throw(IO::IOError) +{ + std::ostringstream reqs; switch(req.method()) { @@ -258,7 +315,7 @@ reqs << "*"; // send protocol version .. - reqs << " RTSP/1.0\r\n"; + reqs << " " << req.protocolVersion() << "\r\n"; // send header lines ... RTSPHeader::const_iterator i = req.header().begin(); @@ -275,44 +332,103 @@ if(!hostFound) reqs << "Host: " << req.url().host() << "\r\n"; - // terminate request and flush stream + // terminate request and send it ... reqs << "\r\n"; - reqs.sync(); + + std::string reqStr = reqs.str(); + write(reqStr.data(), reqStr.size()); +} + +RTSPRequest RTSPSocket::readRequest() throw(IO::IOError) +{ + std::string rtspMethod; + std::string url; + std::string rtspProto; + + std::string line = readLine(); + if(!line.empty()) + { + std::istringstream is(line); + is >> rtspMethod; + is >> url; + is >> rtspProto; + } + + RTSPRequest::Method method; + + if(rtspMethod == "DESCRIBE") + method = RTSPRequest::DESCRIBE; + else if(rtspMethod == "ANNOUNCE") + method = RTSPRequest::ANNOUNCE; + else if(rtspMethod == "GET_PARAMETER") + method = RTSPRequest::GET_PARAMETER; + else if(rtspMethod == "OPTIONS") + method = RTSPRequest::OPTIONS; + else if(rtspMethod == "PAUSE") + method = RTSPRequest::PAUSE; + else if(rtspMethod == "PLAY") + method = RTSPRequest::PLAY; + else if(rtspMethod == "RECORD") + method = RTSPRequest::RECORD; + else if(rtspMethod == "REDIRECT") + method = RTSPRequest::REDIRECT; + else if(rtspMethod == "SETUP") + method = RTSPRequest::SETUP; + else if(rtspMethod == "SET_PARAMETER") + method = RTSPRequest::SET_PARAMETER; + else if(rtspMethod == "TEARDOWN") + method = RTSPRequest::TEARDOWN; + else + { + //@@fixme throw + } + + // construct our request object ... + RTSPRequest ret(method); + ret.setProtocolVersion(rtspProto); + + // set request url if not '*' ... + if(url != "*") + ret.setUrl(url); + + readHeader(ret.header()); + + return ret; +} + +void RTSPSocket::sendResponse(const RTSPResponse& resp) throw(IO::IOError) +{ + std::ostringstream resps; + resps << resp.protocolVersion() << " " << resp.statusCode() << " " << resp.reason() << "\r\n"; + + // send header lines ... + RTSPHeader::const_iterator i = resp.header().begin(); + while(i != resp.header().end()) + { + resps << i->first << ": " << i->second << "\r\n"; + ++i; + } + + // terminate request and send it ... + resps << "\r\n"; + + std::string respStr = resps.str(); + write(respStr.data(), respStr.size()); } RTSPResponse RTSPSocket::readResponse() throw(IO::IOError) { - std::string line1 = readLine(); + std::string line = readLine(); std::string rtspProto, rtspResponse; int rtspResponseCode; - std::string tmp; - std::istringstream is(line1); + std::istringstream is(line); is >> rtspProto; is >> rtspResponseCode; is >> rtspResponse; RTSPResponse resp(*this, rtspProto, rtspResponseCode, rtspResponse); - - while(1) - { - tmp = readLine(); - - // response header terminated ? - if(tmp == "\r\n" || tmp == "\n") - break; - - size_t pos = tmp.find(':'); - if(pos != std::string::npos && tmp.size() > 4) - { - std::string headName = tmp.substr(0, pos); - std::string headVal = tmp.substr(pos+2, tmp.size()-(pos+4)); - - std::cerr << headName << '=' << headVal << std::endl; - - resp.header().set(headName, headVal); - } - } + readHeader(resp.header()); return resp; } |