@@ -1014,85 +1014,3 @@ void daemonize(void) {
10141014 if (fd > STDERR_FILENO ) close (fd );
10151015 }
10161016}
1017-
1018- /*
1019- * =================================
1020- * Internal module implementations
1021- * =================================
1022- */
1023-
1024- static void subscriber_free (const struct ref * r ) {
1025- struct subscriber * sub = container_of (r , struct subscriber , refcount );
1026- free_memory (sub );
1027- }
1028-
1029- static void session_free (const struct ref * refcount ) {
1030- struct client_session * session =
1031- container_of (refcount , struct client_session , refcount );
1032- list_destroy (session -> subscriptions , 0 );
1033- list_destroy (session -> outgoing_msgs , 0 );
1034- if (has_inflight (session )) {
1035- for (int i = 0 ; i < MAX_INFLIGHT_MSGS ; ++ i ) {
1036- if (session -> i_msgs [i ].packet )
1037- DECREF (session -> i_msgs [i ].packet , struct mqtt_packet );
1038- }
1039- }
1040- free_memory (session -> i_acks );
1041- free_memory (session -> i_msgs );
1042- free_memory (session );
1043- }
1044-
1045- void session_init (struct client_session * session , const char * session_id ) {
1046- session -> inflights = ATOMIC_VAR_INIT (0 );
1047- session -> next_free_mid = 1 ;
1048- session -> subscriptions = list_new (NULL );
1049- session -> outgoing_msgs = list_new (NULL );
1050- snprintf (session -> session_id , MQTT_CLIENT_ID_LEN , "%s" , session_id );
1051- session -> i_acks = try_calloc (MAX_INFLIGHT_MSGS , sizeof (time_t ));
1052- session -> i_msgs = try_calloc (MAX_INFLIGHT_MSGS , sizeof (struct inflight_msg ));
1053- session -> refcount = (struct ref ) { session_free , 0 };
1054- }
1055-
1056- struct client_session * client_session_alloc (const char * session_id ) {
1057- struct client_session * session = try_alloc (sizeof (* session ));
1058- session_init (session , session_id );
1059- return session ;
1060- }
1061-
1062- bool is_subscribed (const struct topic * t , const struct client_session * s ) {
1063- struct subscriber * dummy = NULL ;
1064- HASH_FIND_STR (t -> subscribers , s -> session_id , dummy );
1065- return dummy != NULL ;
1066- }
1067-
1068- struct subscriber * subscriber_new (struct topic * t ,
1069- struct client_session * s ,
1070- unsigned char qos ) {
1071- struct subscriber * sub = try_alloc (sizeof (* sub ));
1072- sub -> session = s ;
1073- sub -> granted_qos = qos ;
1074- sub -> refcount = (struct ref ) { .count = 0 , .free = subscriber_free };
1075- memcpy (sub -> id , s -> session_id , MQTT_CLIENT_ID_LEN );
1076- return sub ;
1077- }
1078-
1079- struct subscriber * subscriber_clone (const struct subscriber * s ) {
1080- struct subscriber * sub = try_alloc (sizeof (* sub ));
1081- sub -> session = s -> session ;
1082- sub -> granted_qos = s -> granted_qos ;
1083- sub -> refcount = (struct ref ) { .count = 0 , .free = subscriber_free };
1084- memcpy (sub -> id , s -> id , MQTT_CLIENT_ID_LEN );
1085- return sub ;
1086- }
1087-
1088- void inflight_msg_init (struct inflight_msg * imsg , struct mqtt_packet * p ) {
1089- imsg -> seen = time (NULL );
1090- imsg -> packet = p ;
1091- imsg -> qos = p -> header .bits .qos ;
1092- }
1093-
1094- unsigned next_free_mid (struct client_session * session ) {
1095- if (session -> next_free_mid == MAX_INFLIGHT_MSGS )
1096- session -> next_free_mid = 1 ;
1097- return session -> next_free_mid ++ ;
1098- }
0 commit comments