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