diff options
Diffstat (limited to 'tinyMSRP/src/session')
-rwxr-xr-x | tinyMSRP/src/session/tmsrp_config.c | 45 | ||||
-rwxr-xr-x | tinyMSRP/src/session/tmsrp_data.c | 324 | ||||
-rwxr-xr-x | tinyMSRP/src/session/tmsrp_receiver.c | 253 | ||||
-rwxr-xr-x | tinyMSRP/src/session/tmsrp_sender.c | 391 |
4 files changed, 504 insertions, 509 deletions
diff --git a/tinyMSRP/src/session/tmsrp_config.c b/tinyMSRP/src/session/tmsrp_config.c index 4394bf0..3810097 100755 --- a/tinyMSRP/src/session/tmsrp_config.c +++ b/tinyMSRP/src/session/tmsrp_config.c @@ -2,19 +2,19 @@ * Copyright (C) 2009 Mamadou Diop. * * Contact: Mamadou Diop <diopmamadou(at)doubango.org> -* +* * This file is part of Open Source Doubango Framework. * * DOUBANGO is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. -* +* * DOUBANGO is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. -* +* * You should have received a copy of the GNU General Public License * along with DOUBANGO. * @@ -34,7 +34,7 @@ TINYMSRP_API tmsrp_config_t* tmsrp_config_create() { - return tsk_object_new(tmsrp_config_def_t); + return tsk_object_new(tmsrp_config_def_t); } @@ -53,30 +53,29 @@ TINYMSRP_API tmsrp_config_t* tmsrp_config_create() // static void* tmsrp_config_ctor(tsk_object_t * self, va_list * app) { - tmsrp_config_t *config = self; - if(config){ - config->Failure_Report = tsk_true; - } - return self; + tmsrp_config_t *config = self; + if(config) { + config->Failure_Report = tsk_true; + } + return self; } static void* tmsrp_config_dtor(tsk_object_t * self) -{ - tmsrp_config_t *config = self; - if(config){ - TSK_OBJECT_SAFE_FREE(config->From_Path); - TSK_OBJECT_SAFE_FREE(config->To_Path); - } - - return self; +{ + tmsrp_config_t *config = self; + if(config) { + TSK_OBJECT_SAFE_FREE(config->From_Path); + TSK_OBJECT_SAFE_FREE(config->To_Path); + } + + return self; } -static const tsk_object_def_t tmsrp_config_def_s = -{ - sizeof(tmsrp_config_t), - tmsrp_config_ctor, - tmsrp_config_dtor, - tsk_null, +static const tsk_object_def_t tmsrp_config_def_s = { + sizeof(tmsrp_config_t), + tmsrp_config_ctor, + tmsrp_config_dtor, + tsk_null, }; const tsk_object_def_t *tmsrp_config_def_t = &tmsrp_config_def_s; diff --git a/tinyMSRP/src/session/tmsrp_data.c b/tinyMSRP/src/session/tmsrp_data.c index e8c750e..d872da1 100755 --- a/tinyMSRP/src/session/tmsrp_data.c +++ b/tinyMSRP/src/session/tmsrp_data.c @@ -2,19 +2,19 @@ * Copyright (C) 2009 Mamadou Diop. * * Contact: Mamadou Diop <diopmamadou(at)doubango.org> -* +* * This file is part of Open Source Doubango Framework. * * DOUBANGO is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. -* +* * DOUBANGO is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. -* +* * You should have received a copy of the GNU General Public License * along with DOUBANGO. * @@ -43,38 +43,38 @@ tmsrp_data_in_t* tmsrp_data_in_create() { - return tsk_object_new(tmsrp_data_in_def_t); + return tsk_object_new(tmsrp_data_in_def_t); } tmsrp_data_out_t* _tmsrp_data_out_create(const void* pdata, tsk_size_t size, tsk_bool_t isfilepath) { - return tsk_object_new(tmsrp_data_out_def_t, pdata, size, isfilepath); + return tsk_object_new(tmsrp_data_out_def_t, pdata, size, isfilepath); } tmsrp_data_out_t* tmsrp_data_out_create(const void* pdata, tsk_size_t size) -{ - return _tmsrp_data_out_create(pdata, size, tsk_false); +{ + return _tmsrp_data_out_create(pdata, size, tsk_false); } tmsrp_data_out_t* tmsrp_data_out_file_create(const char* filepath) { - return _tmsrp_data_out_create(filepath, tsk_strlen(filepath), tsk_true); + return _tmsrp_data_out_create(filepath, tsk_strlen(filepath), tsk_true); } /* =========================== Common ============================= */ static int tmsrp_data_deinit(tmsrp_data_t* self) { - if(!self){ - TSK_DEBUG_ERROR("Invalid parameter"); - return -1; - } + if(!self) { + TSK_DEBUG_ERROR("Invalid parameter"); + return -1; + } - TSK_FREE(self->id); - TSK_FREE(self->ctype); - TSK_FREE(self->wctype); + TSK_FREE(self->id); + TSK_FREE(self->ctype); + TSK_FREE(self->wctype); - return 0; + return 0; } @@ -87,45 +87,45 @@ static int tmsrp_data_deinit(tmsrp_data_t* self) int tmsrp_data_in_put(tmsrp_data_in_t* self, const void* pdata, tsk_size_t size) { - int ret = -1; - - if(!self || !self->buffer || !pdata || !size){ - TSK_DEBUG_ERROR("Invalid parameter"); - return ret; - } - - if((ret = tsk_buffer_append(self->buffer, pdata, size))){ - TSK_DEBUG_ERROR("Failed to append data"); - tsk_buffer_cleanup(self->buffer); - return ret; - } - else{ - if(TSK_BUFFER_SIZE(self->buffer) > TMSRP_DATA_IN_MAX_BUFFER){ - tsk_buffer_cleanup(self->buffer); - TSK_DEBUG_ERROR("Too many bytes are waiting."); - return -3; - } - } - - return ret; + int ret = -1; + + if(!self || !self->buffer || !pdata || !size) { + TSK_DEBUG_ERROR("Invalid parameter"); + return ret; + } + + if((ret = tsk_buffer_append(self->buffer, pdata, size))) { + TSK_DEBUG_ERROR("Failed to append data"); + tsk_buffer_cleanup(self->buffer); + return ret; + } + else { + if(TSK_BUFFER_SIZE(self->buffer) > TMSRP_DATA_IN_MAX_BUFFER) { + tsk_buffer_cleanup(self->buffer); + TSK_DEBUG_ERROR("Too many bytes are waiting."); + return -3; + } + } + + return ret; } tmsrp_message_t* tmsrp_data_in_get(tmsrp_data_in_t* self) { - tmsrp_message_t* ret; - tsk_size_t msg_size; + tmsrp_message_t* ret; + tsk_size_t msg_size; - if(!self || !self->buffer || !TSK_BUFFER_DATA(self->buffer) || !TSK_BUFFER_SIZE(self->buffer)){ - //...this is not an error - return tsk_null; - } + if(!self || !self->buffer || !TSK_BUFFER_DATA(self->buffer) || !TSK_BUFFER_SIZE(self->buffer)) { + //...this is not an error + return tsk_null; + } - if((ret = tmsrp_message_parse_2(self->buffer->data, self->buffer->size, &msg_size))){ - tsk_buffer_remove(self->buffer, 0, msg_size); - return ret; - } + if((ret = tmsrp_message_parse_2(self->buffer->data, self->buffer->size, &msg_size))) { + tsk_buffer_remove(self->buffer, 0, msg_size); + return ret; + } - return tsk_null; + return tsk_null; } @@ -133,38 +133,38 @@ tmsrp_message_t* tmsrp_data_in_get(tmsrp_data_in_t* self) tsk_buffer_t* tmsrp_data_out_get(tmsrp_data_out_t* self) { - tsk_buffer_t* ret = tsk_null; - tsk_size_t toread; - - if(!self){ - return tsk_null; - } - - if(!(toread = self->size > TMSRP_MAX_CHUNK_SIZE ? TMSRP_MAX_CHUNK_SIZE : self->size)){ - return tsk_null; - } - - if(self->message){ - ret = tsk_buffer_create(TSK_BUFFER_DATA(self->message), toread); - tsk_buffer_remove(self->message, 0, toread); - self->size = self->message->size; - } - else if(self->file){ - // Buffer hack - tsk_size_t read; - ret = tsk_buffer_create_null(); - ret->data = tsk_calloc(toread, sizeof(uint8_t)); - ret->size = toread; - if((read = (tsk_size_t)fread(ret->data, sizeof(uint8_t), toread, self->file)) == toread){ - self->size -= toread; - } - else{ - TSK_OBJECT_SAFE_FREE(ret); - } - } - - - return ret; + tsk_buffer_t* ret = tsk_null; + tsk_size_t toread; + + if(!self) { + return tsk_null; + } + + if(!(toread = self->size > TMSRP_MAX_CHUNK_SIZE ? TMSRP_MAX_CHUNK_SIZE : self->size)) { + return tsk_null; + } + + if(self->message) { + ret = tsk_buffer_create(TSK_BUFFER_DATA(self->message), toread); + tsk_buffer_remove(self->message, 0, toread); + self->size = self->message->size; + } + else if(self->file) { + // Buffer hack + tsk_size_t read; + ret = tsk_buffer_create_null(); + ret->data = tsk_calloc(toread, sizeof(uint8_t)); + ret->size = toread; + if((read = (tsk_size_t)fread(ret->data, sizeof(uint8_t), toread, self->file)) == toread) { + self->size -= toread; + } + else { + TSK_OBJECT_SAFE_FREE(ret); + } + } + + + return ret; } @@ -179,30 +179,29 @@ tsk_buffer_t* tmsrp_data_out_get(tmsrp_data_out_t* self) // static void* tmsrp_data_in_ctor(tsk_object_t * self, va_list * app) { - tmsrp_data_in_t *data_in = self; - if(data_in){ - data_in->buffer = tsk_buffer_create_null(); - } - return self; + tmsrp_data_in_t *data_in = self; + if(data_in) { + data_in->buffer = tsk_buffer_create_null(); + } + return self; } static void* tmsrp_data_in_dtor(tsk_object_t * self) -{ - tmsrp_data_in_t *data_in = self; - if(data_in){ - tmsrp_data_deinit(TMSRP_DATA(data_in)); - TSK_OBJECT_SAFE_FREE(data_in->buffer); - } - - return self; +{ + tmsrp_data_in_t *data_in = self; + if(data_in) { + tmsrp_data_deinit(TMSRP_DATA(data_in)); + TSK_OBJECT_SAFE_FREE(data_in->buffer); + } + + return self; } -static const tsk_object_def_t tmsrp_data_in_def_s = -{ - sizeof(tmsrp_data_in_t), - tmsrp_data_in_ctor, - tmsrp_data_in_dtor, - tsk_null, +static const tsk_object_def_t tmsrp_data_in_def_s = { + sizeof(tmsrp_data_in_t), + tmsrp_data_in_ctor, + tmsrp_data_in_dtor, + tsk_null, }; const tsk_object_def_t *tmsrp_data_in_def_t = &tmsrp_data_in_def_s; @@ -211,74 +210,73 @@ const tsk_object_def_t *tmsrp_data_in_def_t = &tmsrp_data_in_def_s; // static void* tmsrp_data_out_ctor(tsk_object_t * self, va_list * app) { - tmsrp_data_out_t *data_out = self; - if(data_out){ - tsk_istr_t id; - const void* pdata = va_arg(*app, const void*); - tsk_size_t size = va_arg(*app, tsk_size_t); - tsk_bool_t isfilepath = va_arg(*app, tsk_bool_t); - - if(isfilepath){ - if((data_out->file = fopen((const char*)pdata, "rb"))){ - int ret; - if((ret = fseek(data_out->file, 0L, SEEK_END))){ - TSK_DEBUG_ERROR("fseek for file:[%s] failed with error code %d.", (const char*)pdata, ret); - TMSRP_DATA(data_out)->isOK = tsk_false; - } - else{ - data_out->size = ftell(data_out->file); - if((ret = fseek(data_out->file, 0L, SEEK_SET))){ - TSK_DEBUG_ERROR("fseek for file:[%s] failed with error code %d.", (const char*)pdata, ret); - TMSRP_DATA(data_out)->isOK = tsk_false; - } - else{ - TMSRP_DATA(data_out)->isOK = tsk_true; - } - } - } - else{ - TSK_DEBUG_ERROR("Failed to open(rb) this file:[%s]", (const char*)pdata); - TMSRP_DATA(data_out)->isOK = tsk_false; - } - } - else{ - if((data_out->message = tsk_buffer_create(pdata, size))){ - TMSRP_DATA(data_out)->isOK = (data_out->message->size == size); - data_out->size = data_out->message->size; - } - } - - // content type - TMSRP_DATA(data_out)->ctype = tsk_strdup("application/octet-stream"); - TMSRP_DATA(data_out)->wctype = tsk_strdup("text/plain"); - // random id - tsk_strrandom(&id); - TMSRP_DATA(data_out)->id = tsk_strdup(id); - } - return self; + tmsrp_data_out_t *data_out = self; + if(data_out) { + tsk_istr_t id; + const void* pdata = va_arg(*app, const void*); + tsk_size_t size = va_arg(*app, tsk_size_t); + tsk_bool_t isfilepath = va_arg(*app, tsk_bool_t); + + if(isfilepath) { + if((data_out->file = fopen((const char*)pdata, "rb"))) { + int ret; + if((ret = fseek(data_out->file, 0L, SEEK_END))) { + TSK_DEBUG_ERROR("fseek for file:[%s] failed with error code %d.", (const char*)pdata, ret); + TMSRP_DATA(data_out)->isOK = tsk_false; + } + else { + data_out->size = ftell(data_out->file); + if((ret = fseek(data_out->file, 0L, SEEK_SET))) { + TSK_DEBUG_ERROR("fseek for file:[%s] failed with error code %d.", (const char*)pdata, ret); + TMSRP_DATA(data_out)->isOK = tsk_false; + } + else { + TMSRP_DATA(data_out)->isOK = tsk_true; + } + } + } + else { + TSK_DEBUG_ERROR("Failed to open(rb) this file:[%s]", (const char*)pdata); + TMSRP_DATA(data_out)->isOK = tsk_false; + } + } + else { + if((data_out->message = tsk_buffer_create(pdata, size))) { + TMSRP_DATA(data_out)->isOK = (data_out->message->size == size); + data_out->size = data_out->message->size; + } + } + + // content type + TMSRP_DATA(data_out)->ctype = tsk_strdup("application/octet-stream"); + TMSRP_DATA(data_out)->wctype = tsk_strdup("text/plain"); + // random id + tsk_strrandom(&id); + TMSRP_DATA(data_out)->id = tsk_strdup(id); + } + return self; } static void* tmsrp_data_out_dtor(tsk_object_t * self) -{ - tmsrp_data_out_t *data_out = self; - if(data_out){ - tmsrp_data_deinit(TMSRP_DATA(data_out)); - TSK_OBJECT_SAFE_FREE(data_out->message); - - if(data_out->file){ - fclose(data_out->file); - data_out->file = tsk_null; - } - } - - return self; +{ + tmsrp_data_out_t *data_out = self; + if(data_out) { + tmsrp_data_deinit(TMSRP_DATA(data_out)); + TSK_OBJECT_SAFE_FREE(data_out->message); + + if(data_out->file) { + fclose(data_out->file); + data_out->file = tsk_null; + } + } + + return self; } -static const tsk_object_def_t tmsrp_data_out_def_s = -{ - sizeof(tmsrp_data_out_t), - tmsrp_data_out_ctor, - tmsrp_data_out_dtor, - tsk_null, +static const tsk_object_def_t tmsrp_data_out_def_s = { + sizeof(tmsrp_data_out_t), + tmsrp_data_out_ctor, + tmsrp_data_out_dtor, + tsk_null, }; const tsk_object_def_t *tmsrp_data_out_def_t = &tmsrp_data_out_def_s; diff --git a/tinyMSRP/src/session/tmsrp_receiver.c b/tinyMSRP/src/session/tmsrp_receiver.c index 3ec1333..3a15780 100755 --- a/tinyMSRP/src/session/tmsrp_receiver.c +++ b/tinyMSRP/src/session/tmsrp_receiver.c @@ -2,19 +2,19 @@ * Copyright (C) 2009 Mamadou Diop. * * Contact: Mamadou Diop <diopmamadou(at)doubango.org> -* +* * This file is part of Open Source Doubango Framework. * * DOUBANGO is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. -* +* * DOUBANGO is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. -* +* * You should have received a copy of the GNU General Public License * along with DOUBANGO. * @@ -39,125 +39,125 @@ static void _tmsrp_receiver_alert_user(tmsrp_receiver_t* self, tsk_bool_t outgoing, tmsrp_message_t* message) { - if(self->callback.func){ - tmsrp_event_t* _event = tmsrp_event_create(self->callback.data, outgoing, tmsrp_event_type_message, message); - self->callback.func(_event); - TSK_OBJECT_SAFE_FREE(_event); - } + if(self->callback.func) { + tmsrp_event_t* _event = tmsrp_event_create(self->callback.data, outgoing, tmsrp_event_type_message, message); + self->callback.func(_event); + TSK_OBJECT_SAFE_FREE(_event); + } } tmsrp_receiver_t* tmsrp_receiver_create(tmsrp_config_t* config, tnet_fd_t fd) { - return tsk_object_new(tmsrp_receiver_def_t, config, fd); + return tsk_object_new(tmsrp_receiver_def_t, config, fd); } int tmsrp_receiver_set_fd(tmsrp_receiver_t* self, tnet_fd_t fd) { - if(!self){ - TSK_DEBUG_ERROR("Invalid parameter"); - return -1; - } - self->fd = fd; - return 0; + if(!self) { + TSK_DEBUG_ERROR("Invalid parameter"); + return -1; + } + self->fd = fd; + return 0; } int tmsrp_receiver_start(tmsrp_receiver_t* self, const void* callback_data, tmsrp_event_cb_f func) { - if(!self){ - TSK_DEBUG_ERROR("Invalid parameter"); - return -1; - } + if(!self) { + TSK_DEBUG_ERROR("Invalid parameter"); + return -1; + } - self->callback.data = callback_data; - self->callback.func = func; + self->callback.data = callback_data; + self->callback.func = func; - return 0; + return 0; } int tmsrp_receiver_stop(tmsrp_receiver_t* self) { - return 0; + return 0; } int tmsrp_receiver_recv(tmsrp_receiver_t* self, const void* data, tsk_size_t size) { - tmsrp_message_t* message; - - if(!self || !data || !size){ - TSK_DEBUG_ERROR("Invalid parameter"); - return -1; - } - - // put the data - tmsrp_data_in_put(self->data_in, data, size); - // get msrp messages - while((message = tmsrp_data_in_get(self->data_in))){ - - /* alert that we have received a message (Request or Response) */ - _tmsrp_receiver_alert_user(self, tsk_false, message); - - // - // REQUEST - // - if(TMSRP_MESSAGE_IS_REQUEST(message)){ - /* ============= SEND =============== */ - if(TMSRP_REQUEST_IS_SEND(message)){ - tmsrp_response_t* r2xx; - tmsrp_request_t* REPORT; - - // send 200 OK - if((r2xx = tmsrp_create_response(message, 200, "OK"))){ - if(tmsrp_message_serialize(r2xx, self->buffer) == 0 && self->buffer->data){ - tnet_sockfd_send(self->fd, self->buffer->data, self->buffer->size, 0); - } - - tsk_buffer_cleanup(self->buffer); - TSK_OBJECT_SAFE_FREE(r2xx); - } - // send REPORT - if(tmsrp_isReportRequired(message, tsk_false)){ - if((REPORT = tmsrp_create_report(message, 200, "OK"))){ - if(tmsrp_message_serialize(REPORT, self->buffer) == 0 && self->buffer->data){ - tnet_sockfd_send(self->fd, self->buffer->data, self->buffer->size, 0); - } - tsk_buffer_cleanup(self->buffer); - TSK_OBJECT_SAFE_FREE(REPORT); - } - } - } - /* ============= REPORT =============== */ - if(TMSRP_REQUEST_IS_REPORT(message)){ - tmsrp_response_t* r2xx; - - // send 200 OK - if((r2xx = tmsrp_create_response(message, 200, "Report received"))){ - if(tmsrp_message_serialize(r2xx, self->buffer) == 0 && self->buffer->data){ - tnet_sockfd_send(self->fd, self->buffer->data, self->buffer->size, 0); - } - - tsk_buffer_cleanup(self->buffer); - TSK_OBJECT_SAFE_FREE(r2xx); - } - } - - /* ============= AUTH =============== */ - /* ============= METHOD =============== */ - } - // - // RESPONSE - // - else{ - //short code = TMSRP_RESPONSE_CODE(message); - //TSK_DEBUG_INFO("code=%u, tid=%s, phrase=%s", code, message->tid, TMSRP_RESPONSE_PHRASE(message)); - } - - - // alert user layer - - TSK_OBJECT_SAFE_FREE(message); - } - - return 0; + tmsrp_message_t* message; + + if(!self || !data || !size) { + TSK_DEBUG_ERROR("Invalid parameter"); + return -1; + } + + // put the data + tmsrp_data_in_put(self->data_in, data, size); + // get msrp messages + while((message = tmsrp_data_in_get(self->data_in))) { + + /* alert that we have received a message (Request or Response) */ + _tmsrp_receiver_alert_user(self, tsk_false, message); + + // + // REQUEST + // + if(TMSRP_MESSAGE_IS_REQUEST(message)) { + /* ============= SEND =============== */ + if(TMSRP_REQUEST_IS_SEND(message)) { + tmsrp_response_t* r2xx; + tmsrp_request_t* REPORT; + + // send 200 OK + if((r2xx = tmsrp_create_response(message, 200, "OK"))) { + if(tmsrp_message_serialize(r2xx, self->buffer) == 0 && self->buffer->data) { + tnet_sockfd_send(self->fd, self->buffer->data, self->buffer->size, 0); + } + + tsk_buffer_cleanup(self->buffer); + TSK_OBJECT_SAFE_FREE(r2xx); + } + // send REPORT + if(tmsrp_isReportRequired(message, tsk_false)) { + if((REPORT = tmsrp_create_report(message, 200, "OK"))) { + if(tmsrp_message_serialize(REPORT, self->buffer) == 0 && self->buffer->data) { + tnet_sockfd_send(self->fd, self->buffer->data, self->buffer->size, 0); + } + tsk_buffer_cleanup(self->buffer); + TSK_OBJECT_SAFE_FREE(REPORT); + } + } + } + /* ============= REPORT =============== */ + if(TMSRP_REQUEST_IS_REPORT(message)) { + tmsrp_response_t* r2xx; + + // send 200 OK + if((r2xx = tmsrp_create_response(message, 200, "Report received"))) { + if(tmsrp_message_serialize(r2xx, self->buffer) == 0 && self->buffer->data) { + tnet_sockfd_send(self->fd, self->buffer->data, self->buffer->size, 0); + } + + tsk_buffer_cleanup(self->buffer); + TSK_OBJECT_SAFE_FREE(r2xx); + } + } + + /* ============= AUTH =============== */ + /* ============= METHOD =============== */ + } + // + // RESPONSE + // + else { + //short code = TMSRP_RESPONSE_CODE(message); + //TSK_DEBUG_INFO("code=%u, tid=%s, phrase=%s", code, message->tid, TMSRP_RESPONSE_PHRASE(message)); + } + + + // alert user layer + + TSK_OBJECT_SAFE_FREE(message); + } + + return 0; } @@ -166,38 +166,37 @@ int tmsrp_receiver_recv(tmsrp_receiver_t* self, const void* data, tsk_size_t siz // static void* tmsrp_receiver_ctor(tsk_object_t * self, va_list *app) { - tmsrp_receiver_t *receiver = self; - if(receiver){ - receiver->config = tsk_object_ref(va_arg(*app, tmsrp_config_t*)); - receiver->fd = va_arg(*app, tnet_fd_t); - - receiver->data_in = tmsrp_data_in_create(); - receiver->buffer = tsk_buffer_create_null(); - } - return self; + tmsrp_receiver_t *receiver = self; + if(receiver) { + receiver->config = tsk_object_ref(va_arg(*app, tmsrp_config_t*)); + receiver->fd = va_arg(*app, tnet_fd_t); + + receiver->data_in = tmsrp_data_in_create(); + receiver->buffer = tsk_buffer_create_null(); + } + return self; } static void* tmsrp_receiver_dtor(tsk_object_t * self) -{ - tmsrp_receiver_t *receiver = self; - if(receiver){ - /* Stop */ - tmsrp_receiver_stop(receiver); - - TSK_OBJECT_SAFE_FREE(receiver->config); - TSK_OBJECT_SAFE_FREE(receiver->data_in); - TSK_OBJECT_SAFE_FREE(receiver->buffer); - // the FD is owned by the transport ...do not close it - } - return self; +{ + tmsrp_receiver_t *receiver = self; + if(receiver) { + /* Stop */ + tmsrp_receiver_stop(receiver); + + TSK_OBJECT_SAFE_FREE(receiver->config); + TSK_OBJECT_SAFE_FREE(receiver->data_in); + TSK_OBJECT_SAFE_FREE(receiver->buffer); + // the FD is owned by the transport ...do not close it + } + return self; } -static const tsk_object_def_t tmsrp_receiver_def_s = -{ - sizeof(tmsrp_receiver_t), - tmsrp_receiver_ctor, - tmsrp_receiver_dtor, - tsk_null, +static const tsk_object_def_t tmsrp_receiver_def_s = { + sizeof(tmsrp_receiver_t), + tmsrp_receiver_ctor, + tmsrp_receiver_dtor, + tsk_null, }; const tsk_object_def_t *tmsrp_receiver_def_t = &tmsrp_receiver_def_s; diff --git a/tinyMSRP/src/session/tmsrp_sender.c b/tinyMSRP/src/session/tmsrp_sender.c index 5fdf86f..1d6637c 100755 --- a/tinyMSRP/src/session/tmsrp_sender.c +++ b/tinyMSRP/src/session/tmsrp_sender.c @@ -2,19 +2,19 @@ * Copyright (C) 2009 Mamadou Diop. * * Contact: Mamadou Diop <diopmamadou(at)doubango.org> -* +* * This file is part of Open Source Doubango Framework. * * DOUBANGO is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. -* +* * DOUBANGO is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. -* +* * You should have received a copy of the GNU General Public License * along with DOUBANGO. * @@ -43,96 +43,96 @@ static void* TSK_STDCALL run(void* self); tmsrp_sender_t* tmsrp_sender_create(tmsrp_config_t* config, tnet_fd_t fd) { - return (tmsrp_sender_t*)tsk_object_new(tmsrp_sender_def_t, config, fd); + return (tmsrp_sender_t*)tsk_object_new(tmsrp_sender_def_t, config, fd); } int tmsrp_sender_set_fd(tmsrp_sender_t* self, tnet_fd_t fd) { - if(!self){ - TSK_DEBUG_ERROR("Invalid parameter"); - return -1; - } - self->fd = fd; - return 0; + if(!self) { + TSK_DEBUG_ERROR("Invalid parameter"); + return -1; + } + self->fd = fd; + return 0; } int tmsrp_sender_start(tmsrp_sender_t* self) { - int ret = -1; - - if(!self){ - TSK_DEBUG_ERROR("Invalid parameter"); - goto bail; - } - - TSK_RUNNABLE(self)->run = run; - if((ret = tsk_runnable_start(TSK_RUNNABLE(self), tmsrp_data_out_def_t))){ - goto bail; - } - + int ret = -1; + + if(!self) { + TSK_DEBUG_ERROR("Invalid parameter"); + goto bail; + } + + TSK_RUNNABLE(self)->run = run; + if((ret = tsk_runnable_start(TSK_RUNNABLE(self), tmsrp_data_out_def_t))) { + goto bail; + } + bail: - return ret; + return ret; } int tsmrp_sender_send_data(tmsrp_sender_t* self, const void* pdata, tsk_size_t size, const char* ctype, const char* wctype) { - tmsrp_data_out_t* data_out; - - if(!self){ - TSK_DEBUG_ERROR("Invalid parameter"); - return -1; - } - - if((data_out = tmsrp_data_out_create(pdata, size))){ - if(ctype){ - tsk_strupdate(&TMSRP_DATA(data_out)->ctype, ctype); - } - if(wctype){ - tsk_strupdate(&TMSRP_DATA(data_out)->wctype, wctype); - } - TSK_RUNNABLE_ENQUEUE_OBJECT(self, data_out); - return 0; - } - return -2; + tmsrp_data_out_t* data_out; + + if(!self) { + TSK_DEBUG_ERROR("Invalid parameter"); + return -1; + } + + if((data_out = tmsrp_data_out_create(pdata, size))) { + if(ctype) { + tsk_strupdate(&TMSRP_DATA(data_out)->ctype, ctype); + } + if(wctype) { + tsk_strupdate(&TMSRP_DATA(data_out)->wctype, wctype); + } + TSK_RUNNABLE_ENQUEUE_OBJECT(self, data_out); + return 0; + } + return -2; } int tsmrp_sender_send_file(tmsrp_sender_t* self, const char* filepath) { - tmsrp_data_out_t* data_out; - - if(!self || !filepath){ - TSK_DEBUG_ERROR("Invalid parameter"); - return -1; - } - - if((data_out = tmsrp_data_out_file_create(filepath))){ - if(TMSRP_DATA(data_out)->isOK){ - TSK_RUNNABLE_ENQUEUE_OBJECT(self, data_out); - return 0; - } - else{ - TSK_OBJECT_SAFE_FREE(data_out); - return -3; - } - } - return -2; + tmsrp_data_out_t* data_out; + + if(!self || !filepath) { + TSK_DEBUG_ERROR("Invalid parameter"); + return -1; + } + + if((data_out = tmsrp_data_out_file_create(filepath))) { + if(TMSRP_DATA(data_out)->isOK) { + TSK_RUNNABLE_ENQUEUE_OBJECT(self, data_out); + return 0; + } + else { + TSK_OBJECT_SAFE_FREE(data_out); + return -3; + } + } + return -2; } int tmsrp_sender_stop(tmsrp_sender_t* self) { - int ret = -1; + int ret = -1; - if(!self){ - TSK_DEBUG_ERROR("Invalid parameter"); - goto bail; - } + if(!self) { + TSK_DEBUG_ERROR("Invalid parameter"); + goto bail; + } - if((ret = tsk_runnable_stop(TSK_RUNNABLE(self)))){ - goto bail; - } + if((ret = tsk_runnable_stop(TSK_RUNNABLE(self)))) { + goto bail; + } bail: - return ret; + return ret; } @@ -140,109 +140,109 @@ bail: static void* TSK_STDCALL run(void* self) { - tsk_list_item_t *curr; - tmsrp_sender_t *sender = (tmsrp_sender_t*)self; - tmsrp_data_out_t *data_out; - tsk_buffer_t* chunck, *message = tsk_buffer_create_null(); - tsk_size_t start; - tsk_size_t end; - tsk_size_t total; - tsk_istr_t tid; - int64_t __now = (int64_t)tsk_time_now(); - tsk_bool_t error = tsk_false; - - TSK_DEBUG_INFO("MSRP SENDER::run -- START"); - - TSK_RUNNABLE_RUN_BEGIN(sender); - - if((curr = TSK_RUNNABLE_POP_FIRST(sender))){ - if(!(data_out = (tmsrp_data_out_t*)curr->data)){ - continue; - } - - error = tsk_false; - start = 1; - total = data_out->size; - - while(TSK_RUNNABLE(self)->running && !error && (chunck = tmsrp_data_out_get(data_out))){ - tmsrp_request_t* SEND; - // set end - end = (start + chunck->size) - 1; - // compute new transaction id - tsk_itoa(++__now, &tid); - // create SEND request - SEND = tmsrp_request_create(tid, "SEND"); - // T-Path and From-Path (because of otherURIs) - SEND->To = tsk_object_ref(sender->config->To_Path); - SEND->From = tsk_object_ref(sender->config->From_Path); - // add other headers - tmsrp_message_add_headers(SEND, - TMSRP_HEADER_MESSAGE_ID_VA_ARGS(TMSRP_DATA(data_out)->id), - // TMSRP_HEADER_BYTE_RANGE_VA_ARGS(start, end, total), => See below - TMSRP_HEADER_FAILURE_REPORT_VA_ARGS(sender->config->Failure_Report ? freport_yes : freport_no), - TMSRP_HEADER_SUCCESS_REPORT_VA_ARGS(sender->config->Success_Report), - - tsk_null); - // add data - if(start == 1 && chunck->size && tsk_striequals(TMSRP_DATA(data_out)->ctype, "message/CPIM")){ - tsk_buffer_t* content_cpim = tsk_buffer_create_null(); - if(content_cpim){ - tsk_buffer_append_2(content_cpim, "Subject: %s\r\n\r\nContent-Type: %s\r\n\r\n", - "test", TMSRP_DATA(data_out)->wctype); - end += content_cpim->size; - total += content_cpim->size; - tsk_buffer_append(content_cpim, chunck->data, chunck->size); - tmsrp_message_add_content(SEND, TMSRP_DATA(data_out)->ctype, content_cpim->data, content_cpim->size); - TSK_OBJECT_SAFE_FREE(content_cpim); - } - else{ - TSK_DEBUG_ERROR("Failed to allocate new buffer"); - } - } - else{ - tmsrp_message_add_content(SEND, TMSRP_DATA(data_out)->ctype, chunck->data, chunck->size); - } - // add byte range here not before: think about message/cpim - tmsrp_message_add_headers(SEND, - TMSRP_HEADER_BYTE_RANGE_VA_ARGS(start, end, total), - - tsk_null); - - - // set continuation flag - SEND->end_line.cflag = (end == total) ? '$' : '+'; - // serialize and send - if(!(tmsrp_message_serialize(SEND, message))){ - if(tnet_sockfd_send(sender->fd, message->data, message->size, 0) == 0){ - error = tsk_true; - // abort - } - } - tsk_buffer_cleanup(message); - - // set start - start = (end + 1); - // cleanup - TSK_OBJECT_SAFE_FREE(chunck); - TSK_OBJECT_SAFE_FREE(SEND); - - /* wait */ - if(sender->chunck_duration){ - tsk_thread_sleep(sender->chunck_duration); - } - } - - - tsk_object_unref(curr); - } - - TSK_RUNNABLE_RUN_END(self); - - TSK_OBJECT_SAFE_FREE(message); - - TSK_DEBUG_INFO("MSRP SENDER::run -- STOP"); - - return 0; + tsk_list_item_t *curr; + tmsrp_sender_t *sender = (tmsrp_sender_t*)self; + tmsrp_data_out_t *data_out; + tsk_buffer_t* chunck, *message = tsk_buffer_create_null(); + tsk_size_t start; + tsk_size_t end; + tsk_size_t total; + tsk_istr_t tid; + int64_t __now = (int64_t)tsk_time_now(); + tsk_bool_t error = tsk_false; + + TSK_DEBUG_INFO("MSRP SENDER::run -- START"); + + TSK_RUNNABLE_RUN_BEGIN(sender); + + if((curr = TSK_RUNNABLE_POP_FIRST(sender))) { + if(!(data_out = (tmsrp_data_out_t*)curr->data)) { + continue; + } + + error = tsk_false; + start = 1; + total = data_out->size; + + while(TSK_RUNNABLE(self)->running && !error && (chunck = tmsrp_data_out_get(data_out))) { + tmsrp_request_t* SEND; + // set end + end = (start + chunck->size) - 1; + // compute new transaction id + tsk_itoa(++__now, &tid); + // create SEND request + SEND = tmsrp_request_create(tid, "SEND"); + // T-Path and From-Path (because of otherURIs) + SEND->To = tsk_object_ref(sender->config->To_Path); + SEND->From = tsk_object_ref(sender->config->From_Path); + // add other headers + tmsrp_message_add_headers(SEND, + TMSRP_HEADER_MESSAGE_ID_VA_ARGS(TMSRP_DATA(data_out)->id), + // TMSRP_HEADER_BYTE_RANGE_VA_ARGS(start, end, total), => See below + TMSRP_HEADER_FAILURE_REPORT_VA_ARGS(sender->config->Failure_Report ? freport_yes : freport_no), + TMSRP_HEADER_SUCCESS_REPORT_VA_ARGS(sender->config->Success_Report), + + tsk_null); + // add data + if(start == 1 && chunck->size && tsk_striequals(TMSRP_DATA(data_out)->ctype, "message/CPIM")) { + tsk_buffer_t* content_cpim = tsk_buffer_create_null(); + if(content_cpim) { + tsk_buffer_append_2(content_cpim, "Subject: %s\r\n\r\nContent-Type: %s\r\n\r\n", + "test", TMSRP_DATA(data_out)->wctype); + end += content_cpim->size; + total += content_cpim->size; + tsk_buffer_append(content_cpim, chunck->data, chunck->size); + tmsrp_message_add_content(SEND, TMSRP_DATA(data_out)->ctype, content_cpim->data, content_cpim->size); + TSK_OBJECT_SAFE_FREE(content_cpim); + } + else { + TSK_DEBUG_ERROR("Failed to allocate new buffer"); + } + } + else { + tmsrp_message_add_content(SEND, TMSRP_DATA(data_out)->ctype, chunck->data, chunck->size); + } + // add byte range here not before: think about message/cpim + tmsrp_message_add_headers(SEND, + TMSRP_HEADER_BYTE_RANGE_VA_ARGS(start, end, total), + + tsk_null); + + + // set continuation flag + SEND->end_line.cflag = (end == total) ? '$' : '+'; + // serialize and send + if(!(tmsrp_message_serialize(SEND, message))) { + if(tnet_sockfd_send(sender->fd, message->data, message->size, 0) == 0) { + error = tsk_true; + // abort + } + } + tsk_buffer_cleanup(message); + + // set start + start = (end + 1); + // cleanup + TSK_OBJECT_SAFE_FREE(chunck); + TSK_OBJECT_SAFE_FREE(SEND); + + /* wait */ + if(sender->chunck_duration) { + tsk_thread_sleep(sender->chunck_duration); + } + } + + + tsk_object_unref(curr); + } + + TSK_RUNNABLE_RUN_END(self); + + TSK_OBJECT_SAFE_FREE(message); + + TSK_DEBUG_INFO("MSRP SENDER::run -- STOP"); + + return 0; } @@ -252,35 +252,34 @@ static void* TSK_STDCALL run(void* self) // static void* tmsrp_sender_ctor(tsk_object_t * self, va_list *app) { - tmsrp_sender_t *sender = (tmsrp_sender_t*)self; - if(sender){ - sender->config = (tmsrp_config_t*)tsk_object_ref(va_arg(*app, tmsrp_config_t*)); - sender->fd = va_arg(*app, tnet_fd_t); - - sender->outgoingList = tsk_list_create(); - } - return self; + tmsrp_sender_t *sender = (tmsrp_sender_t*)self; + if(sender) { + sender->config = (tmsrp_config_t*)tsk_object_ref(va_arg(*app, tmsrp_config_t*)); + sender->fd = va_arg(*app, tnet_fd_t); + + sender->outgoingList = tsk_list_create(); + } + return self; } static void* tmsrp_sender_dtor(tsk_object_t * self) -{ - tmsrp_sender_t *sender = self; - if(sender){ - /* Stop */ - tmsrp_sender_stop(sender); - - TSK_OBJECT_SAFE_FREE(sender->config); - TSK_OBJECT_SAFE_FREE(sender->outgoingList); - // the FD is owned by the transport ...do not close it - } - return self; +{ + tmsrp_sender_t *sender = self; + if(sender) { + /* Stop */ + tmsrp_sender_stop(sender); + + TSK_OBJECT_SAFE_FREE(sender->config); + TSK_OBJECT_SAFE_FREE(sender->outgoingList); + // the FD is owned by the transport ...do not close it + } + return self; } -static const tsk_object_def_t tmsrp_sender_def_s = -{ - sizeof(tmsrp_sender_t), - tmsrp_sender_ctor, - tmsrp_sender_dtor, - tsk_null, +static const tsk_object_def_t tmsrp_sender_def_s = { + sizeof(tmsrp_sender_t), + tmsrp_sender_ctor, + tmsrp_sender_dtor, + tsk_null, }; const tsk_object_def_t *tmsrp_sender_def_t = &tmsrp_sender_def_s; |