Skip to content
This repository has been archived by the owner on Jan 29, 2019. It is now read-only.

Commit

Permalink
Full timeout support (due to new socket code).
Browse files Browse the repository at this point in the history
[git-p4: depot-paths = "//depot/fIcy/main/": change = 1202]
  • Loading branch information
wavexx committed Mar 28, 2006
1 parent ea08e40 commit e787be4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 45 deletions.
62 changes: 25 additions & 37 deletions icy.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* icy - protocol functions and constants - implementation
* Copyright(c) 2003-2005 of wave++ (Yuri D'Elia) <wavexx@users.sf.net>
* Copyright(c) 2003-2006 of wave++ (Yuri D'Elia) <wavexx@users.sf.net>
* Distributed under GNU LGPL without ANY warranty.
*/

Expand Down Expand Up @@ -42,51 +42,39 @@ namespace ICY
}


Reader::Reader(Socket& in, const size_t bufSz,
const time_t timeout, const size_t metaSz)
Reader::Reader(Socket& in, const size_t bufSz, const size_t metaSz)
: in(in), bufSz(bufSz), mBufSz(metaSz)
{
buf = new char[bufSz];
mBuf = new char[mBufSz];

if(timeout)
{
this->timeout = new timeval;
this->timeout->tv_sec = timeout;
this->timeout->tv_usec = 0;
}
else
this->timeout = NULL;
}


Reader::~Reader()
{
delete []buf;
delete []mBuf;
if(timeout)
delete timeout;
}


size_t
Reader::dup(std::ostream* out, const size_t size, bool dup)
{
size_t r(0);

while(r != size)
{
size_t p(in.read(buf, (bufSz + r > size)? size - r: bufSz, timeout));
size_t p(in.read(buf, (bufSz + r > size)? size - r: bufSz));
if(!p)
break;
break;

if(out)
out->write(buf, p);
out->write(buf, p);
if(dup)
{
// check for writing errors, but do not throw exceptions: SIGPIPE
// should be generated already.
cout.write(buf, p);
cout.write(buf, p);
if(!cout)
dup = false;
else
Expand All @@ -105,7 +93,7 @@ namespace ICY
{
// read the first byte containing the lenght
char b;
in.readn(&b, sizeof(b), timeout);
in.readn(&b, sizeof(b));

size_t lenght(b * Proto::metaMul);
if(lenght > Proto::metaSz)
Expand All @@ -114,11 +102,11 @@ namespace ICY
// metadata could be empty
if(lenght)
{
in.readn(mBuf, lenght, timeout);
in.readn(mBuf, lenght);

// check for NULL termination
if(mBuf[lenght - 1])
throw std::runtime_error("invalid metadata stream");
throw std::runtime_error("invalid metadata stream");

char* p(mBuf);
char* n;
Expand All @@ -131,21 +119,21 @@ namespace ICY
*/
while(*p)
{
// variable name
n = strstr(p, Proto::vaStart);
if(!n)
break;
string name(p, n);
p = n + Proto::vaStartSz;

// value
n = strstr(p, Proto::vaEnd);
if(!n)
break;
string value(p, n);
p = n + Proto::vaEndSz;

meta.insert(std::make_pair(name, value));
// variable name
n = strstr(p, Proto::vaStart);
if(!n)
break;
string name(p, n);
p = n + Proto::vaStartSz;

// value
n = strstr(p, Proto::vaEnd);
if(!n)
break;
string value(p, n);
p = n + Proto::vaEndSz;

meta.insert(std::make_pair(name, value));
}
}

Expand Down
12 changes: 4 additions & 8 deletions icy.hh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* icy - protocol functions and constants
* Copyright(c) 2003-2005 of wave++ (Yuri D'Elia)
* Copyright(c) 2003-2006 of wave++ (Yuri D'Elia)
* Distributed under GNU LGPL without ANY warranty.
*/

Expand All @@ -15,9 +15,6 @@
#include <map>
#include <string>

// c system headers
#include <time.h>


namespace ICY
{
Expand All @@ -43,7 +40,7 @@ namespace ICY
const extern char* mTitle;
}


// interface
class Reader
{
Expand All @@ -52,12 +49,11 @@ namespace ICY
char* mBuf;
size_t bufSz;
size_t mBufSz;
timeval* timeout;


public:
Reader(Socket& in, const size_t bufSz, const time_t timeout = 0,
const size_t metaSz = Proto::metaSz);
Reader(Socket& in, const size_t bufSz,
const size_t metaSz = Proto::metaSz);
~Reader();


Expand Down

0 comments on commit e787be4

Please sign in to comment.