From 31f78d8cac6c0ebc613e7ec53561ee5753df5010 Mon Sep 17 00:00:00 2001 From: Seven Du Date: Thu, 4 Sep 2014 09:00:38 +0800 Subject: [PATCH] add ability to read from a stream do not use this unless you understand what you are doing and you want to take the risk, you are warnned --- src/include/switch_cpp.h | 3 +++ src/include/switch_module_interfaces.h | 1 + src/include/switch_types.h | 1 + src/mod/languages/mod_lua/freeswitch.i | 2 ++ src/mod/languages/mod_lua/my_swigable_cpp.h | 2 ++ src/switch_cpp.cpp | 25 +++++++++++++++++++++ 6 files changed, 34 insertions(+) diff --git a/src/include/switch_cpp.h b/src/include/switch_cpp.h index 49f4c116dd..2db54e8627 100644 --- a/src/include/switch_cpp.h +++ b/src/include/switch_cpp.h @@ -3,6 +3,7 @@ #ifdef __cplusplus +#include extern "C" { #endif #ifdef DOH @@ -149,7 +150,9 @@ SWITCH_DECLARE(bool) email(char *to, char *from, char *headers = NULL, char *bod SWITCH_DECLARE_CONSTRUCTOR Stream(void); SWITCH_DECLARE_CONSTRUCTOR Stream(switch_stream_handle_t *); virtual SWITCH_DECLARE_CONSTRUCTOR ~ Stream(); + SWITCH_DECLARE(std::string) read(); SWITCH_DECLARE(void) write(const char *data); + SWITCH_DECLARE(void) raw_write(std::string data); SWITCH_DECLARE(const char *) get_data(void); }; diff --git a/src/include/switch_module_interfaces.h b/src/include/switch_module_interfaces.h index aff702962b..0f4eebf239 100644 --- a/src/include/switch_module_interfaces.h +++ b/src/include/switch_module_interfaces.h @@ -91,6 +91,7 @@ struct switch_state_handler_table { }; struct switch_stream_handle { + switch_stream_handle_read_function_t read_function; switch_stream_handle_write_function_t write_function; switch_stream_handle_raw_write_function_t raw_write_function; void *data; diff --git a/src/include/switch_types.h b/src/include/switch_types.h index 55f1bd1614..de5c959192 100644 --- a/src/include/switch_types.h +++ b/src/include/switch_types.h @@ -2152,6 +2152,7 @@ typedef void (*switch_scheduler_func_t) (switch_scheduler_task_t *task); typedef switch_status_t (*switch_state_handler_t) (switch_core_session_t *); typedef struct switch_stream_handle switch_stream_handle_t; +typedef uint8_t * (*switch_stream_handle_read_function_t) (switch_stream_handle_t *handle, int *len); typedef switch_status_t (*switch_stream_handle_write_function_t) (switch_stream_handle_t *handle, const char *fmt, ...); typedef switch_status_t (*switch_stream_handle_raw_write_function_t) (switch_stream_handle_t *handle, uint8_t *data, switch_size_t datalen); diff --git a/src/mod/languages/mod_lua/freeswitch.i b/src/mod/languages/mod_lua/freeswitch.i index d94ce07fb2..4f50313588 100644 --- a/src/mod/languages/mod_lua/freeswitch.i +++ b/src/mod/languages/mod_lua/freeswitch.i @@ -43,6 +43,8 @@ %newobject API::execute; %newobject API::executeString; +%include "std_string.i" + /** * tell swig to grok everything defined in these header files and * build all sorts of c wrappers and lua shadows of the c wrappers. diff --git a/src/mod/languages/mod_lua/my_swigable_cpp.h b/src/mod/languages/mod_lua/my_swigable_cpp.h index ebe597260f..60538b7f91 100644 --- a/src/mod/languages/mod_lua/my_swigable_cpp.h +++ b/src/mod/languages/mod_lua/my_swigable_cpp.h @@ -22,7 +22,9 @@ class Stream { Stream(void); Stream(switch_stream_handle_t *); virtual ~ Stream(); + string read(); void write(const char *data); + void raw_write(void *data, int len); const char *get_data(void); }; diff --git a/src/switch_cpp.cpp b/src/switch_cpp.cpp index 778c53d20e..df45e6a1da 100644 --- a/src/switch_cpp.cpp +++ b/src/switch_cpp.cpp @@ -531,12 +531,37 @@ SWITCH_DECLARE_CONSTRUCTOR Stream::~Stream() } } +/* WARNING!! you are not encouraged to use this unless you understand the risk!!! */ +SWITCH_DECLARE(std::string) Stream::read() +{ + uint8_t *buff; + this_check(std::string("")); + int len = 0; + + this_check(std::string()); + + if (!stream_p->read_function) return std::string(); + + buff = stream_p->read_function(stream_p, &len); + + if (!buff) return std::string(); + if (len < 0) return std::string(); + + return std::string((const char *)buff, len); +} + SWITCH_DECLARE(void) Stream::write(const char *data) { this_check_void(); stream_p->write_function(stream_p, "%s", data); } +SWITCH_DECLARE(void) Stream::raw_write(std::string data) +{ + this_check_void(); + stream_p->raw_write_function(stream_p, (uint8_t *)data.c_str(), data.length()); +} + SWITCH_DECLARE(const char *)Stream::get_data() { this_check("");