loudmouth/lm-connection.c
changeset 262 d372a2b61b1d
parent 261 703c5734ec61
child 266 730617b8c682
equal deleted inserted replaced
261:703c5734ec61 262:d372a2b61b1d
    97 
    97 
    98 	guint         io_watch_out;
    98 	guint         io_watch_out;
    99 	GString      *out_buf;
    99 	GString      *out_buf;
   100 
   100 
   101 	LmConnectData *connect_data;
   101 	LmConnectData *connect_data;
       
   102 
       
   103 	LmDisconnectReason disconnect_reason;
   102 
   104 
   103 	gint          ref_count;
   105 	gint          ref_count;
   104 };
   106 };
   105 
   107 
   106 typedef enum {
   108 typedef enum {
   154 static LmHandlerResult connection_auth_reply (LmMessageHandler    *handler,
   156 static LmHandlerResult connection_auth_reply (LmMessageHandler    *handler,
   155 					      LmConnection        *connection,
   157 					      LmConnection        *connection,
   156 					      LmMessage           *m,
   158 					      LmMessage           *m,
   157 					      gpointer             user_data);
   159 					      gpointer             user_data);
   158 
   160 
       
   161 static void     connection_stream_error      (LmConnection        *connection,
       
   162 					      LmMessage           *m);
   159 static void     connection_stream_received   (LmConnection        *connection, 
   163 static void     connection_stream_received   (LmConnection        *connection, 
   160 					      LmMessage           *m);
   164 					      LmMessage           *m);
   161 
   165 
   162 static gint     connection_handler_compare_func (HandlerData  *a,
   166 static gint     connection_handler_compare_func (HandlerData  *a,
   163 						 HandlerData  *b);
   167 						 HandlerData  *b);
   275 
   279 
   276 	lm_connection_ref (connection);
   280 	lm_connection_ref (connection);
   277 
   281 
   278 	if (lm_message_get_type (m) == LM_MESSAGE_TYPE_STREAM) {
   282 	if (lm_message_get_type (m) == LM_MESSAGE_TYPE_STREAM) {
   279 		connection_stream_received (connection, m);
   283 		connection_stream_received (connection, m);
       
   284 		goto out;
       
   285 	}
       
   286 	else if (lm_message_get_type (m) == LM_MESSAGE_TYPE_STREAM_ERROR) {
       
   287 		connection_stream_error (connection, m);
   280 		goto out;
   288 		goto out;
   281 	}
   289 	}
   282 	
   290 	
   283 	id = lm_message_node_get_attribute (m->node, "id");
   291 	id = lm_message_node_get_attribute (m->node, "id");
   284 	
   292 	
   589 		       "Connection success.\n");
   597 		       "Connection success.\n");
   590 
   598 
   591 		_lm_connection_succeeded (connect_data);
   599 		_lm_connection_succeeded (connect_data);
   592 	}
   600 	}
   593 
   601 
       
   602 	/* set the default disconnect reason to unknown */
       
   603 	connection->disconnect_reason = LM_DISCONNECT_REASON_UNKNOWN;
       
   604 
   594  	return TRUE; 
   605  	return TRUE; 
   595 }
   606 }
   596 
   607 
   597 static const char *
   608 static const char *
   598 connection_condition_to_str (GIOCondition condition)
   609 connection_condition_to_str (GIOCondition condition)
  1106 		gint reason;
  1117 		gint reason;
  1107 		
  1118 		
  1108 		switch (status) {
  1119 		switch (status) {
  1109 		case G_IO_STATUS_EOF:
  1120 		case G_IO_STATUS_EOF:
  1110 			reason = LM_DISCONNECT_REASON_HUP;
  1121 			reason = LM_DISCONNECT_REASON_HUP;
       
  1122 			lm_verbose ("Disconnect reason: %d\n", reason);
  1111 			break;
  1123 			break;
  1112 		case G_IO_STATUS_AGAIN:
  1124 		case G_IO_STATUS_AGAIN:
  1113 			/* No data readable but we didn't hangup */
  1125 			/* No data readable but we didn't hangup */
  1114 			return FALSE;
  1126 			return FALSE;
  1115 			break;
  1127 			break;
  1116 		case G_IO_STATUS_ERROR:
  1128 		case G_IO_STATUS_ERROR:
  1117 			reason = LM_DISCONNECT_REASON_ERROR;
  1129 			/* If no reason set, we set it to ERROR here,
  1118 			break;
  1130 			 * otherwise it might already have been set by
       
  1131 			 * the stream error handler.
       
  1132 			 */
       
  1133 			if (connection->disconnect_reason == LM_DISCONNECT_REASON_UNKNOWN) {
       
  1134 				connection->disconnect_reason = LM_DISCONNECT_REASON_ERROR;
       
  1135 			}
  1119 		default:
  1136 		default:
  1120 			reason = LM_DISCONNECT_REASON_UNKNOWN;
  1137 			reason = connection->disconnect_reason;
       
  1138 			lm_verbose ("Disconnect reason: %d\n", reason);
  1121 		}
  1139 		}
  1122 
  1140 
  1123 		connection_do_close (connection);
  1141 		connection_do_close (connection);
  1124 		connection_signal_disconnect (connection, reason);
  1142 		connection_signal_disconnect (connection, reason);
  1125 
  1143 
  1438 	}
  1456 	}
  1439 	
  1457 	
  1440 	return LM_HANDLER_RESULT_REMOVE_MESSAGE;
  1458 	return LM_HANDLER_RESULT_REMOVE_MESSAGE;
  1441 }
  1459 }
  1442 
  1460 
       
  1461 static void
       
  1462 connection_stream_error (LmConnection *connection, LmMessage *m)
       
  1463 {
       
  1464 	LmMessageNode *node;
       
  1465 
       
  1466 	g_return_if_fail (connection != NULL);
       
  1467 	g_return_if_fail (m != NULL);
       
  1468 
       
  1469 	node = m->node;
       
  1470 
       
  1471 	/* Resource conflict */
       
  1472 	node = lm_message_node_get_child (node, "conflict");
       
  1473 	if (node) {
       
  1474 		lm_verbose ("Stream error: Conflict (resource connected elsewhere)\n");
       
  1475 		connection->disconnect_reason = LM_DISCONNECT_REASON_RESOURCE_CONFLICT;
       
  1476 		return;
       
  1477 	}
       
  1478 
       
  1479 	/* XML is crack */
       
  1480 	node = lm_message_node_get_child (node, "xml-not-well-formed");
       
  1481 	if (node) {
       
  1482 		lm_verbose ("Stream error: XML not well formed\n");
       
  1483 		connection->disconnect_reason = LM_DISCONNECT_REASON_INVALID_XML;
       
  1484 		return;
       
  1485 	}
       
  1486 
       
  1487 	lm_verbose ("Stream error: Unrecognised error\n");
       
  1488 	connection->disconnect_reason = LM_DISCONNECT_REASON_ERROR;
       
  1489 }
  1443 
  1490 
  1444 static void
  1491 static void
  1445 connection_stream_received (LmConnection *connection, LmMessage *m)
  1492 connection_stream_received (LmConnection *connection, LmMessage *m)
  1446 {
  1493 {
  1447 	gboolean result;
  1494 	gboolean result;
  1583 	
  1630 	
  1584 	connection->id_handlers = g_hash_table_new_full (g_str_hash, 
  1631 	connection->id_handlers = g_hash_table_new_full (g_str_hash, 
  1585 							 g_str_equal,
  1632 							 g_str_equal,
  1586 							 g_free, 
  1633 							 g_free, 
  1587 							 (GDestroyNotify) lm_message_handler_unref);
  1634 							 (GDestroyNotify) lm_message_handler_unref);
       
  1635 	connection->disconnect_reason = LM_DISCONNECT_REASON_UNKNOWN;
  1588 	connection->ref_count         = 1;
  1636 	connection->ref_count         = 1;
  1589 	
  1637 	
  1590 	for (i = 0; i < LM_MESSAGE_TYPE_UNKNOWN; ++i) {
  1638 	for (i = 0; i < LM_MESSAGE_TYPE_UNKNOWN; ++i) {
  1591 		connection->handlers[i] = NULL;
  1639 		connection->handlers[i] = NULL;
  1592 	}
  1640 	}