@@ -718,38 +718,73 @@ login()
718
718
SAFE_FREE (lg_msg );
719
719
}
720
720
721
- void
722
- send_msg_frp_server (struct bufferevent * bev ,
723
- const enum msg_type type ,
724
- const char * msg ,
725
- const size_t msg_len ,
726
- struct tmux_stream * stream )
721
+ static int prepare_message (const enum msg_type type ,
722
+ const char * msg ,
723
+ const size_t msg_len ,
724
+ struct msg_hdr * * msg_out ,
725
+ size_t * total_len )
727
726
{
728
- struct bufferevent * bout = NULL ;
729
- if (bev ) {
730
- bout = bev ;
731
- } else {
732
- bout = main_ctl -> connect_bev ;
727
+ // Validate inputs
728
+ if (!msg || !msg_out || !total_len ) {
729
+ debug (LOG_ERR , "Invalid input parameters" );
730
+ return -1 ;
733
731
}
734
- assert (bout );
735
732
736
-
733
+ // Calculate total message length and allocate memory
734
+ * total_len = msg_len + sizeof (struct msg_hdr );
735
+ struct msg_hdr * req_msg = calloc (* total_len , 1 );
736
+ if (!req_msg ) {
737
+ debug (LOG_ERR , "Failed to allocate memory for message" );
738
+ return -1 ;
739
+ }
737
740
738
- debug (LOG_DEBUG , "send plain msg ----> [%c: %s]" , type , msg );
739
-
740
- size_t len = msg_len + sizeof (struct msg_hdr );
741
- struct msg_hdr * req_msg = calloc (len , 1 );
742
- assert (req_msg );
741
+ // Prepare message header and content
743
742
req_msg -> type = type ;
744
743
req_msg -> length = msg_hton ((uint64_t )msg_len );
745
744
memcpy (req_msg -> data , msg , msg_len );
746
-
745
+
746
+ * msg_out = req_msg ;
747
+ return 0 ;
748
+ }
749
+
750
+ void send_msg_frp_server (struct bufferevent * bev ,
751
+ const enum msg_type type ,
752
+ const char * msg ,
753
+ const size_t msg_len ,
754
+ struct tmux_stream * stream )
755
+ {
756
+ // Get output bufferevent
757
+ struct bufferevent * bout = bev ? bev : main_ctl -> connect_bev ;
758
+ if (!bout ) {
759
+ debug (LOG_ERR , "No valid bufferevent" );
760
+ return ;
761
+ }
762
+
763
+ // Log debug info
764
+ debug (LOG_DEBUG , "Sending message: type=%d, len=%zu" , type , msg_len );
765
+ if (msg ) {
766
+ debug (LOG_DEBUG , "Message content: %s" , msg );
767
+ }
768
+
769
+ // Prepare message
770
+ struct msg_hdr * req_msg = NULL ;
771
+ size_t total_len = 0 ;
772
+ if (prepare_message (type , msg , msg_len , & req_msg , & total_len ) != 0 ) {
773
+ return ;
774
+ }
775
+
776
+ // Send message based on mux configuration
747
777
struct common_conf * c_conf = get_common_config ();
748
- if (c_conf -> tcp_mux )
749
- tmux_stream_write (bout , (uint8_t * )req_msg , len , stream );
750
- else
751
- bufferevent_write (bout , (uint8_t * )req_msg , len );
752
-
778
+ if (c_conf -> tcp_mux ) {
779
+ if (tmux_stream_write (bout , (uint8_t * )req_msg , total_len , stream ) < 0 ) {
780
+ debug (LOG_ERR , "Failed to write message through TCP mux" );
781
+ }
782
+ } else {
783
+ if (bufferevent_write (bout , (uint8_t * )req_msg , total_len ) < 0 ) {
784
+ debug (LOG_ERR , "Failed to write message directly" );
785
+ }
786
+ }
787
+
753
788
free (req_msg );
754
789
}
755
790
0 commit comments