Merge branch 'master' of projectlivestream.com:pls

This commit is contained in:
Erik Zandvliet 2010-12-14 10:41:36 +01:00
commit 3790443e69
2 changed files with 44 additions and 17 deletions

View file

@ -33,7 +33,6 @@ int DDV_OpenUnix(std::string adres, bool nonblock = false){
int DDV_Listen(int port){ int DDV_Listen(int port){
int s = socket(AF_INET, SOCK_STREAM, 0); int s = socket(AF_INET, SOCK_STREAM, 0);
int on = 1; int on = 1;
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
struct sockaddr_in addr; struct sockaddr_in addr;
@ -46,12 +45,40 @@ int DDV_Listen(int port){
if (ret == 0){ if (ret == 0){
return s; return s;
}else{ }else{
printf("Listen failed! Error: %s\n", strerror(errno)); fprintf(stderr, "Listen failed! Error: %s\n", strerror(errno));
close(s); close(s);
return 0; return 0;
} }
}else{ }else{
printf("Binding failed! Error: %s\n", strerror(errno)); fprintf(stderr, "Binding failed! Error: %s\n", strerror(errno));
close(s);
return 0;
}
}
int DDV_UnixListen(std::string adres, bool nonblock = false){
unlink(adres.c_str());
int s = socket(AF_UNIX, SOCK_STREAM, 0);
if (nonblock){
int flags = fcntl(s, F_GETFL, 0);
flags |= O_NONBLOCK;
fcntl(s, F_SETFL, flags);
}
sockaddr_un addr;
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, adres.c_str(), adres.size()+1);
int ret = bind(s, (sockaddr*)&addr, sizeof(addr));
if (ret == 0){
ret = listen(s, 100);//start listening, backlog of 100 allowed
if (ret == 0){
return s;
}else{
fprintf(stderr, "Listen failed! Error: %s\n", strerror(errno));
close(s);
return 0;
}
}else{
fprintf(stderr, "Binding failed! Error: %s\n", strerror(errno));
close(s); close(s);
return 0; return 0;
} }
@ -59,7 +86,7 @@ int DDV_Listen(int port){
int DDV_Accept(int sock, bool nonblock = false){ int DDV_Accept(int sock, bool nonblock = false){
int r = accept(sock, 0, 0); int r = accept(sock, 0, 0);
if ((r > 0) && nonblock){ if ((r >= 0) && nonblock){
int flags = fcntl(r, F_GETFL, 0); int flags = fcntl(r, F_GETFL, 0);
flags |= O_NONBLOCK; flags |= O_NONBLOCK;
fcntl(r, F_SETFL, flags); fcntl(r, F_SETFL, flags);
@ -77,7 +104,7 @@ bool DDV_write(void * buffer, int todo, int sock){
case EWOULDBLOCK: socketBlocking = true; break; case EWOULDBLOCK: socketBlocking = true; break;
default: default:
socketError = true; socketError = true;
printf("Could not write! %s\n", strerror(errno)); fprintf(stderr, "Could not write! %s\n", strerror(errno));
return false; return false;
break; break;
} }
@ -87,14 +114,14 @@ bool DDV_write(void * buffer, int todo, int sock){
return true; return true;
} }
bool DDV_ready(int sock){ signed int DDV_ready(int sock){
char tmp; char tmp;
int preflags = fcntl(sock, F_GETFL, 0); int preflags = fcntl(sock, F_GETFL, 0);
int postflags = preflags | O_NONBLOCK; int postflags = preflags | O_NONBLOCK;
fcntl(sock, F_SETFL, postflags); fcntl(sock, F_SETFL, postflags);
int r = recv(sock, &tmp, 1, MSG_PEEK); int r = recv(sock, &tmp, 1, MSG_PEEK);
fcntl(sock, F_SETFL, preflags); fcntl(sock, F_SETFL, preflags);
return (r == 1); return r;
} }
bool DDV_read(void * buffer, int todo, int sock){ bool DDV_read(void * buffer, int todo, int sock){
@ -107,7 +134,7 @@ bool DDV_read(void * buffer, int todo, int sock){
case EWOULDBLOCK: socketBlocking = true; break; case EWOULDBLOCK: socketBlocking = true; break;
default: default:
socketError = true; socketError = true;
printf("Could not read! %s\n", strerror(errno)); fprintf(stderr, "Could not read! %s\n", strerror(errno));
return false; return false;
break; break;
} }
@ -126,11 +153,11 @@ int DDV_iwrite(void * buffer, int todo, int sock){
int r = send(sock, buffer, todo, 0); int r = send(sock, buffer, todo, 0);
if (r < 0){ if (r < 0){
switch (errno){ switch (errno){
case EWOULDBLOCK: break; case EWOULDBLOCK: return 0; break;
default: default:
socketError = true; socketError = true;
printf("Could not write! %s\n", strerror(errno)); fprintf(stderr, "Could not write! %s\n", strerror(errno));
return false; return 0;
break; break;
} }
} }
@ -144,8 +171,8 @@ int DDV_iread(void * buffer, int todo, int sock){
case EWOULDBLOCK: break; case EWOULDBLOCK: break;
default: default:
socketError = true; socketError = true;
printf("Could not read! %s\n", strerror(errno)); fprintf(stderr, "Could not read! %s\n", strerror(errno));
return false; return 0;
break; break;
} }
} }

View file

@ -69,6 +69,7 @@ bool FLV_GetPacket(FLV_Pack *& p, int sock){
if (FLV_Checkheader(p->data)){ if (FLV_Checkheader(p->data)){
sofar = 0; sofar = 0;
memcpy(FLVHeader, p->data, 13); memcpy(FLVHeader, p->data, 13);
//fwrite(p->data, 13, 1, stdout);//output raw stream
}else{ }else{
All_Hell_Broke_Loose = true; All_Hell_Broke_Loose = true;
fprintf(stderr, "Invalid FLV header. All Hell Broke Loose!\n"); fprintf(stderr, "Invalid FLV header. All Hell Broke Loose!\n");
@ -94,12 +95,11 @@ bool FLV_GetPacket(FLV_Pack *& p, int sock){
testlen += (p->data[p->len-2] << 8); testlen += (p->data[p->len-2] << 8);
testlen += (p->data[p->len-3] << 16); testlen += (p->data[p->len-3] << 16);
testlen += (p->data[p->len-4] << 24); testlen += (p->data[p->len-4] << 24);
if (p->len == testlen){ //fwrite(p->data, p->len, 1, stdout);//output raw stream
fprintf(stderr, "Correct length tag...\n"); if (p->len != testlen){
}else{
fprintf(stderr, "Len: %i, testlen: %i\n", p->len, testlen); fprintf(stderr, "Len: %i, testlen: %i\n", p->len, testlen);
All_Hell_Broke_Loose = true; All_Hell_Broke_Loose = true;
fprintf(stderr, "ReadUntil fail: > 500kb tag? All Hell Broke Loose!\n", strerror(errno)); fprintf(stderr, "ReadUntil fail: Wrong size tag? All Hell Broke Loose!\n");
return false; return false;
} }
done = true; done = true;