Merge branch 'master' of octo.ddvtech.com:pls
This commit is contained in:
commit
212f62f17d
19 changed files with 384 additions and 138 deletions
|
@ -1,4 +1,4 @@
|
||||||
SRC = main.cpp ../util/ddv_socket.cpp ../util/flv_tag.cpp
|
SRC = main.cpp ../util/socket.cpp ../util/flv_tag.cpp
|
||||||
OBJ = $(SRC:.cpp=.o)
|
OBJ = $(SRC:.cpp=.o)
|
||||||
OUT = DDV_Buffer
|
OUT = DDV_Buffer
|
||||||
INCLUDES =
|
INCLUDES =
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include "../util/flv_tag.h" //FLV format parser
|
#include "../util/flv_tag.h" //FLV format parser
|
||||||
#include "../util/ddv_socket.h" //DDV Socket lib
|
#include "../util/socket.h" //Socket lib
|
||||||
|
|
||||||
#include <sys/epoll.h>
|
#include <sys/epoll.h>
|
||||||
|
|
||||||
|
@ -44,10 +44,10 @@ namespace Buffer{
|
||||||
bool gotproperaudio; ///< Whether the user received proper audio yet.
|
bool gotproperaudio; ///< Whether the user received proper audio yet.
|
||||||
void * lastpointer; ///< Pointer to data part of current buffer.
|
void * lastpointer; ///< Pointer to data part of current buffer.
|
||||||
static int UserCount; ///< Global user counter.
|
static int UserCount; ///< Global user counter.
|
||||||
DDV::Socket S; ///< Connection to user
|
Socket::Connection S; ///< Connection to user
|
||||||
/// Creates a new user from a newly connected socket.
|
/// Creates a new user from a newly connected socket.
|
||||||
/// Also prints "User connected" text to stdout.
|
/// Also prints "User connected" text to stdout.
|
||||||
user(DDV::Socket fd){
|
user(Socket::Connection fd){
|
||||||
S = fd;
|
S = fd;
|
||||||
MyNum = UserCount++;
|
MyNum = UserCount++;
|
||||||
gotproperaudio = false;
|
gotproperaudio = false;
|
||||||
|
@ -143,7 +143,7 @@ namespace Buffer{
|
||||||
std::string shared_socket = "/tmp/shared_socket_";
|
std::string shared_socket = "/tmp/shared_socket_";
|
||||||
shared_socket += argv[2];
|
shared_socket += argv[2];
|
||||||
|
|
||||||
DDV::ServerSocket SS(shared_socket, true);
|
Socket::Server SS(shared_socket, true);
|
||||||
FLV::Tag metadata;
|
FLV::Tag metadata;
|
||||||
FLV::Tag video_init;
|
FLV::Tag video_init;
|
||||||
FLV::Tag audio_init;
|
FLV::Tag audio_init;
|
||||||
|
@ -155,7 +155,7 @@ namespace Buffer{
|
||||||
int current_buffer = 0;
|
int current_buffer = 0;
|
||||||
int lastproper = 0;//last properly finished buffer number
|
int lastproper = 0;//last properly finished buffer number
|
||||||
unsigned int loopcount = 0;
|
unsigned int loopcount = 0;
|
||||||
DDV::Socket incoming;
|
Socket::Connection incoming;
|
||||||
|
|
||||||
unsigned char packtype;
|
unsigned char packtype;
|
||||||
bool gotVideoInfo = false;
|
bool gotVideoInfo = false;
|
||||||
|
@ -236,12 +236,12 @@ namespace Buffer{
|
||||||
if (!users.back().S.write(metadata.data, metadata.len)){
|
if (!users.back().S.write(metadata.data, metadata.len)){
|
||||||
users.back().Disconnect("failed to receive metadata!");
|
users.back().Disconnect("failed to receive metadata!");
|
||||||
}
|
}
|
||||||
if (!users.back().S.write(video_init.data, video_init.len)){
|
|
||||||
users.back().Disconnect("failed to receive video init!");
|
|
||||||
}
|
|
||||||
if (!users.back().S.write(audio_init.data, audio_init.len)){
|
if (!users.back().S.write(audio_init.data, audio_init.len)){
|
||||||
users.back().Disconnect("failed to receive audio init!");
|
users.back().Disconnect("failed to receive audio init!");
|
||||||
}
|
}
|
||||||
|
if (!users.back().S.write(video_init.data, video_init.len)){
|
||||||
|
users.back().Disconnect("failed to receive video init!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
SRC = main.cpp ../util/ddv_socket.cpp ../util/http_parser.cpp ../util/flv_tag.cpp ../util/amf.cpp
|
SRC = main.cpp ../util/socket.cpp ../util/http_parser.cpp ../util/flv_tag.cpp ../util/amf.cpp
|
||||||
OBJ = $(SRC:.cpp=.o)
|
OBJ = $(SRC:.cpp=.o)
|
||||||
OUT = DDV_Conn_HTTP
|
OUT = DDV_Conn_HTTP
|
||||||
INCLUDES =
|
INCLUDES =
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
#include <sys/epoll.h>
|
#include <sys/epoll.h>
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include "../util/ddv_socket.h"
|
#include "../util/socket.h"
|
||||||
#include "../util/http_parser.h"
|
#include "../util/http_parser.h"
|
||||||
#include "../util/flv_tag.h"
|
#include "../util/flv_tag.h"
|
||||||
#include "../util/MP4/interface.cpp"
|
#include "../util/MP4/interface.cpp"
|
||||||
|
@ -114,12 +114,12 @@ namespace Connector_HTTP{
|
||||||
}//BuildManifest
|
}//BuildManifest
|
||||||
|
|
||||||
/// Main function for Connector_HTTP
|
/// Main function for Connector_HTTP
|
||||||
int Connector_HTTP(DDV::Socket conn){
|
int Connector_HTTP(Socket::Connection conn){
|
||||||
int handler = HANDLER_PROGRESSIVE;///< The handler used for processing this request.
|
int handler = HANDLER_PROGRESSIVE;///< The handler used for processing this request.
|
||||||
bool ready4data = false;///< Set to true when streaming is to begin.
|
bool ready4data = false;///< Set to true when streaming is to begin.
|
||||||
bool inited = false;
|
bool inited = false;
|
||||||
bool progressive_has_sent_header = false;
|
bool progressive_has_sent_header = false;
|
||||||
DDV::Socket ss(-1);
|
Socket::Connection ss(-1);
|
||||||
std::string streamname;
|
std::string streamname;
|
||||||
std::string FlashBuf;
|
std::string FlashBuf;
|
||||||
std::string FlashMeta;
|
std::string FlashMeta;
|
||||||
|
@ -212,7 +212,7 @@ namespace Connector_HTTP{
|
||||||
if (ready4data){
|
if (ready4data){
|
||||||
if (!inited){
|
if (!inited){
|
||||||
//we are ready, connect the socket!
|
//we are ready, connect the socket!
|
||||||
ss = DDV::Socket(streamname);
|
ss = Socket::Connection(streamname);
|
||||||
if (!ss.connected()){
|
if (!ss.connected()){
|
||||||
#if DEBUG >= 1
|
#if DEBUG >= 1
|
||||||
fprintf(stderr, "Could not connect to server!\n");
|
fprintf(stderr, "Could not connect to server!\n");
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
SRC = main.cpp ../util/ddv_socket.cpp
|
SRC = main.cpp ../util/socket.cpp
|
||||||
OBJ = $(SRC:.cpp=.o)
|
OBJ = $(SRC:.cpp=.o)
|
||||||
OUT = DDV_Conn_RAW
|
OUT = DDV_Conn_RAW
|
||||||
INCLUDES =
|
INCLUDES =
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
/// Contains the main code for the RAW connector.
|
/// Contains the main code for the RAW connector.
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "../util/ddv_socket.h"
|
#include "../util/socket.h"
|
||||||
|
|
||||||
/// Contains the main code for the RAW connector.
|
/// Contains the main code for the RAW connector.
|
||||||
/// Expects a single commandline argument telling it which stream to connect to,
|
/// Expects a single commandline argument telling it which stream to connect to,
|
||||||
|
@ -15,7 +15,7 @@ int main(int argc, char ** argv) {
|
||||||
std::string input = "/tmp/shared_socket_";
|
std::string input = "/tmp/shared_socket_";
|
||||||
input += argv[1];
|
input += argv[1];
|
||||||
//connect to the proper stream
|
//connect to the proper stream
|
||||||
DDV::Socket S(input);
|
Socket::Connection S(input);
|
||||||
if (!S.connected()){
|
if (!S.connected()){
|
||||||
std::cout << "Could not open stream " << argv[1] << std::endl;
|
std::cout << "Could not open stream " << argv[1] << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
SRC = main.cpp ../util/ddv_socket.cpp ../util/flv_tag.cpp ../util/amf.cpp ../util/rtmpchunks.cpp ../util/crypto.cpp
|
SRC = main.cpp ../util/socket.cpp ../util/flv_tag.cpp ../util/amf.cpp ../util/rtmpchunks.cpp ../util/crypto.cpp
|
||||||
OBJ = $(SRC:.cpp=.o)
|
OBJ = $(SRC:.cpp=.o)
|
||||||
OUT = DDV_Conn_RTMP
|
OUT = DDV_Conn_RTMP
|
||||||
INCLUDES =
|
INCLUDES =
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <sys/epoll.h>
|
#include <sys/epoll.h>
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
#include "../util/ddv_socket.h"
|
#include "../util/socket.h"
|
||||||
#include "../util/flv_tag.h"
|
#include "../util/flv_tag.h"
|
||||||
#include "../util/amf.h"
|
#include "../util/amf.h"
|
||||||
#include "../util/rtmpchunks.h"
|
#include "../util/rtmpchunks.h"
|
||||||
|
@ -24,20 +24,20 @@ namespace Connector_RTMP{
|
||||||
bool inited = false; ///< Set to true when ready to connect to Buffer.
|
bool inited = false; ///< Set to true when ready to connect to Buffer.
|
||||||
bool stopparsing = false; ///< Set to true when all parsing needs to be cancelled.
|
bool stopparsing = false; ///< Set to true when all parsing needs to be cancelled.
|
||||||
|
|
||||||
DDV::Socket Socket; ///< Socket connected to user
|
Socket::Connection Socket; ///< Socket connected to user
|
||||||
std::string streamname = "/tmp/shared_socket"; ///< Stream that will be opened
|
std::string streamname = "/tmp/shared_socket"; ///< Stream that will be opened
|
||||||
void parseChunk();
|
void parseChunk();
|
||||||
int Connector_RTMP(DDV::Socket conn);
|
int Connector_RTMP(Socket::Connection conn);
|
||||||
};//Connector_RTMP namespace;
|
};//Connector_RTMP namespace;
|
||||||
|
|
||||||
|
|
||||||
/// Main Connector_RTMP function
|
/// Main Connector_RTMP function
|
||||||
int Connector_RTMP::Connector_RTMP(DDV::Socket conn){
|
int Connector_RTMP::Connector_RTMP(Socket::Connection conn){
|
||||||
Socket = conn;
|
Socket = conn;
|
||||||
unsigned int ts;
|
unsigned int ts;
|
||||||
unsigned int fts = 0;
|
unsigned int fts = 0;
|
||||||
unsigned int ftst;
|
unsigned int ftst;
|
||||||
DDV::Socket SS;
|
Socket::Connection SS;
|
||||||
FLV::Tag tag;
|
FLV::Tag tag;
|
||||||
|
|
||||||
//first timestamp set
|
//first timestamp set
|
||||||
|
@ -84,7 +84,7 @@ int Connector_RTMP::Connector_RTMP(DDV::Socket conn){
|
||||||
if (ready4data){
|
if (ready4data){
|
||||||
if (!inited){
|
if (!inited){
|
||||||
//we are ready, connect the socket!
|
//we are ready, connect the socket!
|
||||||
SS = DDV::Socket(streamname);
|
SS = Socket::Connection(streamname);
|
||||||
if (!SS.connected()){
|
if (!SS.connected()){
|
||||||
#if DEBUG >= 1
|
#if DEBUG >= 1
|
||||||
fprintf(stderr, "Could not connect to server!\n");
|
fprintf(stderr, "Could not connect to server!\n");
|
||||||
|
@ -279,22 +279,24 @@ void Connector_RTMP::parseChunk(){
|
||||||
if (tmpint & 0x400){fprintf(stderr, "AAC video support detected\n");}
|
if (tmpint & 0x400){fprintf(stderr, "AAC video support detected\n");}
|
||||||
#endif
|
#endif
|
||||||
Socket.write(RTMPStream::SendCTL(5, RTMPStream::snd_window_size));//send window acknowledgement size (msg 5)
|
Socket.write(RTMPStream::SendCTL(5, RTMPStream::snd_window_size));//send window acknowledgement size (msg 5)
|
||||||
|
Socket.write(RTMPStream::SendCTL(6, RTMPStream::rec_window_size));//send window acknowledgement size (msg 5)
|
||||||
Socket.write(RTMPStream::SendUSR(0, 1));//send UCM StreamBegin (0), stream 1
|
Socket.write(RTMPStream::SendUSR(0, 1));//send UCM StreamBegin (0), stream 1
|
||||||
//send a _result reply
|
//send a _result reply
|
||||||
AMF::Object amfreply("container", AMF::AMF0_DDV_CONTAINER);
|
AMF::Object amfreply("container", AMF::AMF0_DDV_CONTAINER);
|
||||||
amfreply.addContent(AMF::Object("", "_result"));//result success
|
amfreply.addContent(AMF::Object("", "_result"));//result success
|
||||||
amfreply.addContent(amfdata.getContent(1));//same transaction ID
|
amfreply.addContent(amfdata.getContent(1));//same transaction ID
|
||||||
amfreply.addContent(AMF::Object(""));//server properties
|
amfreply.addContent(AMF::Object(""));//server properties
|
||||||
amfreply.getContentP(2)->addContent(AMF::Object("fmsVer", "FMS/3,5,4,1004"));
|
amfreply.getContentP(2)->addContent(AMF::Object("fmsVer", "FMS/3,0,1,123"));
|
||||||
amfreply.getContentP(2)->addContent(AMF::Object("capabilities", (double)127));
|
amfreply.getContentP(2)->addContent(AMF::Object("capabilities", (double)31));
|
||||||
amfreply.getContentP(2)->addContent(AMF::Object("mode", (double)1));
|
//amfreply.getContentP(2)->addContent(AMF::Object("mode", (double)1));
|
||||||
amfreply.addContent(AMF::Object(""));//info
|
amfreply.addContent(AMF::Object(""));//info
|
||||||
amfreply.getContentP(3)->addContent(AMF::Object("level", "status"));
|
amfreply.getContentP(3)->addContent(AMF::Object("level", "status"));
|
||||||
amfreply.getContentP(3)->addContent(AMF::Object("code", "NetConnection.Connect.Success"));
|
amfreply.getContentP(3)->addContent(AMF::Object("code", "NetConnection.Connect.Success"));
|
||||||
amfreply.getContentP(3)->addContent(AMF::Object("description", "Connection succeeded."));
|
amfreply.getContentP(3)->addContent(AMF::Object("description", "Connection succeeded."));
|
||||||
|
amfreply.getContentP(3)->addContent(AMF::Object("clientid", 1337));
|
||||||
amfreply.getContentP(3)->addContent(AMF::Object("objectEncoding", objencoding));
|
amfreply.getContentP(3)->addContent(AMF::Object("objectEncoding", objencoding));
|
||||||
amfreply.getContentP(3)->addContent(AMF::Object("data", AMF::AMF0_ECMA_ARRAY));
|
//amfreply.getContentP(3)->addContent(AMF::Object("data", AMF::AMF0_ECMA_ARRAY));
|
||||||
amfreply.getContentP(3)->getContentP(4)->addContent(AMF::Object("version", "3,5,4,1004"));
|
//amfreply.getContentP(3)->getContentP(4)->addContent(AMF::Object("version", "3,5,4,1004"));
|
||||||
#if DEBUG >= 4
|
#if DEBUG >= 4
|
||||||
amfreply.Print();
|
amfreply.Print();
|
||||||
#endif
|
#endif
|
||||||
|
@ -302,7 +304,7 @@ void Connector_RTMP::parseChunk(){
|
||||||
//send onBWDone packet - no clue what it is, but real server sends it...
|
//send onBWDone packet - no clue what it is, but real server sends it...
|
||||||
amfreply = AMF::Object("container", AMF::AMF0_DDV_CONTAINER);
|
amfreply = AMF::Object("container", AMF::AMF0_DDV_CONTAINER);
|
||||||
amfreply.addContent(AMF::Object("", "onBWDone"));//result
|
amfreply.addContent(AMF::Object("", "onBWDone"));//result
|
||||||
amfreply.addContent(AMF::Object("", (double)0));//zero
|
amfreply.addContent(amfdata.getContent(1));//same transaction ID
|
||||||
amfreply.addContent(AMF::Object("", (double)0, AMF::AMF0_NULL));//null
|
amfreply.addContent(AMF::Object("", (double)0, AMF::AMF0_NULL));//null
|
||||||
Socket.write(RTMPStream::SendChunk(3, 17, next.msg_stream_id, (char)0+amfreply.Pack()));
|
Socket.write(RTMPStream::SendChunk(3, 17, next.msg_stream_id, (char)0+amfreply.Pack()));
|
||||||
parsed3 = true;
|
parsed3 = true;
|
||||||
|
@ -430,24 +432,27 @@ void Connector_RTMP::parseChunk(){
|
||||||
if (tmpint & 0x04){fprintf(stderr, "MP3 audio support detected\n");}
|
if (tmpint & 0x04){fprintf(stderr, "MP3 audio support detected\n");}
|
||||||
if (tmpint & 0x400){fprintf(stderr, "AAC video support detected\n");}
|
if (tmpint & 0x400){fprintf(stderr, "AAC video support detected\n");}
|
||||||
#endif
|
#endif
|
||||||
|
RTMPStream::chunk_snd_max = 4096;
|
||||||
|
Socket.write(RTMPStream::SendCTL(1, RTMPStream::chunk_snd_max));//send chunk size max (msg 1)
|
||||||
Socket.write(RTMPStream::SendCTL(5, RTMPStream::snd_window_size));//send window acknowledgement size (msg 5)
|
Socket.write(RTMPStream::SendCTL(5, RTMPStream::snd_window_size));//send window acknowledgement size (msg 5)
|
||||||
|
Socket.write(RTMPStream::SendCTL(6, RTMPStream::rec_window_size));//send rec window acknowledgement size (msg 6)
|
||||||
Socket.write(RTMPStream::SendUSR(0, 1));//send UCM StreamBegin (0), stream 1
|
Socket.write(RTMPStream::SendUSR(0, 1));//send UCM StreamBegin (0), stream 1
|
||||||
//send a _result reply
|
//send a _result reply
|
||||||
AMF::Object amfreply("container", AMF::AMF0_DDV_CONTAINER);
|
AMF::Object amfreply("container", AMF::AMF0_DDV_CONTAINER);
|
||||||
amfreply.addContent(AMF::Object("", "_result"));//result success
|
amfreply.addContent(AMF::Object("", "_result"));//result success
|
||||||
amfreply.addContent(amfdata.getContent(1));//same transaction ID
|
amfreply.addContent(amfdata.getContent(1));//same transaction ID
|
||||||
// amfreply.addContent(AMFType("", (double)0, 0x05));//null - command info
|
|
||||||
amfreply.addContent(AMF::Object(""));//server properties
|
amfreply.addContent(AMF::Object(""));//server properties
|
||||||
amfreply.getContentP(2)->addContent(AMF::Object("fmsVer", "FMS/3,5,4,1004"));
|
amfreply.getContentP(2)->addContent(AMF::Object("fmsVer", "FMS/3,0,1,123"));
|
||||||
amfreply.getContentP(2)->addContent(AMF::Object("capabilities", (double)127));
|
amfreply.getContentP(2)->addContent(AMF::Object("capabilities", (double)31));
|
||||||
amfreply.getContentP(2)->addContent(AMF::Object("mode", (double)1));
|
//amfreply.getContentP(2)->addContent(AMF::Object("mode", (double)1));
|
||||||
amfreply.addContent(AMF::Object(""));//info
|
amfreply.addContent(AMF::Object(""));//info
|
||||||
amfreply.getContentP(3)->addContent(AMF::Object("level", "status"));
|
amfreply.getContentP(3)->addContent(AMF::Object("level", "status"));
|
||||||
amfreply.getContentP(3)->addContent(AMF::Object("code", "NetConnection.Connect.Success"));
|
amfreply.getContentP(3)->addContent(AMF::Object("code", "NetConnection.Connect.Success"));
|
||||||
amfreply.getContentP(3)->addContent(AMF::Object("description", "Connection succeeded."));
|
amfreply.getContentP(3)->addContent(AMF::Object("description", "Connection succeeded."));
|
||||||
|
amfreply.getContentP(3)->addContent(AMF::Object("clientid", 1337));
|
||||||
amfreply.getContentP(3)->addContent(AMF::Object("objectEncoding", objencoding));
|
amfreply.getContentP(3)->addContent(AMF::Object("objectEncoding", objencoding));
|
||||||
amfreply.getContentP(3)->addContent(AMF::Object("data", AMF::AMF0_ECMA_ARRAY));
|
//amfreply.getContentP(3)->addContent(AMF::Object("data", AMF::AMF0_ECMA_ARRAY));
|
||||||
amfreply.getContentP(3)->getContentP(4)->addContent(AMF::Object("version", "3,5,4,1004"));
|
//amfreply.getContentP(3)->getContentP(4)->addContent(AMF::Object("version", "3,5,4,1004"));
|
||||||
#if DEBUG >= 4
|
#if DEBUG >= 4
|
||||||
amfreply.Print();
|
amfreply.Print();
|
||||||
#endif
|
#endif
|
||||||
|
@ -518,7 +523,7 @@ void Connector_RTMP::parseChunk(){
|
||||||
amfreply.getContentP(3)->addContent(AMF::Object("code", "NetStream.Play.Reset"));
|
amfreply.getContentP(3)->addContent(AMF::Object("code", "NetStream.Play.Reset"));
|
||||||
amfreply.getContentP(3)->addContent(AMF::Object("description", "Playing and resetting..."));
|
amfreply.getContentP(3)->addContent(AMF::Object("description", "Playing and resetting..."));
|
||||||
amfreply.getContentP(3)->addContent(AMF::Object("details", "PLS"));
|
amfreply.getContentP(3)->addContent(AMF::Object("details", "PLS"));
|
||||||
amfreply.getContentP(3)->addContent(AMF::Object("clientid", (double)1));
|
amfreply.getContentP(3)->addContent(AMF::Object("clientid", (double)1337));
|
||||||
#if DEBUG >= 4
|
#if DEBUG >= 4
|
||||||
amfreply.Print();
|
amfreply.Print();
|
||||||
#endif
|
#endif
|
||||||
|
@ -531,8 +536,7 @@ void Connector_RTMP::parseChunk(){
|
||||||
amfreply.getContentP(3)->addContent(AMF::Object("level", "status"));
|
amfreply.getContentP(3)->addContent(AMF::Object("level", "status"));
|
||||||
amfreply.getContentP(3)->addContent(AMF::Object("code", "NetStream.Play.Start"));
|
amfreply.getContentP(3)->addContent(AMF::Object("code", "NetStream.Play.Start"));
|
||||||
amfreply.getContentP(3)->addContent(AMF::Object("description", "Playing!"));
|
amfreply.getContentP(3)->addContent(AMF::Object("description", "Playing!"));
|
||||||
amfreply.getContentP(3)->addContent(AMF::Object("details", "PLS"));
|
amfreply.getContentP(3)->addContent(AMF::Object("clientid", (double)1337));
|
||||||
amfreply.getContentP(3)->addContent(AMF::Object("clientid", (double)1));
|
|
||||||
#if DEBUG >= 4
|
#if DEBUG >= 4
|
||||||
amfreply.Print();
|
amfreply.Print();
|
||||||
#endif
|
#endif
|
||||||
|
|
20
RTMP_Parser/Makefile
Normal file
20
RTMP_Parser/Makefile
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
SRC = main.cpp ../util/amf.cpp ../util/rtmpchunks.cpp ../util/crypto.cpp
|
||||||
|
OBJ = $(SRC:.cpp=.o)
|
||||||
|
OUT = RTMP_Parser
|
||||||
|
INCLUDES =
|
||||||
|
STATIC =
|
||||||
|
CCFLAGS = -Wall -Wextra -funsigned-char -g
|
||||||
|
CC = $(CROSS)g++
|
||||||
|
LD = $(CROSS)ld
|
||||||
|
AR = $(CROSS)ar
|
||||||
|
LIBS = -lssl -lcrypto
|
||||||
|
.SUFFIXES: .cpp
|
||||||
|
.PHONY: clean default
|
||||||
|
default: $(OUT)
|
||||||
|
.cpp.o:
|
||||||
|
$(CC) $(INCLUDES) $(CCFLAGS) -c $< -o $@
|
||||||
|
$(OUT): $(OBJ)
|
||||||
|
$(CC) -o $(OUT) $(OBJ) $(STATIC) $(LIBS)
|
||||||
|
clean:
|
||||||
|
rm -rf $(OBJ) $(OUT) Makefile.bak *~
|
||||||
|
|
123
RTMP_Parser/main.cpp
Normal file
123
RTMP_Parser/main.cpp
Normal file
|
@ -0,0 +1,123 @@
|
||||||
|
/// \file RTMP_Parser/main.cpp
|
||||||
|
/// Debugging tool for RTMP data.
|
||||||
|
/// Expects RTMP data of one side of the conversion through stdin, outputs human-readable information to stderr.
|
||||||
|
/// Automatically skips 3073 bytes of handshake data.
|
||||||
|
|
||||||
|
#define DEBUG 10 //maximum debugging level
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
#include <string>
|
||||||
|
#include "../util/amf.h"
|
||||||
|
#include "../util/rtmpchunks.h"
|
||||||
|
|
||||||
|
/// Debugging tool for RTMP data.
|
||||||
|
/// Expects RTMP data of one side of the conversion through stdin, outputs human-readable information to stderr.
|
||||||
|
/// Will output FLV file to stdout, if available
|
||||||
|
/// Automatically skips 3073 bytes of handshake data.
|
||||||
|
int main(){
|
||||||
|
|
||||||
|
std::string inbuffer;
|
||||||
|
while (std::cin.good()){inbuffer += std::cin.get();}//read all of std::cin to temp
|
||||||
|
inbuffer.erase(0, 3073);//strip the handshake part
|
||||||
|
RTMPStream::Chunk next;
|
||||||
|
AMF::Object amfdata("empty", AMF::AMF0_DDV_CONTAINER);
|
||||||
|
|
||||||
|
|
||||||
|
while (next.Parse(inbuffer)){
|
||||||
|
switch (next.msg_type_id){
|
||||||
|
case 0://does not exist
|
||||||
|
fprintf(stderr, "Error chunk - %i, %i, %i, %i, %i\n", next.cs_id, next.timestamp, next.real_len, next.len_left, next.msg_stream_id);
|
||||||
|
//return 0;
|
||||||
|
break;//happens when connection breaks unexpectedly
|
||||||
|
case 1://set chunk size
|
||||||
|
RTMPStream::chunk_rec_max = ntohl(*(int*)next.data.c_str());
|
||||||
|
fprintf(stderr, "CTRL: Set chunk size: %i\n", RTMPStream::chunk_rec_max);
|
||||||
|
break;
|
||||||
|
case 2://abort message - we ignore this one
|
||||||
|
fprintf(stderr, "CTRL: Abort message: %i\n", ntohl(*(int*)next.data.c_str()));
|
||||||
|
//4 bytes of stream id to drop
|
||||||
|
break;
|
||||||
|
case 3://ack
|
||||||
|
RTMPStream::snd_window_at = ntohl(*(int*)next.data.c_str());
|
||||||
|
fprintf(stderr, "CTRL: Acknowledgement: %i\n", RTMPStream::snd_window_at);
|
||||||
|
break;
|
||||||
|
case 4:{
|
||||||
|
short int ucmtype = ntohs(*(short int*)next.data.c_str());
|
||||||
|
switch (ucmtype){
|
||||||
|
case 0:
|
||||||
|
fprintf(stderr, "CTRL: User control message: stream begin %i\n", ntohl(*(int*)next.data.c_str()+2));
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
fprintf(stderr, "CTRL: User control message: stream EOF %i\n", ntohl(*(int*)next.data.c_str()+2));
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
fprintf(stderr, "CTRL: User control message: stream dry %i\n", ntohl(*(int*)next.data.c_str()+2));
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
fprintf(stderr, "CTRL: User control message: setbufferlen %i\n", ntohl(*(int*)next.data.c_str()+2));
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
fprintf(stderr, "CTRL: User control message: streamisrecorded %i\n", ntohl(*(int*)next.data.c_str()+2));
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
fprintf(stderr, "CTRL: User control message: pingrequest %i\n", ntohl(*(int*)next.data.c_str()+2));
|
||||||
|
break;
|
||||||
|
case 7:
|
||||||
|
fprintf(stderr, "CTRL: User control message: pingresponse %i\n", ntohl(*(int*)next.data.c_str()+2));
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
fprintf(stderr, "CTRL: User control message: UNKNOWN %hi - %i\n", ucmtype, ntohl(*(int*)next.data.c_str()+2));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} break;
|
||||||
|
case 5://window size of other end
|
||||||
|
RTMPStream::rec_window_size = ntohl(*(int*)next.data.c_str());
|
||||||
|
RTMPStream::rec_window_at = RTMPStream::rec_cnt;
|
||||||
|
fprintf(stderr, "CTRL: Window size: %i\n", RTMPStream::rec_window_size);
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
RTMPStream::snd_window_size = ntohl(*(int*)next.data.c_str());
|
||||||
|
//4 bytes window size, 1 byte limit type (ignored)
|
||||||
|
fprintf(stderr, "CTRL: Set peer bandwidth: %i\n", RTMPStream::snd_window_size);
|
||||||
|
break;
|
||||||
|
case 8:
|
||||||
|
fprintf(stderr, "Received %i bytes audio data\n", next.len);
|
||||||
|
break;
|
||||||
|
case 9:
|
||||||
|
fprintf(stderr, "Received %i bytes video data\n", next.len);
|
||||||
|
break;
|
||||||
|
case 15:
|
||||||
|
fprintf(stderr, "Received AFM3 data message\n");
|
||||||
|
break;
|
||||||
|
case 16:
|
||||||
|
fprintf(stderr, "Received AFM3 shared object\n");
|
||||||
|
break;
|
||||||
|
case 17:
|
||||||
|
fprintf(stderr, "Received AFM3 command message\n");
|
||||||
|
break;
|
||||||
|
case 18:{
|
||||||
|
fprintf(stderr, "Received AFM0 data message (metadata):\n");
|
||||||
|
amfdata = AMF::parse(next.data);
|
||||||
|
amfdata.Print();
|
||||||
|
} break;
|
||||||
|
case 19:
|
||||||
|
fprintf(stderr, "Received AFM0 shared object\n");
|
||||||
|
break;
|
||||||
|
case 20:{//AMF0 command message
|
||||||
|
fprintf(stderr, "Received AFM0 command message:\n");
|
||||||
|
amfdata = AMF::parse(next.data);
|
||||||
|
amfdata.Print();
|
||||||
|
} break;
|
||||||
|
case 22:
|
||||||
|
fprintf(stderr, "Received aggregate message\n");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
fprintf(stderr, "Unknown chunk received! Probably protocol corruption, stopping parsing of incoming data.\n");
|
||||||
|
return 1;
|
||||||
|
break;
|
||||||
|
}//switch for type of chunk
|
||||||
|
}//while chunk parsed
|
||||||
|
fprintf(stderr, "No more readable data\n");
|
||||||
|
return 0;
|
||||||
|
}//main
|
|
@ -2,6 +2,7 @@
|
||||||
/// Holds all code for the AMF namespace.
|
/// Holds all code for the AMF namespace.
|
||||||
|
|
||||||
#include "amf.h"
|
#include "amf.h"
|
||||||
|
#include <cstdio> //needed for stderr only
|
||||||
|
|
||||||
/// Returns the std::string Indice for the current object, if available.
|
/// Returns the std::string Indice for the current object, if available.
|
||||||
/// Returns an empty string if no indice exists.
|
/// Returns an empty string if no indice exists.
|
||||||
|
|
|
@ -7,7 +7,6 @@
|
||||||
#include <fcntl.h> //for Tag::FileLoader
|
#include <fcntl.h> //for Tag::FileLoader
|
||||||
#include <stdlib.h> //malloc
|
#include <stdlib.h> //malloc
|
||||||
#include <string.h> //memcpy
|
#include <string.h> //memcpy
|
||||||
#include "ddv_socket.h" //socket functions
|
|
||||||
|
|
||||||
char FLV::Header[13]; ///< Holds the last FLV header parsed.
|
char FLV::Header[13]; ///< Holds the last FLV header parsed.
|
||||||
bool FLV::Parse_Error = false; ///< This variable is set to true if a problem is encountered while parsing the FLV.
|
bool FLV::Parse_Error = false; ///< This variable is set to true if a problem is encountered while parsing the FLV.
|
||||||
|
@ -261,7 +260,7 @@ bool FLV::Tag::MemLoader(char * D, unsigned int S, unsigned int & P){
|
||||||
/// \param sofar Current amount read.
|
/// \param sofar Current amount read.
|
||||||
/// \param sock Socket to read from.
|
/// \param sock Socket to read from.
|
||||||
/// \return True if count bytes are read succesfully, false otherwise.
|
/// \return True if count bytes are read succesfully, false otherwise.
|
||||||
bool FLV::Tag::SockReadUntil(char * buffer, unsigned int count, unsigned int & sofar, DDV::Socket & sock){
|
bool FLV::Tag::SockReadUntil(char * buffer, unsigned int count, unsigned int & sofar, Socket::Connection & sock){
|
||||||
if (sofar == count){return true;}
|
if (sofar == count){return true;}
|
||||||
if (!sock.read(buffer + sofar,count-sofar)){
|
if (!sock.read(buffer + sofar,count-sofar)){
|
||||||
if (errno != EWOULDBLOCK){
|
if (errno != EWOULDBLOCK){
|
||||||
|
@ -284,7 +283,7 @@ bool FLV::Tag::SockReadUntil(char * buffer, unsigned int count, unsigned int & s
|
||||||
/// While this function returns false, the Tag might not contain valid data.
|
/// While this function returns false, the Tag might not contain valid data.
|
||||||
/// \param sock The socket to read from.
|
/// \param sock The socket to read from.
|
||||||
/// \return True if a whole tag is succesfully read, false otherwise.
|
/// \return True if a whole tag is succesfully read, false otherwise.
|
||||||
bool FLV::Tag::SockLoader(DDV::Socket sock){
|
bool FLV::Tag::SockLoader(Socket::Connection sock){
|
||||||
if (buf < 15){data = (char*)realloc(data, 15); buf = 15;}
|
if (buf < 15){data = (char*)realloc(data, 15); buf = 15;}
|
||||||
if (done){
|
if (done){
|
||||||
if (SockReadUntil(data, 11, sofar, sock)){
|
if (SockReadUntil(data, 11, sofar, sock)){
|
||||||
|
@ -325,7 +324,7 @@ bool FLV::Tag::SockLoader(DDV::Socket sock){
|
||||||
/// \param sock The socket to read from.
|
/// \param sock The socket to read from.
|
||||||
/// \return True if a whole tag is succesfully read, false otherwise.
|
/// \return True if a whole tag is succesfully read, false otherwise.
|
||||||
bool FLV::Tag::SockLoader(int sock){
|
bool FLV::Tag::SockLoader(int sock){
|
||||||
return SockLoader(DDV::Socket(sock));
|
return SockLoader(Socket::Connection(sock));
|
||||||
}//Tag::SockLoader
|
}//Tag::SockLoader
|
||||||
|
|
||||||
/// Helper function for FLV::FileLoader.
|
/// Helper function for FLV::FileLoader.
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
/// Holds all headers for the FLV namespace.
|
/// Holds all headers for the FLV namespace.
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "ddv_socket.h"
|
#include "socket.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
/// This namespace holds all FLV-parsing related functionality.
|
/// This namespace holds all FLV-parsing related functionality.
|
||||||
|
@ -31,7 +31,7 @@ namespace FLV {
|
||||||
//loader functions
|
//loader functions
|
||||||
bool MemLoader(char * D, unsigned int S, unsigned int & P);
|
bool MemLoader(char * D, unsigned int S, unsigned int & P);
|
||||||
bool SockLoader(int sock);
|
bool SockLoader(int sock);
|
||||||
bool SockLoader(DDV::Socket sock);
|
bool SockLoader(Socket::Connection sock);
|
||||||
bool FileLoader(FILE * f);
|
bool FileLoader(FILE * f);
|
||||||
protected:
|
protected:
|
||||||
int buf; ///< Maximum length of buffer space.
|
int buf; ///< Maximum length of buffer space.
|
||||||
|
@ -39,7 +39,7 @@ namespace FLV {
|
||||||
unsigned int sofar; ///< How many bytes are read sofar?
|
unsigned int sofar; ///< How many bytes are read sofar?
|
||||||
//loader helper functions
|
//loader helper functions
|
||||||
bool MemReadUntil(char * buffer, unsigned int count, unsigned int & sofar, char * D, unsigned int S, unsigned int & P);
|
bool MemReadUntil(char * buffer, unsigned int count, unsigned int & sofar, char * D, unsigned int S, unsigned int & P);
|
||||||
bool SockReadUntil(char * buffer, unsigned int count, unsigned int & sofar, DDV::Socket & sock);
|
bool SockReadUntil(char * buffer, unsigned int count, unsigned int & sofar, Socket::Connection & sock);
|
||||||
bool FileReadUntil(char * buffer, unsigned int count, unsigned int & sofar, FILE * f);
|
bool FileReadUntil(char * buffer, unsigned int count, unsigned int & sofar, FILE * f);
|
||||||
};//Tag
|
};//Tag
|
||||||
|
|
||||||
|
|
|
@ -127,11 +127,11 @@ void HTTP::Parser::SetVar(std::string i, std::string v){
|
||||||
vars[i] = v;
|
vars[i] = v;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Attempt to read a whole HTTP request or response from DDV::Socket sock.
|
/// Attempt to read a whole HTTP request or response from Socket::Connection.
|
||||||
/// \param sock The socket to use.
|
/// \param sock The socket to use.
|
||||||
/// \param nonblock When true, will not block even if the socket is blocking.
|
/// \param nonblock When true, will not block even if the socket is blocking.
|
||||||
/// \return True of a whole request or response was read, false otherwise.
|
/// \return True of a whole request or response was read, false otherwise.
|
||||||
bool HTTP::Parser::Read(DDV::Socket & sock, bool nonblock){
|
bool HTTP::Parser::Read(Socket::Connection & sock, bool nonblock){
|
||||||
if (nonblock && (sock.ready() < 1)){return parse();}
|
if (nonblock && (sock.ready() < 1)){return parse();}
|
||||||
sock.read(HTTPbuffer);
|
sock.read(HTTPbuffer);
|
||||||
return parse();
|
return parse();
|
||||||
|
@ -206,20 +206,20 @@ bool HTTP::Parser::parse(){
|
||||||
|
|
||||||
/// Sends data as response to conn.
|
/// Sends data as response to conn.
|
||||||
/// The response is automatically first build using HTTP::Parser::BuildResponse().
|
/// The response is automatically first build using HTTP::Parser::BuildResponse().
|
||||||
/// \param conn The DDV::Socket to send the response over.
|
/// \param conn The Socket::Connection to send the response over.
|
||||||
/// \param code The HTTP response code. Usually you want 200.
|
/// \param code The HTTP response code. Usually you want 200.
|
||||||
/// \param message The HTTP response message. Usually you want "OK".
|
/// \param message The HTTP response message. Usually you want "OK".
|
||||||
void HTTP::Parser::SendResponse(DDV::Socket & conn, std::string code, std::string message){
|
void HTTP::Parser::SendResponse(Socket::Connection & conn, std::string code, std::string message){
|
||||||
std::string tmp = BuildResponse(code, message);
|
std::string tmp = BuildResponse(code, message);
|
||||||
conn.write(tmp);
|
conn.write(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sends data as HTTP/1.1 bodypart to conn.
|
/// Sends data as HTTP/1.1 bodypart to conn.
|
||||||
/// HTTP/1.1 chunked encoding is automatically applied if needed.
|
/// HTTP/1.1 chunked encoding is automatically applied if needed.
|
||||||
/// \param conn The DDV::Socket to send the part over.
|
/// \param conn The Socket::Connection to send the part over.
|
||||||
/// \param buffer The buffer to send.
|
/// \param buffer The buffer to send.
|
||||||
/// \param len The length of the buffer.
|
/// \param len The length of the buffer.
|
||||||
void HTTP::Parser::SendBodyPart(DDV::Socket & conn, char * buffer, int len){
|
void HTTP::Parser::SendBodyPart(Socket::Connection & conn, char * buffer, int len){
|
||||||
std::string tmp;
|
std::string tmp;
|
||||||
tmp.append(buffer, len);
|
tmp.append(buffer, len);
|
||||||
SendBodyPart(conn, tmp);
|
SendBodyPart(conn, tmp);
|
||||||
|
@ -227,9 +227,9 @@ void HTTP::Parser::SendBodyPart(DDV::Socket & conn, char * buffer, int len){
|
||||||
|
|
||||||
/// Sends data as HTTP/1.1 bodypart to conn.
|
/// Sends data as HTTP/1.1 bodypart to conn.
|
||||||
/// HTTP/1.1 chunked encoding is automatically applied if needed.
|
/// HTTP/1.1 chunked encoding is automatically applied if needed.
|
||||||
/// \param conn The DDV::Socket to send the part over.
|
/// \param conn The Socket::Connection to send the part over.
|
||||||
/// \param bodypart The data to send.
|
/// \param bodypart The data to send.
|
||||||
void HTTP::Parser::SendBodyPart(DDV::Socket & conn, std::string bodypart){
|
void HTTP::Parser::SendBodyPart(Socket::Connection & conn, std::string bodypart){
|
||||||
if (protocol == "HTTP/1.1"){
|
if (protocol == "HTTP/1.1"){
|
||||||
static char len[10];
|
static char len[10];
|
||||||
int sizelen;
|
int sizelen;
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "ddv_socket.h"
|
#include "socket.h"
|
||||||
|
|
||||||
/// Holds all HTTP processing related code.
|
/// Holds all HTTP processing related code.
|
||||||
namespace HTTP{
|
namespace HTTP{
|
||||||
|
@ -14,7 +14,7 @@ namespace HTTP{
|
||||||
class Parser{
|
class Parser{
|
||||||
public:
|
public:
|
||||||
Parser();
|
Parser();
|
||||||
bool Read(DDV::Socket & sock, bool nonblock = true);
|
bool Read(Socket::Connection & sock, bool nonblock = true);
|
||||||
bool Read(FILE * F);
|
bool Read(FILE * F);
|
||||||
std::string GetHeader(std::string i);
|
std::string GetHeader(std::string i);
|
||||||
std::string GetVar(std::string i);
|
std::string GetVar(std::string i);
|
||||||
|
@ -25,9 +25,9 @@ namespace HTTP{
|
||||||
void SetBody(char * buffer, int len);
|
void SetBody(char * buffer, int len);
|
||||||
std::string BuildRequest();
|
std::string BuildRequest();
|
||||||
std::string BuildResponse(std::string code, std::string message);
|
std::string BuildResponse(std::string code, std::string message);
|
||||||
void SendResponse(DDV::Socket & conn, std::string code, std::string message);
|
void SendResponse(Socket::Connection & conn, std::string code, std::string message);
|
||||||
void SendBodyPart(DDV::Socket & conn, char * buffer, int len);
|
void SendBodyPart(Socket::Connection & conn, char * buffer, int len);
|
||||||
void SendBodyPart(DDV::Socket & conn, std::string bodypart);
|
void SendBodyPart(Socket::Connection & conn, std::string bodypart);
|
||||||
void Clean();
|
void Clean();
|
||||||
bool CleanForNext();
|
bool CleanForNext();
|
||||||
std::string body;
|
std::string body;
|
||||||
|
|
|
@ -42,7 +42,7 @@ std::string RTMPStream::Chunk::Pack(){
|
||||||
unsigned int tmpi;
|
unsigned int tmpi;
|
||||||
unsigned char chtype = 0x00;
|
unsigned char chtype = 0x00;
|
||||||
timestamp -= firsttime;
|
timestamp -= firsttime;
|
||||||
if (prev.cs_id == cs_id){
|
if ((prev.msg_type_id > 0) && (prev.cs_id == cs_id)){
|
||||||
if (msg_stream_id == prev.msg_stream_id){
|
if (msg_stream_id == prev.msg_stream_id){
|
||||||
chtype = 0x40;//do not send msg_stream_id
|
chtype = 0x40;//do not send msg_stream_id
|
||||||
if (len == prev.len){
|
if (len == prev.len){
|
||||||
|
@ -292,6 +292,7 @@ bool RTMPStream::Chunk::Parse(std::string & indata){
|
||||||
break;
|
break;
|
||||||
case 0x40:
|
case 0x40:
|
||||||
if (indata.size() < i+7) return false; //can't read whole header
|
if (indata.size() < i+7) return false; //can't read whole header
|
||||||
|
if (prev.msg_type_id == 0){fprintf(stderr, "Warning: Header type 0x40 with no valid previous chunk!\n");}
|
||||||
timestamp = indata[i++]*256*256;
|
timestamp = indata[i++]*256*256;
|
||||||
timestamp += indata[i++]*256;
|
timestamp += indata[i++]*256;
|
||||||
timestamp += indata[i++];
|
timestamp += indata[i++];
|
||||||
|
@ -305,6 +306,7 @@ bool RTMPStream::Chunk::Parse(std::string & indata){
|
||||||
break;
|
break;
|
||||||
case 0x80:
|
case 0x80:
|
||||||
if (indata.size() < i+3) return false; //can't read whole header
|
if (indata.size() < i+3) return false; //can't read whole header
|
||||||
|
if (prev.msg_type_id == 0){fprintf(stderr, "Warning: Header type 0x80 with no valid previous chunk!\n");}
|
||||||
timestamp = indata[i++]*256*256;
|
timestamp = indata[i++]*256*256;
|
||||||
timestamp += indata[i++]*256;
|
timestamp += indata[i++]*256;
|
||||||
timestamp += indata[i++];
|
timestamp += indata[i++];
|
||||||
|
@ -315,6 +317,7 @@ bool RTMPStream::Chunk::Parse(std::string & indata){
|
||||||
msg_stream_id = prev.msg_stream_id;
|
msg_stream_id = prev.msg_stream_id;
|
||||||
break;
|
break;
|
||||||
case 0xC0:
|
case 0xC0:
|
||||||
|
if (prev.msg_type_id == 0){fprintf(stderr, "Warning: Header type 0xC0 with no valid previous chunk!\n");}
|
||||||
timestamp = prev.timestamp;
|
timestamp = prev.timestamp;
|
||||||
len = prev.len;
|
len = prev.len;
|
||||||
len_left = prev.len_left;
|
len_left = prev.len_left;
|
||||||
|
|
|
@ -21,14 +21,14 @@
|
||||||
#error "No configuration file section was set!"
|
#error "No configuration file section was set!"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "ddv_socket.h" //DDVTech Socket wrapper
|
#include "socket.h" //Socket library
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#define defstr(x) #x ///< converts a define name to string
|
#define defstr(x) #x ///< converts a define name to string
|
||||||
#define defstrh(x) "[" defstr(x) "]" ///< converts define name to [string]
|
#define defstrh(x) "[" defstr(x) "]" ///< converts define name to [string]
|
||||||
DDV::ServerSocket server_socket(-1); ///< Placeholder for the server socket
|
Socket::Server server_socket(-1); ///< Placeholder for the server socket
|
||||||
|
|
||||||
/// Basic signal handler. Disconnects the server_socket if it receives
|
/// Basic signal handler. Disconnects the server_socket if it receives
|
||||||
/// a SIGINT, SIGHUP or SIGTERM signal, but does nothing for SIGPIPE.
|
/// a SIGINT, SIGHUP or SIGTERM signal, but does nothing for SIGPIPE.
|
||||||
|
@ -53,7 +53,7 @@ void signal_handler (int signum){
|
||||||
/// The default port is set by define #DEFAULT_PORT.
|
/// The default port is set by define #DEFAULT_PORT.
|
||||||
/// The configuration file section is set by define #CONFIGSECT.
|
/// The configuration file section is set by define #CONFIGSECT.
|
||||||
int main(int argc, char ** argv){
|
int main(int argc, char ** argv){
|
||||||
DDV::Socket S;//placeholder for incoming connections
|
Socket::Connection S;//placeholder for incoming connections
|
||||||
|
|
||||||
//setup signal handler
|
//setup signal handler
|
||||||
struct sigaction new_action;
|
struct sigaction new_action;
|
||||||
|
@ -135,7 +135,7 @@ int main(int argc, char ** argv){
|
||||||
}//configuration
|
}//configuration
|
||||||
|
|
||||||
//setup a new server socket, for the correct interface and port
|
//setup a new server socket, for the correct interface and port
|
||||||
server_socket = DDV::ServerSocket(listen_port, interface);
|
server_socket = Socket::Server(listen_port, interface);
|
||||||
#if DEBUG >= 3
|
#if DEBUG >= 3
|
||||||
fprintf(stderr, "Made a listening socket on %s:%i...\n", interface.c_str(), listen_port);
|
fprintf(stderr, "Made a listening socket on %s:%i...\n", interface.c_str(), listen_port);
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -1,40 +1,42 @@
|
||||||
/// \file ddv_socket.cpp
|
/// \file socket.cpp
|
||||||
/// Holds all code for the DDV namespace.
|
/// A handy Socket wrapper library.
|
||||||
|
/// Written by Jaron Vietor in 2010 for DDVTech
|
||||||
|
|
||||||
#include "ddv_socket.h"
|
#include "socket.h"
|
||||||
|
|
||||||
/// Create a new base socket. This is a basic constructor for converting any valid socket to a DDV::Socket.
|
/// Create a new base socket. This is a basic constructor for converting any valid socket to a Socket::Connection.
|
||||||
/// \param sockNo Integer representing the socket to convert.
|
/// \param sockNo Integer representing the socket to convert.
|
||||||
DDV::Socket::Socket(int sockNo){
|
Socket::Connection::Connection(int sockNo){
|
||||||
sock = sockNo;
|
sock = sockNo;
|
||||||
Error = false;
|
Error = false;
|
||||||
Blocking = false;
|
Blocking = false;
|
||||||
}//DDV::Socket basic constructor
|
}//Socket::Connection basic constructor
|
||||||
|
|
||||||
/// Create a new disconnected base socket. This is a basic constructor for placeholder purposes.
|
/// Create a new disconnected base socket. This is a basic constructor for placeholder purposes.
|
||||||
/// A socket created like this is always disconnected and should/could be overwritten at some point.
|
/// A socket created like this is always disconnected and should/could be overwritten at some point.
|
||||||
DDV::Socket::Socket(){
|
Socket::Connection::Connection(){
|
||||||
sock = -1;
|
sock = -1;
|
||||||
Error = false;
|
Error = false;
|
||||||
Blocking = false;
|
Blocking = false;
|
||||||
}//DDV::Socket basic constructor
|
}//Socket::Connection basic constructor
|
||||||
|
|
||||||
/// Close connection. The internal socket is closed and then set to -1.
|
/// Close connection. The internal socket is closed and then set to -1.
|
||||||
void DDV::Socket::close(){
|
void Socket::Connection::close(){
|
||||||
#if DEBUG >= 3
|
#if DEBUG >= 4
|
||||||
fprintf(stderr, "Socket closed.\n");
|
fprintf(stderr, "Socket closed.\n");
|
||||||
#endif
|
#endif
|
||||||
|
shutdown(sock, SHUT_RDWR);
|
||||||
::close(sock);
|
::close(sock);
|
||||||
sock = -1;
|
sock = -1;
|
||||||
}//DDV::Socket::close
|
}//Socket::Connection::close
|
||||||
|
|
||||||
/// Returns internal socket number.
|
/// Returns internal socket number.
|
||||||
int DDV::Socket::getSocket(){return sock;}
|
int Socket::Connection::getSocket(){return sock;}
|
||||||
|
|
||||||
/// Create a new Unix Socket. This socket will (try to) connect to the given address right away.
|
/// Create a new Unix Socket. This socket will (try to) connect to the given address right away.
|
||||||
/// \param address String containing the location of the Unix socket to connect to.
|
/// \param address String containing the location of the Unix socket to connect to.
|
||||||
/// \param nonblock Whether the socket should be nonblocking. False by default.
|
/// \param nonblock Whether the socket should be nonblocking. False by default.
|
||||||
DDV::Socket::Socket(std::string address, bool nonblock){
|
Socket::Connection::Connection(std::string address, bool nonblock){
|
||||||
sock = socket(PF_UNIX, SOCK_STREAM, 0);
|
sock = socket(PF_UNIX, SOCK_STREAM, 0);
|
||||||
if (sock < 0){
|
if (sock < 0){
|
||||||
#if DEBUG >= 1
|
#if DEBUG >= 1
|
||||||
|
@ -60,11 +62,11 @@ DDV::Socket::Socket(std::string address, bool nonblock){
|
||||||
#endif
|
#endif
|
||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
}//DDV::Socket Unix Contructor
|
}//Socket::Connection Unix Contructor
|
||||||
|
|
||||||
/// Returns the ready-state for this socket.
|
/// Returns the ready-state for this socket.
|
||||||
/// \returns 1 if data is waiting to be read, -1 if not connected, 0 otherwise.
|
/// \returns 1 if data is waiting to be read, -1 if not connected, 0 otherwise.
|
||||||
signed int DDV::Socket::ready(){
|
signed int Socket::Connection::ready(){
|
||||||
if (sock < 0) return -1;
|
if (sock < 0) return -1;
|
||||||
char tmp;
|
char tmp;
|
||||||
int preflags = fcntl(sock, F_GETFL, 0);
|
int preflags = fcntl(sock, F_GETFL, 0);
|
||||||
|
@ -94,7 +96,7 @@ signed int DDV::Socket::ready(){
|
||||||
/// The connection status is updated after every read/write attempt, when errors occur
|
/// The connection status is updated after every read/write attempt, when errors occur
|
||||||
/// and when the socket is closed manually.
|
/// and when the socket is closed manually.
|
||||||
/// \returns True if socket is connected, false otherwise.
|
/// \returns True if socket is connected, false otherwise.
|
||||||
bool DDV::Socket::connected(){
|
bool Socket::Connection::connected(){
|
||||||
return (sock >= 0);
|
return (sock >= 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,7 +106,7 @@ bool DDV::Socket::connected(){
|
||||||
/// \param buffer Location of the buffer to write from.
|
/// \param buffer Location of the buffer to write from.
|
||||||
/// \param len Amount of bytes to write.
|
/// \param len Amount of bytes to write.
|
||||||
/// \returns True if the whole write was succesfull, false otherwise.
|
/// \returns True if the whole write was succesfull, false otherwise.
|
||||||
bool DDV::Socket::write(const void * buffer, int len){
|
bool Socket::Connection::write(const void * buffer, int len){
|
||||||
int sofar = 0;
|
int sofar = 0;
|
||||||
if (sock < 0){return false;}
|
if (sock < 0){return false;}
|
||||||
while (sofar != len){
|
while (sofar != len){
|
||||||
|
@ -130,38 +132,52 @@ bool DDV::Socket::write(const void * buffer, int len){
|
||||||
/// \param buffer Location of the buffer to read to.
|
/// \param buffer Location of the buffer to read to.
|
||||||
/// \param len Amount of bytes to read.
|
/// \param len Amount of bytes to read.
|
||||||
/// \returns True if the whole read was succesfull, false otherwise.
|
/// \returns True if the whole read was succesfull, false otherwise.
|
||||||
bool DDV::Socket::read(void * buffer, int len){
|
bool Socket::Connection::read(void * buffer, int len){
|
||||||
int sofar = 0;
|
int sofar = 0;
|
||||||
if (sock < 0){return false;}
|
if (sock < 0){return false;}
|
||||||
while (sofar != len){
|
while (sofar != len){
|
||||||
int r = recv(sock, (char*)buffer + sofar, len-sofar, 0);
|
int r = recv(sock, (char*)buffer + sofar, len-sofar, 0);
|
||||||
if (r <= 0){
|
if (r < 0){
|
||||||
|
switch (errno){
|
||||||
|
case EWOULDBLOCK: return 0; break;
|
||||||
|
default:
|
||||||
Error = true;
|
Error = true;
|
||||||
#if DEBUG >= 2
|
#if DEBUG >= 2
|
||||||
fprintf(stderr, "Could not read data! Error: %s\n", strerror(errno));
|
fprintf(stderr, "Could not read data! Error %i: %s\n", r, strerror(errno));
|
||||||
|
#endif
|
||||||
|
close();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}else{
|
||||||
|
if (r == 0){
|
||||||
|
Error = true;
|
||||||
|
#if DEBUG >= 2
|
||||||
|
fprintf(stderr, "Could not read data! Socket is closed.\n");
|
||||||
#endif
|
#endif
|
||||||
close();
|
close();
|
||||||
return false;
|
return false;
|
||||||
}else{
|
}
|
||||||
sofar += r;
|
sofar += r;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}//DDV::Socket::read
|
}//Socket::Connection::read
|
||||||
|
|
||||||
/// Read call that is compatible with file access syntax. This function simply calls the other read function.
|
/// Read call that is compatible with file access syntax. This function simply calls the other read function.
|
||||||
bool DDV::Socket::read(void * buffer, int width, int count){return read(buffer, width*count);}
|
bool Socket::Connection::read(void * buffer, int width, int count){return read(buffer, width*count);}
|
||||||
/// Write call that is compatible with file access syntax. This function simply calls the other write function.
|
/// Write call that is compatible with file access syntax. This function simply calls the other write function.
|
||||||
bool DDV::Socket::write(void * buffer, int width, int count){return write(buffer, width*count);}
|
bool Socket::Connection::write(void * buffer, int width, int count){return write(buffer, width*count);}
|
||||||
/// Write call that is compatible with std::string. This function simply calls the other write function.
|
/// Write call that is compatible with std::string. This function simply calls the other write function.
|
||||||
bool DDV::Socket::write(const std::string data){return write(data.c_str(), data.size());}
|
bool Socket::Connection::write(const std::string data){return write(data.c_str(), data.size());}
|
||||||
|
|
||||||
/// Incremental write call. This function tries to write len bytes to the socket from the buffer,
|
/// Incremental write call. This function tries to write len bytes to the socket from the buffer,
|
||||||
/// returning the amount of bytes it actually wrote.
|
/// returning the amount of bytes it actually wrote.
|
||||||
/// \param buffer Location of the buffer to write from.
|
/// \param buffer Location of the buffer to write from.
|
||||||
/// \param len Amount of bytes to write.
|
/// \param len Amount of bytes to write.
|
||||||
/// \returns The amount of bytes actually written.
|
/// \returns The amount of bytes actually written.
|
||||||
int DDV::Socket::iwrite(void * buffer, int len){
|
int Socket::Connection::iwrite(void * buffer, int len){
|
||||||
|
if (sock < 0){return 0;}
|
||||||
int r = send(sock, buffer, len, 0);
|
int r = send(sock, buffer, len, 0);
|
||||||
if (r < 0){
|
if (r < 0){
|
||||||
switch (errno){
|
switch (errno){
|
||||||
|
@ -178,14 +194,15 @@ int DDV::Socket::iwrite(void * buffer, int len){
|
||||||
}
|
}
|
||||||
if (r == 0){close();}
|
if (r == 0){close();}
|
||||||
return r;
|
return r;
|
||||||
}//DDV::Socket::iwrite
|
}//Socket::Connection::iwrite
|
||||||
|
|
||||||
/// Incremental read call. This function tries to read len bytes to the buffer from the socket,
|
/// Incremental read call. This function tries to read len bytes to the buffer from the socket,
|
||||||
/// returning the amount of bytes it actually read.
|
/// returning the amount of bytes it actually read.
|
||||||
/// \param buffer Location of the buffer to read to.
|
/// \param buffer Location of the buffer to read to.
|
||||||
/// \param len Amount of bytes to read.
|
/// \param len Amount of bytes to read.
|
||||||
/// \returns The amount of bytes actually read.
|
/// \returns The amount of bytes actually read.
|
||||||
int DDV::Socket::iread(void * buffer, int len){
|
int Socket::Connection::iread(void * buffer, int len){
|
||||||
|
if (sock < 0){return 0;}
|
||||||
int r = recv(sock, buffer, len, 0);
|
int r = recv(sock, buffer, len, 0);
|
||||||
if (r < 0){
|
if (r < 0){
|
||||||
switch (errno){
|
switch (errno){
|
||||||
|
@ -202,14 +219,14 @@ int DDV::Socket::iread(void * buffer, int len){
|
||||||
}
|
}
|
||||||
if (r == 0){close();}
|
if (r == 0){close();}
|
||||||
return r;
|
return r;
|
||||||
}//DDV::Socket::iread
|
}//Socket::Connection::iread
|
||||||
|
|
||||||
/// Read call that is compatible with std::string.
|
/// Read call that is compatible with std::string.
|
||||||
/// Data is read using iread (which is nonblocking if the DDV::Socket itself is),
|
/// Data is read using iread (which is nonblocking if the Socket::Connection itself is),
|
||||||
/// then appended to end of buffer.
|
/// then appended to end of buffer. This functions reads at least one byte before returning.
|
||||||
/// \param buffer std::string to append data to.
|
/// \param buffer std::string to append data to.
|
||||||
/// \return True if new data arrived, false otherwise.
|
/// \return True if new data arrived, false otherwise.
|
||||||
bool DDV::Socket::read(std::string & buffer){
|
bool Socket::Connection::read(std::string & buffer){
|
||||||
char cbuffer[5000];
|
char cbuffer[5000];
|
||||||
if (!read(cbuffer, 1)){return false;}
|
if (!read(cbuffer, 1)){return false;}
|
||||||
int num = iread(cbuffer+1, 4999);
|
int num = iread(cbuffer+1, 4999);
|
||||||
|
@ -221,19 +238,62 @@ bool DDV::Socket::read(std::string & buffer){
|
||||||
return true;
|
return true;
|
||||||
}//read
|
}//read
|
||||||
|
|
||||||
/// Create a new base ServerSocket. The socket is never connected, and a placeholder for later connections.
|
/// Read call that is compatible with std::string.
|
||||||
DDV::ServerSocket::ServerSocket(){
|
/// Data is read using iread (which is nonblocking if the Socket::Connection itself is),
|
||||||
sock = -1;
|
/// then appended to end of buffer.
|
||||||
}//DDV::ServerSocket base Constructor
|
/// \param buffer std::string to append data to.
|
||||||
|
/// \return True if new data arrived, false otherwise.
|
||||||
|
bool Socket::Connection::iread(std::string & buffer){
|
||||||
|
char cbuffer[5000];
|
||||||
|
int num = iread(cbuffer, 5000);
|
||||||
|
if (num < 1){return false;}
|
||||||
|
buffer.append(cbuffer, num);
|
||||||
|
return true;
|
||||||
|
}//iread
|
||||||
|
|
||||||
/// Create a new TCP ServerSocket. The socket is immediately bound and set to listen.
|
/// Incremental write call that is compatible with std::string.
|
||||||
|
/// Data is written using iwrite (which is nonblocking if the Socket::Connection itself is),
|
||||||
|
/// then removed from front of buffer.
|
||||||
|
/// \param buffer std::string to remove data from.
|
||||||
|
/// \return True if more data was sent, false otherwise.
|
||||||
|
bool Socket::Connection::iwrite(std::string & buffer){
|
||||||
|
if (buffer.size() < 1){return false;}
|
||||||
|
int tmp = iwrite((void*)buffer.c_str(), buffer.size());
|
||||||
|
if (tmp < 1){return false;}
|
||||||
|
buffer = buffer.substr(tmp);
|
||||||
|
return true;
|
||||||
|
}//iwrite
|
||||||
|
|
||||||
|
/// Write call that is compatible with std::string.
|
||||||
|
/// Data is written using write (which is always blocking),
|
||||||
|
/// then removed from front of buffer.
|
||||||
|
/// \param buffer std::string to remove data from.
|
||||||
|
/// \return True if more data was sent, false otherwise.
|
||||||
|
bool Socket::Connection::swrite(std::string & buffer){
|
||||||
|
if (buffer.size() < 1){return false;}
|
||||||
|
bool tmp = write((void*)buffer.c_str(), buffer.size());
|
||||||
|
if (tmp){buffer = "";}
|
||||||
|
return tmp;
|
||||||
|
}//write
|
||||||
|
|
||||||
|
/// Gets hostname for connection, if available.
|
||||||
|
std::string Socket::Connection::getHost(){
|
||||||
|
return remotehost;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a new base Server. The socket is never connected, and a placeholder for later connections.
|
||||||
|
Socket::Server::Server(){
|
||||||
|
sock = -1;
|
||||||
|
}//Socket::Server base Constructor
|
||||||
|
|
||||||
|
/// Create a new TCP Server. The socket is immediately bound and set to listen.
|
||||||
/// A maximum of 100 connections will be accepted between accept() calls.
|
/// A maximum of 100 connections will be accepted between accept() calls.
|
||||||
/// Any further connections coming in will be dropped.
|
/// Any further connections coming in will be dropped.
|
||||||
/// \param port The TCP port to listen on
|
/// \param port The TCP port to listen on
|
||||||
/// \param hostname (optional) The interface to bind to. The default is 0.0.0.0 (all interfaces).
|
/// \param hostname (optional) The interface to bind to. The default is 0.0.0.0 (all interfaces).
|
||||||
/// \param nonblock (optional) Whether accept() calls will be nonblocking. Default is false (blocking).
|
/// \param nonblock (optional) Whether accept() calls will be nonblocking. Default is false (blocking).
|
||||||
DDV::ServerSocket::ServerSocket(int port, std::string hostname, bool nonblock){
|
Socket::Server::Server(int port, std::string hostname, bool nonblock){
|
||||||
sock = socket(AF_INET, SOCK_STREAM, 0);
|
sock = socket(AF_INET6, SOCK_STREAM, 0);
|
||||||
if (sock < 0){
|
if (sock < 0){
|
||||||
#if DEBUG >= 1
|
#if DEBUG >= 1
|
||||||
fprintf(stderr, "Could not create socket! Error: %s\n", strerror(errno));
|
fprintf(stderr, "Could not create socket! Error: %s\n", strerror(errno));
|
||||||
|
@ -247,10 +307,14 @@ DDV::ServerSocket::ServerSocket(int port, std::string hostname, bool nonblock){
|
||||||
flags |= O_NONBLOCK;
|
flags |= O_NONBLOCK;
|
||||||
fcntl(sock, F_SETFL, flags);
|
fcntl(sock, F_SETFL, flags);
|
||||||
}
|
}
|
||||||
struct sockaddr_in addr;
|
struct sockaddr_in6 addr;
|
||||||
addr.sin_family = AF_INET;
|
addr.sin6_family = AF_INET6;
|
||||||
addr.sin_port = htons(port);//set port
|
addr.sin6_port = htons(port);//set port
|
||||||
inet_pton(AF_INET, hostname.c_str(), &addr.sin_addr);//set interface, 0.0.0.0 (default) is all
|
if (hostname == "0.0.0.0"){
|
||||||
|
addr.sin6_addr = in6addr_any;
|
||||||
|
}else{
|
||||||
|
inet_pton(AF_INET6, hostname.c_str(), &addr.sin6_addr);//set interface, 0.0.0.0 (default) is all
|
||||||
|
}
|
||||||
int ret = bind(sock, (sockaddr*)&addr, sizeof(addr));//do the actual bind
|
int ret = bind(sock, (sockaddr*)&addr, sizeof(addr));//do the actual bind
|
||||||
if (ret == 0){
|
if (ret == 0){
|
||||||
ret = listen(sock, 100);//start listening, backlog of 100 allowed
|
ret = listen(sock, 100);//start listening, backlog of 100 allowed
|
||||||
|
@ -270,15 +334,15 @@ DDV::ServerSocket::ServerSocket(int port, std::string hostname, bool nonblock){
|
||||||
close();
|
close();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}//DDV::ServerSocket TCP Constructor
|
}//Socket::Server TCP Constructor
|
||||||
|
|
||||||
/// Create a new Unix ServerSocket. The socket is immediately bound and set to listen.
|
/// Create a new Unix Server. The socket is immediately bound and set to listen.
|
||||||
/// A maximum of 100 connections will be accepted between accept() calls.
|
/// A maximum of 100 connections will be accepted between accept() calls.
|
||||||
/// Any further connections coming in will be dropped.
|
/// Any further connections coming in will be dropped.
|
||||||
/// The address used will first be unlinked - so it succeeds if the Unix socket already existed. Watch out for this behaviour - it will delete any file located at address!
|
/// The address used will first be unlinked - so it succeeds if the Unix socket already existed. Watch out for this behaviour - it will delete any file located at address!
|
||||||
/// \param address The location of the Unix socket to bind to.
|
/// \param address The location of the Unix socket to bind to.
|
||||||
/// \param nonblock (optional) Whether accept() calls will be nonblocking. Default is false (blocking).
|
/// \param nonblock (optional) Whether accept() calls will be nonblocking. Default is false (blocking).
|
||||||
DDV::ServerSocket::ServerSocket(std::string address, bool nonblock){
|
Socket::Server::Server(std::string address, bool nonblock){
|
||||||
unlink(address.c_str());
|
unlink(address.c_str());
|
||||||
sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
if (sock < 0){
|
if (sock < 0){
|
||||||
|
@ -314,15 +378,18 @@ DDV::ServerSocket::ServerSocket(std::string address, bool nonblock){
|
||||||
close();
|
close();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}//DDV::ServerSocket Unix Constructor
|
}//Socket::Server Unix Constructor
|
||||||
|
|
||||||
/// Accept any waiting connections. If the DDV::ServerSocket is blocking, this function will block until there is an incoming connection.
|
/// Accept any waiting connections. If the Socket::Server is blocking, this function will block until there is an incoming connection.
|
||||||
/// If the DDV::ServerSocket is nonblocking, it might return a DDV::Socket that is not connected, so check for this.
|
/// If the Socket::Server is nonblocking, it might return a Socket::Connection that is not connected, so check for this.
|
||||||
/// \param nonblock (optional) Whether the newly connected socket should be nonblocking. Default is false (blocking).
|
/// \param nonblock (optional) Whether the newly connected socket should be nonblocking. Default is false (blocking).
|
||||||
/// \returns A DDV::Socket, which may or may not be connected, depending on settings and circumstances.
|
/// \returns A Socket::Connection, which may or may not be connected, depending on settings and circumstances.
|
||||||
DDV::Socket DDV::ServerSocket::accept(bool nonblock){
|
Socket::Connection Socket::Server::accept(bool nonblock){
|
||||||
if (sock < 0){return DDV::Socket(-1);}
|
if (sock < 0){return Socket::Connection(-1);}
|
||||||
int r = ::accept(sock, 0, 0);
|
struct sockaddr_in6 addrinfo;
|
||||||
|
socklen_t len = sizeof(addrinfo);
|
||||||
|
static char addrconv[INET6_ADDRSTRLEN];
|
||||||
|
int r = ::accept(sock, (sockaddr*)&addrinfo, &len);
|
||||||
//set the socket to be nonblocking, if requested.
|
//set the socket to be nonblocking, if requested.
|
||||||
//we could do this through accept4 with a flag, but that call is non-standard...
|
//we could do this through accept4 with a flag, but that call is non-standard...
|
||||||
if ((r >= 0) && nonblock){
|
if ((r >= 0) && nonblock){
|
||||||
|
@ -330,26 +397,48 @@ DDV::Socket DDV::ServerSocket::accept(bool nonblock){
|
||||||
flags |= O_NONBLOCK;
|
flags |= O_NONBLOCK;
|
||||||
fcntl(r, F_SETFL, flags);
|
fcntl(r, F_SETFL, flags);
|
||||||
}
|
}
|
||||||
|
Socket::Connection tmp(r);
|
||||||
if (r < 0){
|
if (r < 0){
|
||||||
if (errno != EWOULDBLOCK && errno != EAGAIN){close();}
|
if (errno != EWOULDBLOCK && errno != EAGAIN){close();}
|
||||||
|
}else{
|
||||||
|
if (addrinfo.sin6_family == AF_INET6){
|
||||||
|
tmp.remotehost = inet_ntop(AF_INET6, &(addrinfo.sin6_addr), addrconv, INET6_ADDRSTRLEN);
|
||||||
|
#if DEBUG >= 4
|
||||||
|
printf("IPv6 addr: %s\n", tmp.remotehost.c_str());
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
return DDV::Socket(r);
|
if (addrinfo.sin6_family == AF_INET){
|
||||||
|
tmp.remotehost = inet_ntop(AF_INET, &(((sockaddr_in*)&addrinfo)->sin_addr), addrconv, INET6_ADDRSTRLEN);
|
||||||
|
#if DEBUG >= 4
|
||||||
|
printf("IPv4 addr: %s\n", tmp.remotehost.c_str());
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
if (addrinfo.sin6_family == AF_UNIX){
|
||||||
|
#if DEBUG >= 4
|
||||||
|
tmp.remotehost = ((sockaddr_un*)&addrinfo)->sun_path;
|
||||||
|
printf("Unix addr: %s\n", tmp.remotehost.c_str());
|
||||||
|
#endif
|
||||||
|
tmp.remotehost = "UNIX_SOCKET";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Close connection. The internal socket is closed and then set to -1.
|
/// Close connection. The internal socket is closed and then set to -1.
|
||||||
void DDV::ServerSocket::close(){
|
void Socket::Server::close(){
|
||||||
|
shutdown(sock, SHUT_RDWR);
|
||||||
::close(sock);
|
::close(sock);
|
||||||
sock = -1;
|
sock = -1;
|
||||||
}//DDV::ServerSocket::close
|
}//Socket::Server::close
|
||||||
|
|
||||||
/// Returns the connected-state for this socket.
|
/// Returns the connected-state for this socket.
|
||||||
/// Note that this function might be slightly behind the real situation.
|
/// Note that this function might be slightly behind the real situation.
|
||||||
/// The connection status is updated after every accept attempt, when errors occur
|
/// The connection status is updated after every accept attempt, when errors occur
|
||||||
/// and when the socket is closed manually.
|
/// and when the socket is closed manually.
|
||||||
/// \returns True if socket is connected, false otherwise.
|
/// \returns True if socket is connected, false otherwise.
|
||||||
bool DDV::ServerSocket::connected(){
|
bool Socket::Server::connected(){
|
||||||
return (sock >= 0);
|
return (sock >= 0);
|
||||||
}//DDV::ServerSocket::connected
|
}//Socket::Server::connected
|
||||||
|
|
||||||
/// Returns internal socket number.
|
/// Returns internal socket number.
|
||||||
int DDV::ServerSocket::getSocket(){return sock;}
|
int Socket::Server::getSocket(){return sock;}
|
|
@ -1,5 +1,6 @@
|
||||||
/// \file ddv_socket.h
|
/// \file socket.h
|
||||||
/// Holds all headers for the DDV namespace.
|
/// A handy Socket wrapper library.
|
||||||
|
/// Written by Jaron Vietor in 2010 for DDVTech
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <string>
|
#include <string>
|
||||||
|
@ -14,17 +15,18 @@
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
|
||||||
|
|
||||||
///Holds DDV Socket tools.
|
///Holds Socket tools.
|
||||||
namespace DDV{
|
namespace Socket{
|
||||||
|
|
||||||
/// This class is for easy communicating through sockets, either TCP or Unix.
|
/// This class is for easy communicating through sockets, either TCP or Unix.
|
||||||
class Socket{
|
class Connection{
|
||||||
private:
|
private:
|
||||||
int sock; ///< Internally saved socket number.
|
int sock; ///< Internally saved socket number.
|
||||||
|
std::string remotehost; ///< Stores remote host address.
|
||||||
public:
|
public:
|
||||||
Socket(); ///< Create a new disconnected base socket.
|
Connection(); ///< Create a new disconnected base socket.
|
||||||
Socket(int sockNo); ///< Create a new base socket.
|
Connection(int sockNo); ///< Create a new base socket.
|
||||||
Socket(std::string adres, bool nonblock = false); ///< Create a new Unix Socket.
|
Connection(std::string adres, bool nonblock = false); ///< Create a new Unix Socket.
|
||||||
bool Error; ///< Set to true if a socket error happened.
|
bool Error; ///< Set to true if a socket error happened.
|
||||||
bool Blocking; ///< Set to true if a socket is currently or wants to be blocking.
|
bool Blocking; ///< Set to true if a socket is currently or wants to be blocking.
|
||||||
signed int ready(); ///< Returns the ready-state for this socket.
|
signed int ready(); ///< Returns the ready-state for this socket.
|
||||||
|
@ -37,19 +39,24 @@ namespace DDV{
|
||||||
int iwrite(void * buffer, int len); ///< Incremental write call.
|
int iwrite(void * buffer, int len); ///< Incremental write call.
|
||||||
int iread(void * buffer, int len); ///< Incremental read call.
|
int iread(void * buffer, int len); ///< Incremental read call.
|
||||||
bool read(std::string & buffer); ///< Read call that is compatible with std::string.
|
bool read(std::string & buffer); ///< Read call that is compatible with std::string.
|
||||||
|
bool swrite(std::string & buffer); ///< Read call that is compatible with std::string.
|
||||||
|
bool iread(std::string & buffer); ///< Incremental write call that is compatible with std::string.
|
||||||
|
bool iwrite(std::string & buffer); ///< Write call that is compatible with std::string.
|
||||||
void close(); ///< Close connection.
|
void close(); ///< Close connection.
|
||||||
|
std::string getHost(); ///< Gets hostname for connection, if available.
|
||||||
int getSocket(); ///< Returns internal socket number.
|
int getSocket(); ///< Returns internal socket number.
|
||||||
|
friend class Server;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// This class is for easily setting up listening socket, either TCP or Unix.
|
/// This class is for easily setting up listening socket, either TCP or Unix.
|
||||||
class ServerSocket{
|
class Server{
|
||||||
private:
|
private:
|
||||||
int sock; ///< Internally saved socket number.
|
int sock; ///< Internally saved socket number.
|
||||||
public:
|
public:
|
||||||
ServerSocket(); ///< Create a new base ServerSocket.
|
Server(); ///< Create a new base Server.
|
||||||
ServerSocket(int port, std::string hostname = "0.0.0.0", bool nonblock = false); ///< Create a new TCP ServerSocket.
|
Server(int port, std::string hostname = "0.0.0.0", bool nonblock = false); ///< Create a new TCP Server.
|
||||||
ServerSocket(std::string adres, bool nonblock = false); ///< Create a new Unix ServerSocket.
|
Server(std::string adres, bool nonblock = false); ///< Create a new Unix Server.
|
||||||
Socket accept(bool nonblock = false); ///< Accept any waiting connections.
|
Connection accept(bool nonblock = false); ///< Accept any waiting connections.
|
||||||
bool connected(); ///< Returns the connected-state for this socket.
|
bool connected(); ///< Returns the connected-state for this socket.
|
||||||
void close(); ///< Close connection.
|
void close(); ///< Close connection.
|
||||||
int getSocket(); ///< Returns internal socket number.
|
int getSocket(); ///< Returns internal socket number.
|
Loading…
Add table
Reference in a new issue