Commit 54e848b9 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

- Avoid identical malloc/memcpy/free for every packet sent,

- don't reinvent memcpy()
- and other clean ups
parent 8b1edfb8
...@@ -159,7 +159,7 @@ int Broadcast::Send(Message* m, const struct sockaddr *dst, socklen_t len) ...@@ -159,7 +159,7 @@ int Broadcast::Send(Message* m, const struct sockaddr *dst, socklen_t len)
m->CreateFinalMessage(); m->CreateFinalMessage();
/* Get the message and the length */ /* Get the message and the length */
char* message = m->GetFinalMessage(); uint8_t* message = m->GetFinalMessage();
unsigned int length = m->GetFinalMessageLen(); unsigned int length = m->GetFinalMessageLen();
if(message==NULL) if(message==NULL)
......
...@@ -40,81 +40,64 @@ using namespace std; ...@@ -40,81 +40,64 @@ using namespace std;
#include "program.h" #include "program.h"
#include "message.h" #include "message.h"
static const char mime_type[] = "application/sdp";
/***************************************************************************** /*****************************************************************************
* Constructors * Constructors
*****************************************************************************/ *****************************************************************************/
Message::Message(char t, uint16_t v, const char* ip) Message::Message(char type, uint16_t v, const char* ip)
{ {
version = v; version = v;
type = t;
ip_server=inet_addr(ip); //TODO automaticaly get it ?
msg=NULL;
sdp="";
sap=NULL;
return;
}
/*****************************************************************************
* Destructor
*****************************************************************************/
Message::~Message(void)
{
if(msg) free(msg);
if(sap) free(sap);
return;
}
u_long ip_server = inet_addr (ip); //TODO automaticaly get it ?
msg_len = 4
+ 4 /* (IPv4 specific) */
/*+ authentification length (N/A) */
+ sizeof (mime_type);
/***************************************************************************** msg = (uint8_t *)realloc (NULL, msg_len);
* BuildHeader - Build the Message Header once initialized if (msg == NULL)
* According to RFC 2974 (SAP) return; // TODO: throw an exception
*****************************************************************************/
void Message::BuildHeader(const char* payload_type)
{
unsigned int mh_size;
mh_size = 4 // Build the Message Header once initialized according to RFC 2974 (SAP)
+ 4 /*(IPv4 specific)*/
/* + authentification length (N/A)*/
+ strlen(payload_type) + 1;
sap=(char*)malloc(mh_size); /* Byte 0 : Version number V1 = 001 (3 bits)
/* Byte 1 : Version number V1 = 001 (3 bits)
* Address type IPv4/IPv6 = 0/1 (1 bit) * Address type IPv4/IPv6 = 0/1 (1 bit)
* Reserved 0 (1 bit) * Reserved 0 (1 bit)
* Message Type ann/del = 0/1 (1 bit) * Message Type ann/del = 0/1 (1 bit)
* Encryption on/off = 0/1 (1 bit) * Encryption on/off = 0/1 (1 bit)
* Compressed on/off = 0/1 (1 bit) */ * Compressed on/off = 0/1 (1 bit) */
sap[0]=0x20; msg[0] = 0x20;
//if ( ip_version == SAP_IPV6 ) sap[0]|=0x10; //if ( ip_version == SAP_IPV6 ) msg[0] |= 0x10;
if ( type == SAP_DELETE ) sap[0]|=0x04; //if ( type == SAP_DELETE ) msg[0] |= 0x04;
//if ( encrypted ) sap[0]|=0x02; //if ( encrypted ) msg[0] |= 0x02;
//if ( compressed ) sap[0]|=0x01; //if ( compressed ) msg[0] |= 0x01;
/* Byte 2 : Authentification length - Not supported */ /* Byte 1 : Authentification length - Not supported */
sap[1]=0x00; msg[1] = 0x00;
/* Byte 3 & Byte 4 : Message Id Hash */ /* Bytes 2-3 : Message Id Hash */
sap[2]=version; msg[2] = version & 0xff;
sap[3]=version>>8; msg[3] = version >> 8;
/* next 4 (or 16) byte: Originating source */ /* Bytes 4-7 (or 4-19) byte: Originating source */
sap[4]=ip_server; memcpy (msg + 4, &ip_server, 4);
sap[5]=ip_server>>8;
sap[6]=ip_server>>16; /* finally MIME type */
sap[7]=ip_server>>24; memcpy (msg + 8, mime_type, sizeof (mime_type));
}
strncpy(sap+8, payload_type, 15);
sap[8+strlen(payload_type)]='\0'; /*****************************************************************************
* Destructor
header_len=9+strlen(payload_type); *****************************************************************************/
return; Message::~Message(void)
{
if (msg != NULL)
free (msg);
} }
void Message::AddProgram(Program *p)
bool Message::AddProgram(Program *p)
{ {
/* FIXME */ /* FIXME */
/* RFC 2327 Compliance ? */ /* RFC 2327 Compliance ? */
...@@ -130,16 +113,10 @@ void Message::AddProgram(Program *p) ...@@ -130,16 +113,10 @@ void Message::AddProgram(Program *p)
string u="u="+p->GetSite()+"\n"; string u="u="+p->GetSite()+"\n";
string t; string t;
if(p->IsPermanent()) if(p->IsPermanent())
{
t="t=0 0\n"; t="t=0 0\n";
}
else else
{ return false;
/* TODO */
t="t=0 0\n";
fprintf(stderr, "Non permanent program not supported ...\n");
fprintf(stderr, "Announcing permanent session instead\n");
}
string m = "m=video " + p->GetPort() + " " string m = "m=video " + p->GetPort() + " "
+ (p->IsRTP() ? "RTP/AVP" : "udp") +" 33\n"; + (p->IsRTP() ? "RTP/AVP" : "udp") +" 33\n";
...@@ -159,46 +136,31 @@ void Message::AddProgram(Program *p) ...@@ -159,46 +136,31 @@ void Message::AddProgram(Program *p)
a+= "a=x-plgroup:"+p->GetPlGroup()+"\n"; a+= "a=x-plgroup:"+p->GetPlGroup()+"\n";
} }
sdp += v + o + s + u + t + m + c + a; string sdp = v + o + s + u + t + m + c + a;
printf("%s\n",sdp.c_str()); puts (sdp.c_str ());
return; if (msg_len + sdp.length () > 1024)
} //RFC 2974 Chap 6.
return false;
void Message::CreateFinalMessage(void) // updates full message
{ uint8_t *newmsg = (uint8_t *)realloc (msg, msg_len + sdp.length ());
/* Deletes previous message */ if (newmsg == NULL)
if(msg != NULL) return false;
free(msg);
/* Check the length */ // TODO: don't identically overwrite earlier programs
msg_len = sdp.size() + header_len; memcpy (newmsg + msg_len, sdp.c_str(), sdp.length ());
if(msg_len>1024) msg = newmsg;
{ msg_len += sdp.length ();
msg_len=0;
msg=NULL; //RFC 2974 Chap 6.
return;
}
msg=(char*)malloc(msg_len*sizeof(char));
unsigned int i;
for(i=0; i<header_len; i++)
{
msg[i]=sap[i];
}
for(; i<msg_len; i++)
{
msg[i]=sdp[i-header_len];
}
return true;
} }
void Message::CreateFinalMessage(void)
{
}
uint8_t* Message::GetFinalMessage(){return msg;}
char* Message::GetFinalMessage(){return msg;}
unsigned int Message::GetFinalMessageLen(){return msg_len;} unsigned int Message::GetFinalMessageLen(){return msg_len;}
...@@ -32,20 +32,13 @@ class Message { ...@@ -32,20 +32,13 @@ class Message {
public: public:
Message(char message_type, uint16_t version, const char* ip); Message(char message_type, uint16_t version, const char* ip);
~Message(); ~Message();
void BuildHeader(const char* payload_type); bool AddProgram(Program*);
void AddProgram(Program*);
void CreateFinalMessage(); void CreateFinalMessage();
char* GetFinalMessage(); uint8_t* GetFinalMessage();
unsigned int GetFinalMessageLen(); unsigned int GetFinalMessageLen();
private: private:
uint16_t version; // Version of this annoucement uint8_t* msg; // final message
uint8_t type; // Announce or Deletion size_t msg_len;
uint32_t ip_server; // IP of the server (IPV4 specific) uint16_t version;
char* sap; // header of the message
string sdp; // SDP message
char* msg; // final message
unsigned int header_len; //len of the SAP HEADER
unsigned int msg_len; //len of the complete packet
}; };
...@@ -223,8 +223,6 @@ int main(int argc, char *argv[]) ...@@ -223,8 +223,6 @@ int main(int argc, char *argv[])
/* Create a new message */ /* Create a new message */
Message *message = new Message(SAP_ANNOUNCE, 0x4212+i, "1.2.3.4"); Message *message = new Message(SAP_ANNOUNCE, 0x4212+i, "1.2.3.4");
/* Build the message header */
message->BuildHeader("application/sdp");
/* Add the program */ /* Add the program */
message->AddProgram(config.Programs[i]); message->AddProgram(config.Programs[i]);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment