[Telepathy-commits] [telepathy-sofiasip/master] Do one less whole-body strdup (and strlen) per received message

Mikhail Zabaluev mikhail.zabaluev at nokia.com
Fri Oct 17 15:28:51 PDT 2008


---
 src/sip-connection.c   |   53 +++++++++++++++++++++++++++++-------------------
 src/sip-text-channel.c |   17 ++++++---------
 src/sip-text-channel.h |    5 +--
 3 files changed, 41 insertions(+), 34 deletions(-)

diff --git a/src/sip-connection.c b/src/sip-connection.c
index d709f53..1d2767a 100644
--- a/src/sip-connection.c
+++ b/src/sip-connection.c
@@ -835,7 +835,9 @@ tpsip_connection_nua_i_message_cb (TpsipConnection   *self,
   TpHandleRepoIface *contact_repo;
   TpHandle handle;
   const sip_t *sip = ev->sip;
-  char *text = NULL;
+  const char *text = "";
+  gsize len = 0;
+  gboolean own_text = FALSE;
 
   /* Block anything else except text/plain messages (like isComposings) */
   if (sip->sip_content_type
@@ -853,38 +855,48 @@ tpsip_connection_nua_i_message_cb (TpsipConnection   *self,
   if (sip->sip_payload && sip->sip_payload->pl_len > 0)
     {
       const char *charset = NULL;
-      if (sip->sip_content_type && sip->sip_content_type->c_params != 0)
+      if (sip->sip_content_type)
         {
-          charset = msg_params_find (sip->sip_content_type->c_params, "charset=");
+          charset = msg_header_find_param (sip->sip_content_type->c_common,
+              "charset");
         }
 
       /* Default charset is UTF-8, we only need to convert if it's a different one */
       if (charset && g_ascii_strcasecmp (charset, "UTF-8"))
         {
           GError *error;
-          gsize in_len, out_len;
+          gsize in_len;
           text = g_convert (sip->sip_payload->pl_data, sip->sip_payload->pl_len,
-              "UTF-8", charset, &in_len, &out_len, &error);
+              "UTF-8", charset, &in_len, &len, &error);
 
           if (text == NULL)
             {
               gint status;
-              gchar *message;
+              const char *message = NULL;
 
-              status = (error->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
-                       ? 400 : 500;
-              message = g_strdup_printf ("Character set conversion failed"
-                                                " for the message body: %s",
-                                         error->message);
+              g_message ("character set conversion failed for the message body: %s", error->message);
+              g_error_free (error);
+
+              if (error->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
+                {
+                  status = 400;
+                  message = "Invalid character sequence in the message body";
+                }
+              else
+                {
+                  status = 500;
+                  message = "Character set conversion failed for the message body";
+                }
               nua_respond (ev->nua_handle,
                            status, message,
                            NUTAG_WITH_THIS(ev->nua),
                            TAG_END());
 
-              g_free (message);
-              g_error_free (error);
               goto end;
             }
+
+          own_text = TRUE;
+
           if (in_len != sip->sip_payload->pl_len)
             {
               nua_respond (ev->nua_handle,
@@ -902,18 +914,15 @@ tpsip_connection_nua_i_message_cb (TpsipConnection   *self,
                                 NULL))
             {
               nua_respond (ev->nua_handle,
-                           400, "Invalid characters in the message body",
+                           400, "Invalid character sequence in the message body",
                            NUTAG_WITH_THIS(ev->nua),
                            TAG_END());
               goto end;
             }
-          text = g_strndup (sip->sip_payload->pl_data, sip->sip_payload->pl_len);
+          text = sip->sip_payload->pl_data;
+          len = (gsize) sip->sip_payload->pl_len;
         }
     }
-  else
-    {
-      text = g_strdup ("");
-    }
 
   contact_repo = tp_base_connection_get_handles (
       (TpBaseConnection *)self, TP_HANDLE_TYPE_CONTACT);
@@ -942,7 +951,8 @@ tpsip_connection_nua_i_message_cb (TpsipConnection   *self,
                    NUTAG_WITH_SAVED(event),
                    TAG_END());
 
-      tpsip_text_channel_receive (channel, ev->nua_handle, event, handle, text);
+      tpsip_text_channel_receive (channel,
+          ev->nua_handle, event, handle, text, len);
 
       tp_handle_unref (contact_repo, handle);
     }
@@ -955,7 +965,8 @@ tpsip_connection_nua_i_message_cb (TpsipConnection   *self,
     }
 
 end:
-  g_free (text);
+  if (own_text)
+    g_free ((gpointer) text);
 
   return TRUE;
 }
diff --git a/src/sip-text-channel.c b/src/sip-text-channel.c
index bcb52e6..462d797 100644
--- a/src/sip-text-channel.c
+++ b/src/sip-text-channel.c
@@ -124,8 +124,6 @@ struct _TpsipTextChannelPrivate
 };
 
 
-#define _tpsip_text_pending_new() \
-	(g_slice_new(TpsipTextPendingMessage))
 #define _tpsip_text_pending_new0() \
 	(g_slice_new0(TpsipTextPendingMessage))
 
@@ -776,7 +774,7 @@ tpsip_text_channel_send(TpSvcChannelTypeText *iface,
 	      SIPTAG_PAYLOAD_STR(text),
 	      TAG_END());
 
-  msg = _tpsip_text_pending_new();
+  msg = _tpsip_text_pending_new0 ();
   msg->nh = msg_nh;
   msg->text = g_strdup(text);
   msg->type = type;
@@ -879,16 +877,15 @@ tpsip_text_channel_nua_r_message_cb (TpsipTextChannel *self,
 void tpsip_text_channel_receive(TpsipTextChannel *chan,
                                 nua_handle_t *nh,
                                 nua_saved_event_t *event,
-			        TpHandle sender,
-			        const char *message)
+                                TpHandle sender,
+                                const char *text,
+                                gsize len)
 {
   TpsipTextChannelPrivate *priv = TPSIP_TEXT_CHANNEL_GET_PRIVATE (chan);
   TpsipTextPendingMessage *msg;
   TpHandleRepoIface *contact_repo;
 
-  DEBUG("enter");
-
-  msg = _tpsip_text_pending_new();
+  msg = _tpsip_text_pending_new0 ();
 
   msg->id = priv->recv_id++;
   msg->timestamp = time(NULL);
@@ -896,7 +893,7 @@ void tpsip_text_channel_receive(TpsipTextChannel *chan,
   msg->saved_event = event;
   msg->sender = sender;
   msg->type = TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
-  msg->text = g_strdup(message);
+  msg->text = g_strndup (text, len);
 
   g_queue_push_tail(priv->pending_messages, msg);
 
@@ -907,7 +904,7 @@ void tpsip_text_channel_receive(TpsipTextChannel *chan,
 
   tp_handle_ref (contact_repo, sender);
 
-  DEBUG("received message: %s", message);
+  DEBUG("received message: %s", text);
 
   tp_svc_channel_type_text_emit_received ((TpSvcChannelTypeText *)chan,
       msg->id, msg->timestamp, msg->sender, msg->type,
diff --git a/src/sip-text-channel.h b/src/sip-text-channel.h
index 0b84bef..1fc7cdb 100644
--- a/src/sip-text-channel.h
+++ b/src/sip-text-channel.h
@@ -27,8 +27,6 @@
 
 #include <tpsip/sofia-decls.h>
 
-typedef struct _TpsipHandleStorage TpsipHandleStorage;
-
 G_BEGIN_DECLS
 
 typedef struct _TpsipTextChannel TpsipTextChannel;
@@ -67,7 +65,8 @@ void tpsip_text_channel_receive (TpsipTextChannel  *obj,
                                  nua_handle_t      *nh,
                                  nua_saved_event_t *event,
                                  TpHandle           sender,
-                                 const char        *message);
+                                 const char        *text,
+                                 gsize              len);
 
 G_END_DECLS
 
-- 
1.5.6.5




More information about the Telepathy-commits mailing list