netxclient 2.2.0

netx client assembly.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
use anyhow::{anyhow, bail, Context, Result};
use aqueue::Actor;
use data_rw::{Data, DataOwnedReader};
use log::warn;
use once_cell::sync::OnceCell;
use oneshot::{channel as oneshot, Receiver, Sender};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::atomic::{AtomicI64, Ordering};
use std::sync::Arc;
use tokio::io::{AsyncReadExt, ReadHalf};
use tokio::sync::watch::{channel, Receiver as WReceiver, Sender as WSender};
use tokio::time::{sleep, Duration};

#[cfg(all(feature = "tcpclient", not(feature = "tcp-channel-client")))]
use tcpclient::{SocketClientTrait, TcpClient};

#[cfg(feature = "tcp-channel-client")]
use tcp_channel_client::TcpClient;

use crate::client::controller::IController;
use crate::client::maybe_stream::MaybeStream;
use crate::client::request_manager::{IRequestManager, RequestManager};
use crate::client::result::RetResult;
use crate::client::NetxClientArc;

cfg_if::cfg_if! {
if #[cfg(feature = "use_openssl")]{
   use openssl::ssl::SslConnector;
   use tokio_openssl::SslStream;
   use std::pin::Pin;
}else if #[cfg(feature = "use_rustls")]{
   use tokio_rustls::TlsConnector;
   use tokio_rustls::rustls::pki_types::ServerName;
}}

/// Configuration for TLS (Transport Layer Security).
#[derive(Clone)]
pub enum TlsConfig {
    /// No TLS configuration.
    None,
    /// OpenSSL TLS configuration.
    #[cfg(all(feature = "use_openssl", not(feature = "use_rustls")))]
    OpenSsl {
        /// The domain name for the TLS connection.
        domain: String,
        /// The OpenSSL connector.
        connector: SslConnector,
    },
    /// Rustls TLS configuration.
    #[cfg(all(feature = "use_rustls", not(feature = "use_openssl")))]
    Rustls {
        /// The domain name for the TLS connection.
        domain: ServerName<'static>,
        /// The Rustls connector.
        connector: TlsConnector,
    },
}

#[cfg(all(feature = "tcpclient", not(feature = "tcp-channel-client")))]
pub type NetPeer = Actor<TcpClient<MaybeStream>>;

/// Type alias for `NetPeer` when the `tcp-channel-client` feature is enabled.
#[cfg(all(feature = "tcp-channel-client", not(feature = "tcpclient")))]
pub type NetPeer = TcpClient<MaybeStream>;

/// Type alias for the read half of a network stream.
pub type NetReadHalf = ReadHalf<MaybeStream>;

/// Represents a network client with various configurations and states.
pub struct NetXClient<T> {
    /// Information about the server.
    pub server_info: ServerOption,
    /// Configuration for TLS (Transport Layer Security).
    pub tls_config: TlsConfig,
    /// Mode of the client.
    pub mode: u8,
    /// Session information.
    pub session: T,
    /// Optional network peer.
    net: Option<Arc<NetPeer>>,
    /// Optional receiver for connection status updates.
    connect_stats: Option<WReceiver<(bool, String)>>,
    /// Dictionary to store results with their corresponding serial numbers.
    result_dict: HashMap<i64, Sender<crate::error::Result<DataOwnedReader>>>,
    /// Atomic counter for generating serial numbers.
    serial_atomic: AtomicI64,
    /// Manager for handling requests.
    request_manager: OnceCell<Arc<Actor<RequestManager<T>>>>,
    /// Optional controller for handling special functions.
    controller: Option<Box<dyn IController>>,
}

/// Trait for session management.
///
/// This trait defines methods for getting and storing session IDs,
/// which are used to manage individual sessions within the network client.
pub trait SessionSave {
    /// Gets the current session ID.
    ///
    /// # Returns
    ///
    /// * `i64` - The current session ID.
    fn get_session_id(&self) -> i64;

    /// Stores the given session ID.
    ///
    /// # Parameters
    ///
    /// * `session_id` - The session ID to store.
    fn store_session_id(&mut self, session_id: i64);
}

/// Tags for special functions that can be called by the network client.
enum SpecialFunctionTag {
    /// Tag for the connect function.
    Connect = 2147483647,
    /// Tag for the disconnect function.
    Disconnect = 2147483646,
    /// Tag for the closed function.
    Closed = 2147483645,
}

/// `Send` implementation for `NetXClient`.
unsafe impl<T> Send for NetXClient<T> {}

/// `Sync` implementation for `NetXClient`.
unsafe impl<T> Sync for NetXClient<T> {}

/// Custom `Drop` implementation for `NetXClient`.
impl<T> Drop for NetXClient<T> {
    /// Logs a debug message when the `NetXClient` is dropped.
    fn drop(&mut self) {
        log::debug!("{} is drop", self.server_info)
    }
}

/// Configuration options for the server.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ServerOption {
    /// The address of the server.
    pub addr: String,
    /// The name of the service.
    pub service_name: String,
    /// The key used for verification.
    pub verify_key: String,
    /// The timeout for requests in milliseconds.
    pub request_out_time_ms: u32,
}

/// Implementation of the `Display` trait for `ServerOption`.
///
/// This allows `ServerOption` to be formatted as a string,
/// displaying the service name and address.
impl std::fmt::Display for ServerOption {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}[{}]", self.service_name, self.addr)
    }
}

/// Implementation of the `ServerOption` struct.
impl ServerOption {
    /// Creates a new `ServerOption`.
    ///
    /// # Parameters
    ///
    /// * `addr` - The address of the server.
    /// * `service_name` - The name of the service.
    /// * `verify_key` - The key used for verification.
    /// * `request_out_time_ms` - The timeout for requests in milliseconds.
    ///
    /// # Returns
    ///
    /// * `ServerOption` - A new instance of `ServerOption`.
    pub fn new(
        addr: String,
        service_name: String,
        verify_key: String,
        request_out_time_ms: u32,
    ) -> ServerOption {
        ServerOption {
            addr,
            service_name,
            verify_key,
            request_out_time_ms,
        }
    }
}

impl<T: SessionSave + 'static> NetXClient<T> {
    cfg_if::cfg_if! {
        if #[cfg(feature = "use_openssl")] {
            /// Creates a new `NetXClient` with OpenSSL TLS configuration.
            ///
            /// # Parameters
            ///
            /// * `server_info` - Configuration options for the server.
            /// * `session` - The session information.
            /// * `domain` - The domain name for the TLS connection.
            /// * `connector` - The OpenSSL connector.
            ///
            /// # Returns
            ///
            /// * `NetxClientArc<T>` - A new instance of `NetXClient` wrapped in an `Arc`.
            pub fn new_ssl(server_info: ServerOption, session:T,domain:String,connector:SslConnector) ->NetxClientArc<T>{
                let request_out_time_ms=server_info.request_out_time_ms;
                let netx_client=Arc::new(Actor::new(NetXClient{
                    tls_config:TlsConfig::OpenSsl {domain,connector},
                    session,
                    server_info,
                    net:None,
                    result_dict:HashMap::new(),
                    connect_stats:None,
                    serial_atomic:AtomicI64::new(1),
                    request_manager:OnceCell::new(),
                    controller:None,
                    mode:0
                }));

                let request_manager=RequestManager::new(request_out_time_ms,Arc::downgrade(&netx_client));
                unsafe {
                    if netx_client.deref_inner().request_manager.set(request_manager).is_err(){
                        log::error!("not set request_manager,request_manager may not be none")
                    }
                }
                netx_client
            }
        } else if #[cfg(feature = "use_rustls")] {
             /// Creates a new `NetXClient` with Rustls TLS configuration.
             ///
             /// # Parameters
             ///
             /// * `server_info` - Configuration options for the server.
             /// * `session` - The session information.
             /// * `domain` - The domain name for the TLS connection.
             /// * `connector` - The Rustls connector.
             ///
             /// # Returns
             ///
             /// * `NetxClientArc<T>` - A new instance of `NetXClient` wrapped in an `Arc`.
             pub fn new_tls(server_info: ServerOption, session:T,domain:ServerName<'static>,connector:TlsConnector) ->NetxClientArc<T>{
                let request_out_time_ms=server_info.request_out_time_ms;
                let netx_client=Arc::new(Actor::new(NetXClient{
                    tls_config:TlsConfig::Rustls {domain,connector},
                    session,
                    server_info,
                    net:None,
                    result_dict:HashMap::new(),
                    connect_stats:None,
                    serial_atomic:AtomicI64::new(1),
                    request_manager:OnceCell::new(),
                    controller:None,
                    mode:0
                }));

                let request_manager=RequestManager::new(request_out_time_ms,Arc::downgrade(&netx_client));
                unsafe {
                    if netx_client.deref_inner().request_manager.set(request_manager).is_err(){
                        log::error!("not set request_manager,request_manager may not be none")
                    }
                }
                netx_client
            }
        }
    }

    /// Creates a new `NetXClient` without TLS configuration.
    ///
    /// # Parameters
    ///
    /// * `server_info` - Configuration options for the server.
    /// * `session` - The session information.
    ///
    /// # Returns
    ///
    /// * `NetxClientArc<T>` - A new instance of `NetXClient` wrapped in an `Arc`.
    pub fn new(server_info: ServerOption, session: T) -> NetxClientArc<T> {
        let request_out_time_ms = server_info.request_out_time_ms;
        let netx_client = Arc::new(Actor::new(NetXClient {
            tls_config: TlsConfig::None,
            session,
            server_info,
            net: None,
            result_dict: HashMap::new(),
            connect_stats: None,
            serial_atomic: AtomicI64::new(1),
            request_manager: OnceCell::new(),
            controller: None,
            mode: 0,
        }));

        let request_manager =
            RequestManager::new(request_out_time_ms, Arc::downgrade(&netx_client));
        unsafe {
            if netx_client
                .deref_inner()
                .request_manager
                .set(request_manager)
                .is_err()
            {
                log::error!("not set request_manager,request_manager may not be none")
            }
        }
        netx_client
    }

    /// Initializes the `NetXClient` with a given controller.
    ///
    /// # Parameters
    ///
    /// * `controller` - The controller to initialize the client with. It must implement the `IController` trait and be `Sync`, `Send`, and `'static`.
    #[inline]
    pub fn init<C: IController + Sync + Send + 'static>(&mut self, controller: C) {
        self.controller = Some(Box::new(controller));
    }

    /// Reads data from the network stream into the buffer and processes it.
    ///
    /// This function reads data from the provided `reader` and processes it using
    /// the `read_buffer` method. If an error occurs during reading, it logs the error.
    /// After reading, it cleans up the connection and calls the special disconnect function.
    ///
    /// # Parameters
    ///
    /// * `(netx_client, set_connect)` - A tuple containing the `NetxClientArc` and a `WSender`
    ///   for connection status updates.
    /// * `client` - An `Arc` containing the network peer.
    /// * `reader` - The read half of the network stream.
    ///
    /// # Returns
    ///
    /// * `Result<bool>` - Returns `Ok(true)` if the operation is successful, otherwise returns an error.
    #[allow(clippy::type_complexity)]
    async fn input_buffer(
        (netx_client, set_connect): (NetxClientArc<T>, WSender<(bool, String)>),
        client: Arc<NetPeer>,
        mut reader: NetReadHalf,
    ) -> Result<bool> {
        if let Err(er) = Self::read_buffer(&netx_client, set_connect, client, &mut reader).await {
            log::error!("read buffer err:{}", er);
        }
        netx_client.clean_connect().await?;
        log::debug!("disconnect to {}", netx_client.get_service_info());
        netx_client
            .call_special_function(SpecialFunctionTag::Disconnect as i32)
            .await?;
        Ok(true)
    }

    /// Reads data from the network stream into the buffer and processes it.
    ///
    /// This function reads data from the provided `reader` and processes it using
    /// the `read_buffer` method. If an error occurs during reading, it logs the error.
    /// After reading, it cleans up the connection and calls the special disconnect function.
    ///
    /// # Parameters
    ///
    /// * `netx_client` - A reference to the `NetxClientArc`.
    /// * `set_connect` - A `WSender` for connection status updates.
    /// * `client` - An `Arc` containing the network peer.
    /// * `reader` - The read half of the network stream.
    ///
    /// # Returns
    ///
    /// * `Result<()>` - Returns `Ok(())` if the operation is successful, otherwise returns an error.
    async fn read_buffer(
        netx_client: &NetxClientArc<T>,
        set_connect: WSender<(bool, String)>,
        client: Arc<NetPeer>,
        reader: &mut NetReadHalf,
    ) -> Result<()> {
        let server_info = netx_client.get_service_info();
        let mut session_id = netx_client.get_session_id();
        client
            .send_all(
                Self::get_verify_buff(
                    &server_info.service_name,
                    &server_info.verify_key,
                    &session_id,
                )
                .into_inner(),
            )
            .await?;
        let mut option_connect = Some(set_connect);
        while let Ok(len) = reader.read_u32_le().await {
            let len = (len - 4) as usize;
            let mut buff = vec![0; len];
            reader.read_exact(&mut buff).await?;
            let mut dr = DataOwnedReader::new(buff);
            let cmd = dr.read_fixed::<i32>()?;
            match cmd {
                1000 => match dr.read_fixed::<bool>()? {
                    false => {
                        let msg = dr.read_fixed_str()?;
                        log::debug!("{server_info} {msg}");
                        if (dr.len() - dr.get_offset()) == 1 && dr.read_fixed::<u8>()? == 1 {
                            log::debug!("mode 1");
                            netx_client.set_mode(1).await;
                        }
                        client
                            .send_all(
                                Self::get_session_id_buff(netx_client.get_mode()).into_inner(),
                            )
                            .await?;

                        // call connect if error disconnect
                        if let Some(set_connect) = option_connect.take() {
                            let client = client.clone();
                            let netx_client = netx_client.clone();
                            tokio::spawn(async move {
                                if let Err(err) = netx_client
                                    .call_special_function(SpecialFunctionTag::Connect as i32)
                                    .await
                                {
                                    log::error!("call connect error:{}", err);
                                    let _ = client.disconnect().await;
                                    if set_connect.send((false, err.to_string())).is_err() {
                                        log::error!("talk connect rx is close");
                                    }
                                    drop(set_connect);
                                } else {
                                    if set_connect.send((true, "success".into())).is_err() {
                                        log::error!("talk connect rx is close");
                                    }
                                    drop(set_connect);
                                }
                            });
                        }
                    }
                    true => {
                        let err = dr.read_fixed_str()?;
                        log::error!("connect {} error:{}", server_info, err);
                        if let Some(set_connect) = option_connect.take() {
                            if set_connect.send((false, err.to_string())).is_err() {
                                log::error!("talk connect rx is close");
                            }
                            drop(set_connect);
                        }
                        break;
                    }
                },
                2000 => {
                    session_id = dr.read_fixed::<i64>()?;
                    log::debug!("{} save session id:{}", server_info, session_id);
                    netx_client.store_session_id(session_id).await;
                }
                2400 => {
                    let tt = dr.read_fixed::<u8>()?;
                    let cmd = dr.read_fixed::<i32>()?;
                    let session_id = dr.read_fixed::<i64>()?;
                    match tt {
                        0 => {
                            let run_netx_client = netx_client.clone();
                            tokio::spawn(async move {
                                let _ = run_netx_client.execute_controller(tt, cmd, dr).await;
                            });
                        }
                        1 => {
                            let run_netx_client = netx_client.clone();
                            let send_client = client.clone();
                            tokio::spawn(async move {
                                let res = run_netx_client.execute_controller(tt, cmd, dr).await;
                                if let Err(er) = send_client
                                    .send_all(
                                        Self::get_result_buff(
                                            session_id,
                                            res,
                                            run_netx_client.get_mode(),
                                        )
                                        .into_inner(),
                                    )
                                    .await
                                {
                                    log::error!("send buff 1 error:{}", er);
                                }
                            });
                        }
                        2 => {
                            let run_netx_client = netx_client.clone();
                            let send_client = client.clone();
                            tokio::spawn(async move {
                                let res = run_netx_client.execute_controller(tt, cmd, dr).await;
                                if let Err(er) = send_client
                                    .send_all(
                                        Self::get_result_buff(
                                            session_id,
                                            res,
                                            run_netx_client.get_mode(),
                                        )
                                        .into_inner(),
                                    )
                                    .await
                                {
                                    log::error!("send buff 2 error:{}", er);
                                }
                            });
                        }
                        _ => {
                            panic!("not found call type:{}", tt);
                        }
                    }
                }
                2500 => {
                    let serial = dr.read_fixed::<i64>()?;
                    netx_client.set_result(serial, dr).await;
                }
                _ => {
                    log::error!("{} Unknown command:{}->{:?}", server_info, cmd, dr);
                    break;
                }
            }
        }

        Ok(())
    }

    /// Calls a special function on the controller if it exists.
    ///
    /// # Parameters
    ///
    /// * `cmd_tag` - The command tag to be sent to the controller.
    ///
    /// # Returns
    ///
    /// * `Result<()>` - Returns `Ok(())` if the operation is successful, otherwise returns an error.
    #[inline]
    pub(crate) async fn call_special_function(&self, cmd_tag: i32) -> Result<()> {
        if let Some(ref controller) = self.controller {
            controller
                .call(1, cmd_tag, DataOwnedReader::new(vec![0; 4]))
                .await?;
        }
        Ok(())
    }

    /// Executes a command on the controller if it exists.
    ///
    /// # Parameters
    ///
    /// * `tt` - The type tag for the command.
    /// * `cmd` - The command to be executed.
    /// * `dr` - The data reader containing the command data.
    ///
    /// # Returns
    ///
    /// * `Result<RetResult>` - Returns the result of the command execution if successful, otherwise returns an error.
    #[inline]
    pub(crate) async fn execute_controller(
        &self,
        tt: u8,
        cmd: i32,
        dr: DataOwnedReader,
    ) -> Result<RetResult> {
        if let Some(ref controller) = self.controller {
            return controller.call(tt, cmd, dr).await;
        }
        bail!("controller is none")
    }

    /// Generates a verification buffer.
    ///
    /// # Parameters
    ///
    /// * `service_name` - The name of the service.
    /// * `verify_key` - The key used for verification.
    /// * `session_id` - The session ID.
    ///
    /// # Returns
    ///
    /// * `Data` - The verification buffer.
    #[inline]
    fn get_verify_buff(service_name: &str, verify_key: &str, session_id: &i64) -> Data {
        let mut data = Data::with_capacity(128);
        data.write_fixed(1000);
        data.write_fixed(service_name);
        data.write_fixed(verify_key);
        data.write_fixed(session_id);
        data
    }

    /// Generates a session ID buffer.
    ///
    /// # Parameters
    ///
    /// * `mode` - The mode of the client.
    ///
    /// # Returns
    ///
    /// * `Data` - The session ID buffer.
    fn get_session_id_buff(mode: u8) -> Data {
        let mut buff = Data::with_capacity(32);
        buff.write_fixed(2000);
        if mode == 0 {
            buff
        } else {
            let len = buff.len() + 4;
            let mut data = Data::with_capacity(len);
            data.write_fixed(len as u32);
            data.write_buf(&buff);
            data
        }
    }

    /// Generates a result buffer.
    ///
    /// # Parameters
    ///
    /// * `session_id` - The session ID.
    /// * `result` - The result of the operation.
    /// * `mode` - The mode of the client.
    ///
    /// # Returns
    ///
    /// * `Data` - The result buffer.
    #[inline]
    fn get_result_buff(session_id: i64, result: RetResult, mode: u8) -> Data {
        let mut data = Data::with_capacity(1024);
        data.write_fixed(2500u32);
        data.write_fixed(session_id);
        if result.is_error {
            data.write_fixed(true);
            data.write_fixed(result.error_id);
            data.write_fixed(result.msg);
        } else {
            data.write_fixed(false);
            data.write_fixed(result.arguments.len() as u32);
            for argument in result.arguments {
                data.write_fixed(argument.into_inner());
            }
        }

        if mode == 0 {
            data
        } else {
            let len = data.len() + 4usize;
            let mut buff = Data::with_capacity(len);
            buff.write_fixed(len as u32);
            buff.write_buf(&data);
            buff
        }
    }

    /// Sets the mode of the client.
    ///
    /// # Parameters
    ///
    /// * `mode` - The mode to set.
    #[inline]
    fn set_mode(&mut self, mode: u8) {
        self.mode = mode
    }

    /// Gets the current mode of the client.
    ///
    /// # Returns
    ///
    /// * `u8` - The current mode.
    #[inline]
    pub fn get_mode(&self) -> u8 {
        self.mode
    }

    /// Gets the address of the server as a string.
    ///
    /// # Returns
    ///
    /// * `String` - The server address.
    #[inline]
    pub fn get_addr_string(&self) -> String {
        self.server_info.addr.clone()
    }

    /// Gets the service information of the server.
    ///
    /// # Returns
    ///
    /// * `ServerOption` - The service information.
    #[inline]
    pub fn get_service_info(&self) -> ServerOption {
        self.server_info.clone()
    }

    /// Gets the current session ID.
    ///
    /// # Returns
    ///
    /// * `i64` - The current session ID.
    #[inline]
    pub fn get_session_id(&self) -> i64 {
        self.session.get_session_id()
    }

    /// Stores the given session ID.
    ///
    /// # Parameters
    ///
    /// * `session_id` - The session ID to store.
    #[inline]
    pub fn store_session_id(&mut self, session_id: i64) {
        self.session.store_session_id(session_id)
    }

    /// Sets the network client.
    ///
    /// # Parameters
    ///
    /// * `client` - The network client to set.
    #[inline]
    pub fn set_network_client(&mut self, client: Arc<NetPeer>) {
        self.net = Some(client);
    }

    /// Sets the connection status receiver.
    ///
    /// # Parameters
    ///
    /// * `stats` - The connection status receiver to set.
    #[inline]
    pub fn set_connect_stats(&mut self, stats: Option<WReceiver<(bool, String)>>) {
        self.connect_stats = stats;
    }

    /// Checks if the client is connected.
    ///
    /// # Returns
    ///
    /// * `bool` - `true` if connected, `false` otherwise.
    #[inline]
    pub fn is_connect(&self) -> bool {
        self.net.is_some()
    }

    /// Generates a new serial number.
    ///
    /// # Returns
    ///
    /// * `i64` - The new serial number.
    #[inline]
    pub fn new_serial(&self) -> i64 {
        self.serial_atomic.fetch_add(1, Ordering::Acquire)
    }

    /// Gets the length of the callback dictionary.
    ///
    /// # Returns
    ///
    /// * `usize` - The length of the callback dictionary.
    #[inline]
    pub fn get_callback_len(&mut self) -> usize {
        self.result_dict.len()
    }

    /// Sets the request session ID.
    ///
    /// # Parameters
    ///
    /// * `session_id` - The session ID to set.
    ///
    /// # Returns
    ///
    /// * `Result<()>` - Returns `Ok(())` if successful, otherwise returns an error.
    #[inline]
    pub(crate) async fn set_request_session_id(&self, session_id: i64) -> crate::error::Result<()> {
        if let Some(request) = self.request_manager.get() {
            return request.set(session_id).await;
        }
        Ok(())
    }
}

/// Trait defining the internal operations for the `NetXClient`.
///
/// This trait includes methods for handling timeouts, setting results and errors,
/// calling special functions, executing controllers, cleaning connections, and resetting connection statuses.
pub(crate) trait INextClientInner {
    /// Gets the request or connect timeout time in milliseconds.
    fn get_timeout_ms(&self) -> u32;

    /// Sets the response result.
    ///
    /// # Parameters
    /// - `serial`: The serial number of the request.
    /// - `data`: The data to be set as the result.
    async fn set_result(&self, serial: i64, data: DataOwnedReader);

    /// Sets the response error.
    ///
    /// # Parameters
    /// - `serial`: The serial number of the request.
    /// - `err`: The error to be set as the result.
    async fn set_error(&self, serial: i64, err: crate::error::Error);

    /// Calls a special function (disconnect or connect command).
    ///
    /// # Parameters
    /// - `cmd_tag`: The command tag to be sent.
    ///
    /// # Returns
    /// - `Result<()>`: Returns `Ok(())` if the operation is successful, otherwise returns an error.
    async fn call_special_function(&self, cmd_tag: i32) -> Result<()>;

    /// Calls the request controller.
    ///
    /// # Parameters
    /// - `tt`: The type tag for the command.
    /// - `cmd`: The command to be executed.
    /// - `data`: The data reader containing the command data.
    ///
    /// # Returns
    /// - `RetResult`: Returns the result of the command execution if successful, otherwise returns an error.
    async fn execute_controller(&self, tt: u8, cmd: i32, data: DataOwnedReader) -> RetResult;

    /// Cleans the current connection.
    ///
    /// # Returns
    /// - `Result<()>`: Returns `Ok(())` if the operation is successful, otherwise returns an error.
    async fn clean_connect(&self) -> crate::error::Result<()>;

    /// Resets the network connection status.
    async fn reset_connect_stats(&self);

    /// Sets the mode of the client.
    ///
    /// # Parameters
    /// - `mode`: The mode to set.
    async fn set_mode(&self, mode: u8);

    /// Stores the session ID.
    ///
    /// # Parameters
    /// - `session_id`: The session ID to store.
    async fn store_session_id(&self, session_id: i64);
}

/// Implementation of the `INextClientInner` trait for `Actor<NetXClient<T>>`.
///
/// This implementation provides the internal operations for the `NetXClient`,
/// including methods for handling timeouts, setting results and errors,
/// calling special functions, executing controllers, cleaning connections,
/// and resetting connection statuses.
impl<T: SessionSave + 'static> INextClientInner for Actor<NetXClient<T>> {
    #[inline]
    fn get_timeout_ms(&self) -> u32 {
        unsafe { self.deref_inner().server_info.request_out_time_ms }
    }

    #[inline]
    async fn set_result(&self, serial: i64, data: DataOwnedReader) {
        let have_tx: Option<Sender<crate::error::Result<DataOwnedReader>>> = self
            .inner_call(|inner| async move { inner.get_mut().result_dict.remove(&serial) })
            .await;

        if let Some(tx) = have_tx {
            if tx.send(Ok(data)).is_err() {
                warn!("rx is close 1");
            }
        } else {
            match RetResult::from(data) {
                Ok(res) => match res.check() {
                    Ok(_) => log::error!("not found 2 {}", serial),
                    Err(err) => log::error!("{}", err),
                },
                Err(er) => log::error!("not found {} :{}", serial, er),
            }
        }
    }

    #[inline]
    async fn set_error(&self, serial: i64, err: crate::error::Error) {
        let have_tx: Option<Sender<crate::error::Result<DataOwnedReader>>> = self
            .inner_call(|inner| async move { inner.get_mut().result_dict.remove(&serial) })
            .await;
        if let Some(tx) = have_tx {
            if tx.send(Err(err)).is_err() {
                warn!("rx is close 2");
            }
        }
    }

    #[inline]
    async fn call_special_function(&self, cmd_tag: i32) -> Result<()> {
        unsafe { self.deref_inner().call_special_function(cmd_tag).await }
    }

    #[inline]
    async fn execute_controller(&self, tt: u8, cmd: i32, dr: DataOwnedReader) -> RetResult {
        unsafe {
            match self.deref_inner().execute_controller(tt, cmd, dr).await {
                Ok(res) => res,
                Err(err) => {
                    log::error!("call controller error:{:?}", err);
                    RetResult::error(1, format!("call controller err:{}", err))
                }
            }
        }
    }

    #[inline]
    async fn clean_connect(&self) -> crate::error::Result<()> {
        let net: Result<Arc<NetPeer>> = self
            .inner_call(|inner| async move { inner.get_mut().net.take().context("not connect") })
            .await;
        match net {
            Err(_) => Ok(()),
            Ok(net) => {
                net.disconnect().await?;
                sleep(Duration::from_millis(100)).await;
                Ok(())
            }
        }
    }

    #[inline]
    async fn reset_connect_stats(&self) {
        self.inner_call(|inner| async move {
            inner.get_mut().set_connect_stats(None);
        })
        .await
    }

    #[inline]
    async fn set_mode(&self, mode: u8) {
        self.inner_call(|inner| async move {
            inner.get_mut().set_mode(mode);
        })
        .await
    }

    #[inline]
    async fn store_session_id(&self, session_id: i64) {
        self.inner_call(|inner| async move {
            inner.get_mut().store_session_id(session_id);
        })
        .await
    }
}

#[allow(clippy::too_many_arguments)]
pub trait INetXClient {
    /// Initializes the NetX client controller.
    ///
    /// # Parameters
    /// - `controller`: The controller to initialize, which must implement the `IController` trait and be `Sync`, `Send`, and `'static`.
    ///
    /// # Returns
    /// A future that resolves to a `Result<()>`.
    fn init<C: IController + Sync + Send + 'static>(
        &self,
        controller: C,
    ) -> impl std::future::Future<Output = ()>;

    /// Connects to the network.
    ///
    /// # Returns
    /// A future that resolves to a `Result<()>`.
    fn connect_network(
        self: &Arc<Self>,
    ) -> impl std::future::Future<Output = crate::error::Result<()>> + Send;

    /// Gets the TLS configuration.
    ///
    /// # Returns
    /// The TLS configuration.
    fn get_tls_config(&self) -> TlsConfig;

    /// Gets the NetX server address.
    ///
    /// # Returns
    /// The server address as a `String`.
    fn get_address(&self) -> String;

    /// Gets the NetX client service configuration.
    ///
    /// # Returns
    /// The service configuration as a `ServerOption`.
    fn get_service_info(&self) -> ServerOption;

    /// Gets the NetX session ID.
    ///
    /// # Returns
    /// The session ID as an `i64`.
    fn get_session_id(&self) -> i64;

    /// Gets the NetX mode.
    ///
    /// # Returns
    /// The mode as a `u8`.
    fn get_mode(&self) -> u8;

    /// Generates a new serial ID.
    ///
    /// # Returns
    /// The new serial ID as an `i64`.
    fn new_serial(&self) -> i64;

    /// Checks if the client is connected to the server.
    ///
    /// # Returns
    /// `true` if connected, `false` otherwise.
    fn is_connect(&self) -> bool;

    /// Gets the TCP socket peer.
    ///
    /// # Returns
    /// A future that resolves to an `Option<Arc<NetPeer>>`.
    fn get_peer(&self) -> impl std::future::Future<Output = Option<Arc<NetPeer>>>;

    /// Sets the TCP socket peer.
    ///
    /// # Parameters
    /// - `client`: The network client to set.
    ///
    /// # Returns
    /// A future that resolves to `()`.
    fn set_network_client(&self, client: Arc<NetPeer>) -> impl std::future::Future<Output = ()>;

    /// Gets the length of the request wait callback.
    ///
    /// # Returns
    /// A future that resolves to the length as a `usize`.
    fn get_callback_len(&self) -> impl std::future::Future<Output = usize>;

    /// Closes the NetX client.
    ///
    /// # Returns
    /// A future that resolves to a `Result<()>`.
    fn close(&self) -> impl std::future::Future<Output = crate::error::Result<()>>;

    /// Calls a function with the given serial and buffer.
    ///
    /// # Parameters
    /// - `serial`: The serial ID.
    /// - `buff`: The data buffer.
    ///
    /// # Returns
    /// A future that resolves to a `Result<RetResult>`.
    fn call(
        &self,
        serial: i64,
        buff: Data,
    ) -> impl std::future::Future<Output = crate::error::Result<RetResult>>;

    /// Runs the client with the given buffer.
    ///
    /// # Parameters
    /// - `buff`: The data buffer.
    ///
    /// # Returns
    /// A future that resolves to a `Result<()>`.
    fn run(&self, buff: Data) -> impl std::future::Future<Output = crate::error::Result<()>>;
}

/// Implementation of the `INetXClient` trait for `Actor<NetXClient<T>>`.
///
/// This implementation provides the external operations for the `NetXClient`,
/// including methods for initializing the controller, connecting to the network,
/// getting configuration and session information, generating serial IDs, and
/// checking connection status.
#[allow(clippy::too_many_arguments)]
impl<T: SessionSave + 'static> INetXClient for Actor<NetXClient<T>> {
    #[inline]
    async fn init<C: IController + Sync + Send + 'static>(&self, controller: C) {
        self.inner_call(|inner| async move {
            inner.get_mut().init(controller);
        })
        .await
    }

    #[inline]
    async fn connect_network(self: &Arc<Self>) -> crate::error::Result<()> {
        let netx_client = self.clone();
        let wait_handler: crate::error::Result<Option<WReceiver<(bool, String)>>> = self.inner_call(|inner|async move  {
            if inner.get().is_connect() {
                return match inner.get().connect_stats {
                    Some(ref stats) => {
                        Ok(Some(stats.clone()))
                    },
                    None => {
                        warn!("inner is connect,but not get stats");
                        Ok(None)
                    }
                }
            }

            let (set_connect, wait_connect) = channel((false, "not connect".to_string()));

            let client={
            cfg_if::cfg_if! {
            if #[cfg(feature = "use_openssl")]{
                if let TlsConfig::OpenSsl{domain,connector}=netx_client.get_tls_config(){
                      let ssl=connector.configure()?.into_ssl(&domain)?;
                      tokio::time::timeout(Duration::from_millis(self.get_timeout_ms() as u64),TcpClient::connect_stream_type(netx_client.get_address(),|tcp_stream| async move{
                         let mut stream = SslStream::new(ssl, tcp_stream)?;
                         Pin::new(&mut stream).connect().await?;
                         Ok(MaybeStream::ServerSsl(stream))
                      },NetXClient::input_buffer, (netx_client, set_connect))).await.map_err(|_|anyhow!("connect timeout"))??
                }else{
                      tokio::time::timeout(Duration::from_millis(self.get_timeout_ms() as u64),TcpClient::connect_stream_type(netx_client.get_address(), |tcp_stream| async move{
                        Ok(MaybeStream::Plain(tcp_stream))
                      },NetXClient::input_buffer, (netx_client, set_connect))).await.map_err(|_|anyhow!("connect timeout"))??
                }
            }else if #[cfg(feature = "use_rustls")]{
                if let TlsConfig::Rustls{domain,connector}=netx_client.get_tls_config(){
                      tokio::time::timeout(Duration::from_millis(self.get_timeout_ms() as u64),TcpClient::connect_stream_type(netx_client.get_address(),|tcp_stream| async move{
                         let stream =connector.connect(domain,tcp_stream).await?;
                         Ok(MaybeStream::ServerTls(stream))
                      },NetXClient::input_buffer, (netx_client, set_connect))).await.map_err(|_|anyhow!("connect timeout"))??
                }else{
                      tokio::time::timeout(Duration::from_millis(self.get_timeout_ms() as u64),TcpClient::connect_stream_type(netx_client.get_address(), |tcp_stream| async move{
                        Ok(MaybeStream::Plain(tcp_stream))
                      },NetXClient::input_buffer, (netx_client, set_connect))).await.map_err(|_|anyhow!("connect timeout"))??
                }
            }else{
                    tokio::time::timeout(Duration::from_millis(self.get_timeout_ms() as u64),TcpClient::connect_stream_type(netx_client.get_address(), |tcp_stream| async move{
                        Ok(MaybeStream::Plain(tcp_stream))
                    },NetXClient::input_buffer, (netx_client, set_connect))).await.map_err(|_|anyhow!("connect timeout"))??
            }}};

            let ref_inner = inner.get_mut();
            ref_inner.set_network_client(client);
            ref_inner.connect_stats = Some(wait_connect.clone());
            Ok(Some(wait_connect))

        }).await;

        if let Some(mut wait_handler) = wait_handler? {
            match wait_handler.changed().await {
                Err(err) => {
                    self.reset_connect_stats().await;
                    return Err(err.into());
                }
                Ok(_) => {
                    self.reset_connect_stats().await;
                    let (is_connect, msg) = &(*wait_handler.borrow());
                    if !is_connect {
                        return Err(crate::error::Error::ConnectError(msg.clone()));
                    }
                }
            }
        }

        Ok(())
    }

    #[inline]
    fn get_tls_config(&self) -> TlsConfig {
        unsafe { self.deref_inner().tls_config.clone() }
    }

    #[inline]
    fn get_address(&self) -> String {
        unsafe { self.deref_inner().get_addr_string() }
    }

    #[inline]
    fn get_service_info(&self) -> ServerOption {
        unsafe { self.deref_inner().get_service_info() }
    }

    #[inline]
    fn get_session_id(&self) -> i64 {
        unsafe { self.deref_inner().get_session_id() }
    }

    #[inline]
    fn get_mode(&self) -> u8 {
        unsafe { self.deref_inner().get_mode() }
    }

    #[inline]
    fn new_serial(&self) -> i64 {
        unsafe { self.deref_inner().new_serial() }
    }

    #[inline]
    fn is_connect(&self) -> bool {
        unsafe { self.deref_inner().is_connect() }
    }

    #[inline]
    async fn get_peer(&self) -> Option<Arc<NetPeer>> {
        self.inner_call(|inner| async move { inner.get().net.clone() })
            .await
    }

    #[inline]
    async fn set_network_client(&self, client: Arc<NetPeer>) {
        self.inner_call(|inner| async move {
            inner.get_mut().set_network_client(client);
        })
        .await
    }

    #[inline]
    async fn get_callback_len(&self) -> usize {
        self.inner_call(|inner| async move { inner.get_mut().get_callback_len() })
            .await
    }

    #[inline]
    async fn close(&self) -> crate::error::Result<()> {
        let net: Result<Arc<NetPeer>> = self
            .inner_call(|inner| async move {
                if let Err(er) = inner
                    .get()
                    .call_special_function(SpecialFunctionTag::Closed as i32)
                    .await
                {
                    log::error!("call controller Closed err:{}", er)
                }
                inner.get_mut().controller = None;
                inner.get_mut().net.take().context("not connect")
            })
            .await;
        match net {
            Err(_) => Ok(()),
            Ok(net) => {
                net.disconnect().await?;
                sleep(Duration::from_millis(100)).await;
                Ok(())
            }
        }
    }

    #[inline]
    async fn call(&self, serial: i64, buff: Data) -> crate::error::Result<RetResult> {
        let (net, rx): (
            Arc<NetPeer>,
            Receiver<crate::error::Result<DataOwnedReader>>,
        ) = self
            .inner_call(|inner| async move {
                if let Some(ref net) = inner.get().net {
                    if inner.get_mut().result_dict.contains_key(&serial) {
                        bail!("serial is have")
                    }
                    let (tx, rx): (
                        Sender<crate::error::Result<DataOwnedReader>>,
                        Receiver<crate::error::Result<DataOwnedReader>>,
                    ) = oneshot();
                    inner.get_mut().result_dict.insert(serial, tx);
                    Ok((net.clone(), rx))
                } else {
                    bail!("not connect")
                }
            })
            .await?;
        unsafe {
            self.deref_inner().set_request_session_id(serial).await?;
        }
        if self.get_mode() == 0 {
            net.send_all(buff.into_inner()).await?;
        } else {
            let len = buff.len() + 4;
            let mut data = Data::with_capacity(len);
            data.write_fixed(len as u32);
            data.write_buf(&buff);

            net.send_all(data.into_inner()).await?;
        }
        match rx.await {
            Err(_) => Err(crate::error::Error::SerialClose(serial)),
            Ok(data) => Ok(RetResult::from(data?)?),
        }
    }

    #[inline]
    async fn run(&self, buff: Data) -> crate::error::Result<()> {
        let net = self
            .inner_call(|inner| async move {
                if let Some(ref net) = inner.get().net {
                    Ok(net.clone())
                } else {
                    bail!("not connect")
                }
            })
            .await?;
        if self.get_mode() == 0 {
            net.send_all(buff.into_inner()).await?;
        } else {
            let len = buff.len() + 4;
            let mut data = Data::with_capacity(len);
            data.write_fixed(len as u32);
            data.write_buf(&buff);
            net.send_all(data.into_inner()).await?;
        }

        Ok(())
    }
}

#[macro_export]
macro_rules! call {
    // Helper macro to count the number of arguments
    (@uint $($x:tt)*)=>(());
    (@count $($rest:expr),*)=>(<[()]>::len(&[$(call!(@uint $rest)),*]));

    // Macro to call a command and deserialize the result
    ($client:expr=>$cmd:expr;$($args:expr), *$(,)*) => ({
            if $client.is_connect() ==false{
                $client.connect_network().await?;
            }
            use data_rw::Data;
            let mut data=Data::with_capacity(128);
            let args_count=call!(@count $($args),*) as i32;
            let serial=$client.new_serial();
            data.write_fixed(2400u32);
            data.write_fixed(2u8);
            data.write_fixed($cmd);
            data.write_fixed(serial);
            data.write_fixed(args_count);
            $(data.pack_serialize($args)?;)*
            let mut ret= $client.call(serial,data).await?.check()?;
            ret.deserialize()?
    });

    // Macro to call a command and return the result
    (@result $client:expr=>$cmd:expr;$($args:expr), *$(,)*) => ({
            if $client.is_connect() ==false{
               $client.connect_network().await?;
            }
            use data_rw::Data;
            let mut data=Data::with_capacity(128);
            let args_count=call!(@count $($args),*) as i32;
            let serial=$client.new_serial();
            data.write_fixed(2400u32);
            data.write_fixed(2u8);
            data.write_fixed($cmd);
            data.write_fixed(serial);
            data.write_fixed(args_count);
            $(data.pack_serialize($args)?;)*
            $client.call(serial,data).await?
    });

    // Macro to run a command without returning a result
    (@run $client:expr=>$cmd:expr;$($args:expr), *$(,)*) => ({
            if $client.is_connect() ==false{
                $client.connect_network().await?;
            }
            use data_rw::Data;
            let mut data=Data::with_capacity(128);
            let args_count=call!(@count $($args),*) as i32;
            let serial=$client.new_serial();
            data.write_fixed(2400u32);
            data.write_fixed(0u8);
            data.write_fixed($cmd);
            data.write_fixed(serial);
            data.write_fixed(args_count);
            $(data.pack_serialize($args)?;)*
            $client.run(data).await?;
    });

    // Macro to run a command without returning a result and log errors
     (@run_not_err $client:expr=>$cmd:expr;$($args:expr), *$(,)*) => ({
            if $client.is_connect() ==false{
               if let Err(err)= $client.connect_network().await{
                    log::error!{"run connect {} is error:{}",$cmd,err}
               }
            }
            use data_rw::Data;
            let mut data=Data::with_capacity(128);
            let args_count=call!(@count $($args),*) as i32;
            let serial=$client.new_serial();
            data.write_fixed(2400u32);
            data.write_fixed(0u8);
            data.write_fixed($cmd);
            data.write_fixed(serial);
            data.write_fixed(args_count);
            $(
              if let Err(err)=  data.pack_serialize($args){
                 log::error!{"pack_serialize {} is error:{}",$cmd,err};
              }
            )*
            if let Err(err)= $client.run(data).await{
                 log::warn!{"run {} is error:{}",$cmd,err}
            }
    });

    // Macro to call a command, check the result, and return an error if the check fails
    (@checkrun $client:expr=>$cmd:expr;$($args:expr), *$(,)*) => ({
            if $client.is_connect() ==false{
                $client.connect_network().await?;
            }
            use data_rw::Data;
            let mut data=Data::with_capacity(128);
            let args_count=call!(@count $($args),*) as i32;
            let serial=$client.new_serial();
            data.write_fixed(2400u32);
            data.write_fixed(1u8);
            data.write_fixed($cmd);
            data.write_fixed(serial);
            data.write_fixed(args_count);
            $(data.pack_serialize($args)?;)*
            $client.call(serial,data).await?.check()?;

    });

}

/// Macro to create a `Box<dyn $interface>` that clones `$client`.
#[macro_export]
macro_rules! impl_interface {
    ($client:expr=>$interface:ty) => {
        paste::paste! {
              Box::new([<___impl_ $interface _call>]::new($client.clone()))  as  Box<dyn $interface>
        }
    };
}

/// Macro to create an implementation of `$interface` that clones `$client`.
#[macro_export]
macro_rules! impl_struct {
    ($client:expr=>$interface:ty) => {
        paste::paste! {
            [<___impl_ $interface _call>]::new_impl($client.clone())
        }
    };
}

/// Macro to create a struct for `$interface` that references `$client`.
#[macro_export]
macro_rules! impl_ref {
    ($client:expr=>$interface:ty) => {
        paste::paste! {
            [<___impl_ $interface _call>]::new_impl_ref(&$client)
        }
    };
}

/// Macro to create a `Box<dyn $interface>` without cloning `$client`.
#[macro_export]
macro_rules! impl_owned_interface {
    ($client:expr=>$interface:ty) => {
        paste::paste! {
              Box::new([<___impl_ $interface _call>]::new($client))  as  Box<dyn $interface>
        }
    };
}