Skip to content

NetGear API References

NetGear API usage examples can be found here âž¶

NetGear API parameters are explained here âž¶

NetGear ¶

NetGear is exclusively designed to transfer video frames synchronously and asynchronously between interconnecting systems over the network in real-time.

NetGear implements a high-level wrapper around PyZmQ python library that contains python bindings for ZeroMQ - a high-performance asynchronous distributed messaging library that provides a message queue, but unlike message-oriented middleware, its system can run without a dedicated message broker.

NetGear also supports real-time Frame Compression capabilities for optimizing performance while sending the frames directly over the network, by encoding the frame before sending it and decoding it on the client's end automatically in real-time.

Info

NetGear API now internally implements robust Lazy Pirate pattern (auto-reconnection) for its synchronous messaging patterns (i.e. zmq.PAIR & zmq.REQ/zmq.REP) at both Server and Client ends, where its API instead of doing a blocking receive, will:

  • Poll the socket and receive from it only when it's sure a reply has arrived.
  • Attempt to reconnect, if no reply has arrived within a timeout period.
  • Abandon the connection if there is still no reply after several requests.

NetGear as of now seamlessly supports four ZeroMQ messaging patterns:

  • zmq.PAIR (ZMQ Pair Pattern)
  • zmq.REQ/zmq.REP (ZMQ Request/Reply Pattern)
  • zmq.PUB/zmq.SUB (ZMQ Publish/Subscribe Pattern)
  • zmq.PUSH/zmq.PULL (ZMQ Push/Pull Pattern)

whereas the supported protocol are: tcp and ipc.

In every pattern the receiver (Client) binds and the sender (Server) connects, so address is always the receiver's address.

Modes of Operation
  • Primary Modes

    NetGear API primarily has two modes of operations:

    • Send Mode: which employs send() function to send video frames over the network in real-time.

    • Receive Mode: which employs recv() function to receive frames, sent over the network with Send Mode in real-time. The mode sends back confirmation when the frame is received successfully in few patterns.

  • Exclusive Modes

    In addition to these primary modes, NetGear API offers applications-specific Exclusive Modes:

    • Multi-Servers Mode: In this exclusive mode, NetGear API robustly handles multiple servers at once, thereby providing seamless access to frames and unidirectional data transfer from multiple Servers/Publishers across the network in real-time.

    • Multi-Clients Mode: In this exclusive mode, NetGear API robustly handles multiple clients at once, thereby providing seamless access to frames and unidirectional data transfer to multiple Client/Consumers across the network in real-time.

    • Bidirectional Mode: This exclusive mode provides seamless support for bidirectional data transmission between between Server and Client along with video frames.

    • Secure Mode: In this exclusive mode, NetGear API provides easy access to powerful, smart & secure ZeroMQ's Security Layers that enables strong encryption on data, and unbreakable authentication between the Server and Client with the help of custom certificates/keys that brings cheap, standardized privacy and authentication for distributed systems over the network.

Source code in vidgear/gears/netgear.py
  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
class NetGear:
    """
    NetGear is exclusively designed to transfer video frames synchronously and asynchronously between interconnecting systems over the network in real-time.

    NetGear implements a high-level wrapper around PyZmQ python library that contains python bindings for ZeroMQ - a high-performance asynchronous distributed messaging library
    that provides a message queue, but unlike message-oriented middleware, its system can run without a dedicated message broker.

    NetGear also supports real-time Frame Compression capabilities for optimizing performance while sending the frames directly over the network, by encoding the frame before sending
    it and decoding it on the client's end automatically in real-time.

    !!! info
        NetGear API now internally implements robust *Lazy Pirate pattern* (auto-reconnection) for its synchronous messaging patterns _(i.e. `zmq.PAIR` & `zmq.REQ/zmq.REP`)_
        at both Server and Client ends, where its API instead of doing a blocking receive, will:

        * Poll the socket and receive from it only when it's sure a reply has arrived.
        * Attempt to reconnect, if no reply has arrived within a timeout period.
        * Abandon the connection if there is still no reply after several requests.

    NetGear as of now seamlessly supports four ZeroMQ messaging patterns:

    - `zmq.PAIR` _(ZMQ Pair Pattern)_
    - `zmq.REQ/zmq.REP` _(ZMQ Request/Reply Pattern)_
    - `zmq.PUB/zmq.SUB` _(ZMQ Publish/Subscribe Pattern)_
    - `zmq.PUSH/zmq.PULL` _(ZMQ Push/Pull Pattern)_

    _whereas the supported protocol are: `tcp` and `ipc`_.

    In every pattern the **receiver (Client) binds** and the **sender (Server) connects**, so `address` is always the receiver's address.

    ??? tip "Modes of Operation"

        * **Primary Modes**

            NetGear API primarily has two modes of operations:

            * **Send Mode:** _which employs `send()` function to send video frames over the network in real-time._

            * **Receive Mode:** _which employs `recv()` function to receive frames, sent over the network with *Send Mode* in real-time. The mode sends back confirmation when the
            frame is received successfully in few patterns._

        * **Exclusive Modes**

            In addition to these primary modes, NetGear API offers applications-specific Exclusive Modes:

            * **Multi-Servers Mode:** _In this exclusive mode, NetGear API robustly **handles multiple servers at once**, thereby providing seamless access to frames and unidirectional
            data transfer from multiple Servers/Publishers across the network in real-time._

            * **Multi-Clients Mode:** _In this exclusive mode, NetGear API robustly **handles multiple clients at once**, thereby providing seamless access to frames and unidirectional
            data transfer to multiple Client/Consumers across the network in real-time._

            * **Bidirectional Mode:** _This exclusive mode **provides seamless support for bidirectional data transmission between between Server and Client along with video frames**._

            * **Secure Mode:** _In this exclusive mode, NetGear API **provides easy access to powerful, smart & secure ZeroMQ's Security Layers** that enables strong encryption on
            data, and unbreakable authentication between the Server and Client with the help of custom certificates/keys that brings cheap, standardized privacy and authentication
            for distributed systems over the network._
    """

    def __init__(
        self,
        address: str | None = None,
        port: str | None = None,
        protocol: str | None = None,
        pattern: int = 0,
        receive_mode: bool = False,
        logging: bool = False,
        **options: Any,
    ):
        """
        This constructor method initializes the object state and attributes of the NetGear class.

        Parameters:
            address (str): sets the receiver's network address (or `ipc` path).
            port (str): sets the valid Network Port of the Server/Client.
            protocol (str): sets the valid messaging protocol between Server/Client.
            pattern (int): sets the supported messaging pattern(flow of communication) between Server/Client
            receive_mode (bool): select the Netgear's Mode of operation.
            logging (bool): enables/disables logging.
            options (dict): provides the flexibility to alter various NetGear internal properties.
        """
        # enable logging if specified
        self.__logging = logging if isinstance(logging, bool) else False

        # print current version
        logcurr_vidgear_ver(logging=self.__logging)

        # raise error(s) for critical Class imports
        import_dependency_safe(
            "zmq" if zmq is None else "", min_version="4.0", pkg_name="pyzmq"
        )
        import_dependency_safe(
            "simplejpeg" if simplejpeg is None else "", error="log", min_version="1.9.0"
        )

        # define valid messaging patterns => `0`: PAIR, `1`:(REQ, REP), `2`:(PUB, SUB), `3`:(PUSH, PULL)
        valid_messaging_patterns = {
            0: (zmq.PAIR, zmq.PAIR),
            1: (zmq.REQ, zmq.REP),
            2: (zmq.PUB, zmq.SUB),
            3: (zmq.PUSH, zmq.PULL),
        }

        # Handle messaging pattern
        if not (isinstance(pattern, int) and pattern in valid_messaging_patterns):
            logger.warning(
                "Invalid pattern `{}`, Defaulting to `zmq.PAIR`! Kindly refer Docs for more Information.".format(
                    pattern
                )
            )
            pattern = 0
        self.__pattern = pattern

        # Handle messaging protocol
        if protocol not in ["tcp", "ipc"]:
            protocol is not None and logger.warning(
                "Protocol `{}` is not supported. Defaulting to `tcp` protocol!".format(
                    protocol
                )
            )
            protocol = "tcp"

        # Handle connection params
        self.__msg_flag = 0  # handles connection flags
        self.__msg_copy = False  # handles whether to copy data
        self.__msg_track = False  # handles whether to track packets

        # define Secure Mode
        self.__z_auth = None
        valid_security_mech = {0: "Grasslands", 1: "StoneHouse", 2: "IronHouse"}
        self.__secure_mode = 0  # handles ZMQ security layer status
        self.__curve_keys = None  # (secret, public, server_public) CURVE keys
        allowed_ips = None  # optional ZAP IP whitelist (receiver only)
        overwrite_cert = False  # checks if certificates overwriting allowed
        custom_cert_location = ""  # handles custom ZMQ certificates path

        # define SSH Tunneling Mode
        self.__ssh_tunnel_mode = None  # handles ssh_tunneling mode state
        self.__ssh_tunnel_pwd = None
        self.__ssh_tunnel_keyfile = None

        # define exclusive modes
        self.__multiserver_mode = False
        self.__multiclient_mode = False
        self.__bi_mode = False

        # define frame-compression handler (enabled by default if simplejpeg is installed)
        self.__jpeg_compression = simplejpeg is not None
        self.__jpeg_compression_quality = 90  # 90% quality
        self.__jpeg_compression_fastdct = True  # fastest DCT on by default
        self.__jpeg_compression_fastupsample = False  # fastupsample off by default
        self.__jpeg_compression_colorspace = "BGR"  # use BGR colorspace by default
        self.__jpeg_compression_colorsubsampling = "422"  # chroma subsampling
        self.__raw_fallback_warned = False  # warn once about uncompressible frames

        # define receiver return data handler
        self.__return_data = None

        # generate 8-digit random system id
        self.__id = "".join(
            secrets.choice(string.ascii_uppercase + string.digits) for i in range(8)
        )

        # define termination flags
        self.__terminate = False
        self.__terminate_sent = False
        self.__terminate_delivered = False  # terminate flag queued for sending
        self.__abandoned = False  # all reconnection attempts failed

        # reliability settings
        self.__max_retries = 3  # synchronous patterns only
        self.__request_timeout = 4000  # 4 secs (also `send()` timeout for pattern 3)
        self.__receive_timeout = None  # patterns 2 & 3 receiver only
        self.__send_hwm = 4  # pattern 3 sender queue length

        # Handle user-defined options dictionary values
        # reformat dictionary
        options = {str(k).strip(): v for k, v in options.items()}

        # loop over dictionary key & values and assign to global variables if valid
        for key, value in options.items():
            # handle multi-server mode
            if key == "multiserver_mode" and isinstance(value, bool):
                if value and pattern == 0:
                    logger.critical("Multi-Server Mode is disabled!")
                    raise ValueError(
                        "[NetGear:ERROR] :: `{}` pattern is not valid when Multi-Server Mode is enabled. Kindly refer Docs for more Information.".format(
                            pattern
                        )
                    )
                self.__multiserver_mode = value

            # handle multi-client mode
            elif key == "multiclient_mode" and isinstance(value, bool):
                if value and pattern == 0:
                    logger.critical("Multi-Client Mode is disabled!")
                    raise ValueError(
                        "[NetGear:ERROR] :: `{}` pattern is not valid when Multi-Client Mode is enabled. Kindly refer Docs for more Information.".format(
                            pattern
                        )
                    )
                self.__multiclient_mode = value

            # handle bidirectional mode
            elif key == "bidirectional_mode" and isinstance(value, bool):
                if value and pattern >= 2:
                    logger.warning("Bidirectional data transmission is disabled!")
                    raise ValueError(
                        "[NetGear:ERROR] :: `{}` pattern is not valid when Bidirectional Mode is enabled. Kindly refer Docs for more Information!".format(
                            pattern
                        )
                    )
                self.__bi_mode = value

            # handle secure mode
            elif (
                key == "secure_mode"
                and isinstance(value, int)
                and (value in valid_security_mech)
            ):
                self.__secure_mode = value
            elif key == "custom_cert_location" and isinstance(value, str):
                # verify custom auth certificates path for secure mode
                custom_cert_location = os.path.abspath(value)
                assert os.path.isdir(
                    custom_cert_location
                ), "[NetGear:ERROR] :: `custom_cert_location` value must be the path to a valid directory!"
                assert check_WriteAccess(
                    custom_cert_location,
                    is_windows=(os.name == "nt"),
                    logging=self.__logging,
                ), "[NetGear:ERROR] :: Permission Denied!, cannot write ZMQ authentication certificates to '{}' directory!".format(
                    value
                )
            elif key == "overwrite_cert" and isinstance(value, bool):
                # enable/disable auth certificate overwriting in secure mode
                overwrite_cert = value
            elif key == "allowed_ips" and isinstance(value, (list, tuple)):
                # optional IP whitelist for Secure Mode (receiver only)
                allowed_ips = [str(ip) for ip in value]

            # handle ssh-tunneling mode
            elif key == "ssh_tunnel_mode" and isinstance(value, str):
                self.__ssh_tunnel_mode = value.strip()
            elif key == "ssh_tunnel_pwd" and isinstance(value, str):
                self.__ssh_tunnel_pwd = value
            elif key == "ssh_tunnel_keyfile" and isinstance(value, str):
                # user's SSH private key (`ssh -i`)
                self.__ssh_tunnel_keyfile = value if os.path.isfile(value) else None
                if self.__ssh_tunnel_keyfile is None:
                    logger.warning(
                        "Discarded invalid or non-existential SSH Tunnel Key-file at {}!".format(
                            value
                        )
                    )

            # handle jpeg compression
            elif key == "jpeg_compression" and simplejpeg is not None:
                if isinstance(value, bool):
                    self.__jpeg_compression = value
                elif (
                    isinstance(value, str) and value.strip().upper() in _JPEG_CHANNELS
                ):
                    # set encoding colorspace and enable frame-compression
                    self.__jpeg_compression_colorspace = value.strip().upper()
                    self.__jpeg_compression = True
                else:
                    logger.warning(
                        "Skipped invalid `jpeg_compression` value: {}!".format(value)
                    )
            elif key == "jpeg_compression_quality" and isinstance(value, (int, float)):
                # set valid jpeg quality
                if value >= 10 and value <= 100:
                    self.__jpeg_compression_quality = int(value)
                else:
                    logger.warning("Skipped invalid `jpeg_compression_quality` value!")
            elif key == "jpeg_compression_fastdct" and isinstance(value, bool):
                self.__jpeg_compression_fastdct = value
            elif key == "jpeg_compression_fastupsample" and isinstance(value, bool):
                self.__jpeg_compression_fastupsample = value
            elif key == "jpeg_compression_colorsubsampling":
                if str(value).strip() in _JPEG_SUBSAMPLINGS:
                    self.__jpeg_compression_colorsubsampling = str(value).strip()
                else:
                    logger.warning(
                        "Skipped invalid `jpeg_compression_colorsubsampling` value!"
                    )

            # assign maximum retries in synchronous patterns
            elif key == "max_retries" and isinstance(value, int) and pattern < 2:
                if value >= 1:
                    self.__max_retries = value
                else:
                    logger.warning("Invalid `max_retries` value skipped!")

            # assign request timeout in synchronous patterns, and send timeout in pattern 3
            elif (
                key == "request_timeout"
                and isinstance(value, (int, float))
                and pattern in (0, 1, 3)
            ):
                if value >= 4:
                    self.__request_timeout = int(value * 1000)  # to milliseconds
                else:
                    logger.warning("Invalid `request_timeout` value skipped!")

            # assign receiver timeout for asynchronous patterns
            elif (
                key in ["receive_timeout", "subscriber_timeout"]
                and isinstance(value, (int, float))
                and pattern >= 2
            ):
                if value > 0:
                    self.__receive_timeout = int(value * 1000)  # to milliseconds
                else:
                    logger.warning("Invalid `{}` value skipped!".format(key))

            # assign sender queue length for pattern 3
            elif key == "send_hwm" and isinstance(value, int) and pattern == 3:
                if value >= 1:
                    self.__send_hwm = value
                else:
                    logger.warning("Invalid `send_hwm` value skipped!")

            # handle ZMQ flags
            elif key == "flag" and isinstance(value, int):
                self.__msg_flag = value
                self.__msg_flag and logger.warning(
                    "The flag optional value is set to `1` (NOBLOCK) for this run. `send()` may raise `zmq.Again`, and NetGear might not terminate gracefully."
                )
            elif key == "copy" and isinstance(value, bool):
                self.__msg_copy = value
            elif key == "track" and isinstance(value, bool):
                self.__msg_track = value
                self.__msg_copy and self.__msg_track and logger.info(
                    "The `track` optional value will be ignored for this run because `copy=True` is also defined."
                )

        # Handle ssh tunneling if enabled
        if self.__ssh_tunnel_mode is not None:
            if receive_mode:
                # SSH Tunnel Mode only available for server mode
                logger.error("SSH Tunneling cannot be enabled for Client-end!")
                self.__ssh_tunnel_mode = None
            elif protocol != "tcp":
                raise ValueError(
                    "[NetGear:ERROR] :: SSH Tunneling Mode only supports `tcp` protocol!"
                )
            else:
                # check if SSH server is reachable
                ssh_address, _, ssh_port = self.__ssh_tunnel_mode.partition(":")
                ssh_ip = ssh_address.rpartition("@")[2]
                assert check_open_port(
                    ssh_ip, port=int(ssh_port or 22)
                ), "[NetGear:ERROR] :: Host `{}` is not available for SSH Tunneling at port-{}!".format(
                    ssh_address, ssh_port or 22
                )

        # Handle multiple exclusive modes if enabled
        if self.__multiclient_mode and self.__multiserver_mode:
            raise ValueError(
                "[NetGear:ERROR] :: Multi-Client and Multi-Server Mode cannot be enabled simultaneously!"
            )
        elif (self.__multiserver_mode or self.__multiclient_mode) and (
            self.__ssh_tunnel_mode
        ):
            raise ValueError(
                "[NetGear:ERROR] :: SSH Tunneling and {} Mode cannot be enabled simultaneously. Kindly refer docs!".format(
                    "Multi-Server" if self.__multiserver_mode else "Multi-Client"
                )
            )
        self.__bi_mode and self.__logging and logger.debug(
            "Bidirectional Data Transmission is enabled for this connection!"
        )
        self.__ssh_tunnel_mode and self.__logging and logger.debug(
            "SSH Tunneling is enabled for host:`{}`.".format(self.__ssh_tunnel_mode)
        )

        # initialize and assign receive mode to global variable
        self.__receive_mode = receive_mode
        self.__zmq_type = valid_messaging_patterns[pattern][
            1 if receive_mode else 0
        ]

        # validate ports for exclusive modes:
        # list of ports where the socket binds/connects to many peers, single port otherwise
        many_ports = (self.__multiserver_mode and receive_mode) or (
            self.__multiclient_mode and not receive_mode
        )
        if self.__multiserver_mode or self.__multiclient_mode:
            mode_name = "Multi-Server" if self.__multiserver_mode else "Multi-Client"
            if many_ports and not isinstance(port, (tuple, list)):
                raise ValueError(
                    "[NetGear:ERROR] :: Incorrect port value! Kindly provide a list/tuple of {} ports while {} mode is enabled. For more information refer VidGear docs.".format(
                        "Server" if self.__multiserver_mode else "Client", mode_name
                    )
                )
            if not many_ports and (port is None or isinstance(port, (tuple, list))):
                raise ValueError(
                    "[NetGear:ERROR] :: Kindly provide a unique & valid port value at {}-end. For more information refer VidGear docs.".format(
                        "Client" if receive_mode else "Server"
                    )
                )
            logger.debug("Enabling {} Mode at PORT(s): {}!".format(mode_name, port))
            # track unique ports of connected peers
            self.__port_buffer = []
            self.__port = port
        else:
            port = "5555" if port is None else port

        # build endpoint(s): the receiver binds, the sender connects
        suffix_port = self.__multiserver_mode or self.__multiclient_mode
        self.__endpoints = [
            zmq_endpoint(protocol, address, pt, bind=receive_mode, suffix_port=suffix_port)
            for pt in (port if many_ports else [port])
        ]

        # define ZMQ messaging context (per-instance, so `close()` can't
        # tear down sockets of other NetGear instances in this process)
        self.__msg_context = zmq.Context()
        self.__msg_context.linger = 0

        # Handle Secure mode
        if self.__secure_mode > 0:
            if not zmq.has("curve"):
                raise RuntimeError(
                    "[NetGear:ERROR] :: Secure Mode requires libzmq built with CURVE support!"
                )
            if receive_mode and overwrite_cert:
                overwrite_cert = False
                logger.warning(
                    "Overwriting ZMQ Authentication certificates is disabled for Client's end!"
                )
            else:
                overwrite_cert and self.__logging and logger.info(
                    "Overwriting ZMQ Authentication certificates over previous ones!"
                )
            self.__curve_keys, auth_publickeys_dir = load_curve_keys(
                custom_cert_location or os.path.join(expanduser("~"), ".vidgear"),
                receive_mode,
                overwrite=overwrite_cert,
                logging=logging,
            )
            if receive_mode:
                # the receiver binds and is the CURVE server: ZAP authentication only applies here
                self.__z_auth = ThreadAuthenticator(self.__msg_context)
                self.__z_auth.start()
                if allowed_ips:
                    # whitelist peer (sender) IPs
                    self.__z_auth.allow(*allowed_ips)
                self.__z_auth.configure_curve(
                    domain="*",
                    location=(
                        auth_publickeys_dir
                        if self.__secure_mode == 2
                        else auth.CURVE_ALLOW_ANY
                    ),
                )
            else:
                allowed_ips and logger.warning(
                    "`allowed_ips` is only used at the receiver's (Client) end!"
                )

        # create the socket
        try:
            self.__msg_socket = self.__make_socket()
        except Exception as e:
            logger.exception(str(e))
            self.__secure_mode and logger.critical(
                "Failed to activate Secure Mode: `{}` for this connection!".format(
                    valid_security_mech[self.__secure_mode]
                )
            )
            self.__z_auth is not None and self.__z_auth.stop()
            self.__msg_context.destroy(linger=0)
            raise RuntimeError(
                "[NetGear:ERROR] :: {} Mode failed to {} address: {} with pattern: {}! Kindly recheck all parameters.".format(
                    "Receive" if receive_mode else "Send",
                    "bind" if receive_mode else "connect",
                    self.__endpoints,
                    pattern,
                )
            ) from e

        # log progress
        if self.__logging:
            logger.debug(
                "Successfully {} to address: {} with pattern: {}.".format(
                    "bound" if receive_mode else "connected",
                    self.__endpoints,
                    pattern,
                )
            )
            if pattern < 2:
                logger.debug(
                    "Reliable transmission is enabled for this pattern with max-retries: {} and timeout: {} secs.".format(
                        self.__max_retries, self.__request_timeout / 1000
                    )
                )
            elif self.__receive_timeout and receive_mode:
                logger.debug(
                    "Timeout: {} secs is enabled for this system.".format(
                        self.__receive_timeout / 1000
                    )
                )
            self.__jpeg_compression and logger.debug(
                "JPEG Frame-Compression is activated for this connection with Colorspace:`{}`, Quality:`{}`%, Subsampling:`{}`, Fastdct:`{}`, and Fastupsample:`{}`.".format(
                    self.__jpeg_compression_colorspace,
                    self.__jpeg_compression_quality,
                    self.__jpeg_compression_colorsubsampling,
                    self.__jpeg_compression_fastdct,
                    self.__jpeg_compression_fastupsample,
                )
            )
            self.__secure_mode and logger.debug(
                "Enabled ZMQ Security Mechanism: `{}` for this connection.".format(
                    valid_security_mech[self.__secure_mode]
                )
            )
            logger.debug("Unique System ID is {}.".format(self.__id))

        # retry budget, reset after every successful exchange
        self.__retries_left = self.__max_retries

        if receive_mode:
            # threaded queue mode: the handler thread owns the socket from now on
            self.__queue = queue.Queue(maxsize=96)
            self.__thread = Thread(target=self.__recv_handler, name="NetGear", daemon=True)
            self.__thread.start()
            self.__logging and logger.debug("Receive Mode is now activated.")
        else:
            self.__logging and logger.debug(
                "Send Mode is successfully activated and ready to send data."
            )

    def __make_socket(self):
        """
        Creates the messaging socket with every socket option applied, and binds/connects it.
        Used by both initial setup and every Lazy-Pirate reconnect, so options (CURVE, HWM, ...) never get lost.
        """
        sock = self.__msg_context.socket(self.__zmq_type)
        try:
            if self.__pattern == 1 and not self.__receive_mode:
                sock.REQ_RELAXED = True
                sock.REQ_CORRELATE = True
            if self.__pattern == 2:
                sock.set_hwm(1)
                self.__receive_mode and sock.setsockopt(zmq.SUBSCRIBE, b"")
            if self.__pattern == 3 and not self.__receive_mode:
                # bounded queue + timeout, so `send()` raises instead of blocking forever,
                # and nothing queues for clients that aren't connected
                sock.sndhwm = self.__send_hwm
                sock.sndtimeo = self.__request_timeout
                sock.immediate = True
            if self.__curve_keys is not None:
                secret, public, server_public = self.__curve_keys
                sock.curve_secretkey = secret
                sock.curve_publickey = public
                if self.__receive_mode:
                    sock.curve_server = True
                else:
                    sock.curve_serverkey = server_public
            for endpoint in self.__endpoints:
                if self.__receive_mode:
                    sock.bind(endpoint)
                elif self.__ssh_tunnel_mode:
                    ssh.tunnel_connection(
                        sock,
                        endpoint,
                        self.__ssh_tunnel_mode,
                        keyfile=self.__ssh_tunnel_keyfile,
                        password=self.__ssh_tunnel_pwd,
                    )
                else:
                    sock.connect(endpoint)
        except Exception:
            sock.close(linger=0)
            raise
        return sock

    def __jpeg_encodable(self, frame: NDArray) -> bool:
        """
        Checks whether simplejpeg can encode this frame with the selected colorspace.
        """
        channels = frame.shape[2] if frame.ndim == 3 else (1 if frame.ndim == 2 else -1)
        return (
            frame.dtype == np.uint8
            and channels == _JPEG_CHANNELS[self.__jpeg_compression_colorspace]
        )

    def __encode(self, frame: NDArray) -> tuple[dict, Any]:
        """
        Encodes a frame for the wire.

        **Returns:** a tuple of header fields and the payload buffer.
        """
        if self.__jpeg_compression:
            if self.__jpeg_encodable(frame):
                if frame.ndim == 2:
                    # simplejpeg needs 3-D input (GRAY decodes to `(h, w, 1)`)
                    frame = frame[:, :, np.newaxis]
                # rows may be strided (simplejpeg >= 1.8.0), but columns and channels must be contiguous
                if frame.strides[1] != frame.shape[2] or (
                    frame.shape[2] > 1 and frame.strides[2] != 1
                ):
                    frame = np.ascontiguousarray(frame)
                payload = simplejpeg.encode_jpeg(
                    frame,
                    quality=self.__jpeg_compression_quality,
                    colorspace=self.__jpeg_compression_colorspace,
                    colorsubsampling=self.__jpeg_compression_colorsubsampling,
                    fastdct=self.__jpeg_compression_fastdct,
                )
                return {
                    "compression": {
                        "dct": self.__jpeg_compression_fastdct,
                        "ups": self.__jpeg_compression_fastupsample,
                        "colorspace": self.__jpeg_compression_colorspace,
                    },
                    "dtype": "",
                    "shape": "",
                }, payload
            if not self.__raw_fallback_warned:
                self.__raw_fallback_warned = True
                logger.warning(
                    "Frame of dtype `{}` and shape `{}` cannot be JPEG compressed with `{}` colorspace (needs uint8 with matching channels). Sending such frames uncompressed.".format(
                        frame.dtype, frame.shape, self.__jpeg_compression_colorspace
                    )
                )
        frame = np.ascontiguousarray(frame)
        return {
            "compression": False,
            "dtype": str(frame.dtype),
            "shape": frame.shape,
        }, frame

    def __decode(self, buffer: Any, compression: Any, dtype: Any, shape: Any) -> NDArray:
        """
        Decodes a frame received from a peer. Raises on invalid or oversized data.
        """
        if not compression:
            return np.frombuffer(buffer, dtype=dtype).reshape(shape)
        if simplejpeg is None:
            raise ValueError("`simplejpeg` is required to decode JPEG compressed frames!")
        # check dimensions before decoding, since the decoder allocates from the header
        height, width, _, _ = simplejpeg.decode_jpeg_header(buffer)
        if height * width > _JPEG_MAX_PIXELS:
            raise ValueError(
                "JPEG frame of {}x{} exceeds the {} pixels limit!".format(
                    width, height, _JPEG_MAX_PIXELS
                )
            )
        # decode with this end's own flags
        frame = simplejpeg.decode_jpeg(
            buffer,
            colorspace=compression["colorspace"],
            fastdct=self.__jpeg_compression_fastdct,
            fastupsample=self.__jpeg_compression_fastupsample,
        )
        if compression["colorspace"] == "GRAY":
            # GRAY decodes to `(h, w, 1)`
            frame = np.squeeze(frame, axis=2)
        return frame

    def __put(self, item: Any) -> None:
        """
        Puts an item in the receive queue, waiting while it is full unless NetGear is closing.
        """
        while not self.__terminate:
            try:
                self.__queue.put(item, timeout=_POLL_MS / 1000)
                return
            except queue.Full:
                continue

    def __recv_handler(self):
        """
        A threaded receiver handler, that keep iterating data from ZMQ socket to a internally monitored queue,
        until the thread is terminated, or socket disconnects. This thread owns the socket.
        """
        result = _END
        try:
            self.__recv_loop()
        except Exception as e:
            logger.exception(str(e))
            result = e
        finally:
            self.__msg_socket.close(linger=0)
            # signal end of stream (or error) after all queued frames
            self.__put(result)

    def __recv_loop(self):
        """
        Receives messages until terminated, the peer(s) signal termination, or the connection times out.
        """
        # silence timeout: reconnect (synchronous patterns) or exit (asynchronous patterns)
        timeout = (
            self.__request_timeout * 3 if self.__pattern < 2 else self.__receive_timeout
        )
        last_msg = time.monotonic()

        while not self.__terminate:
            if not self.__msg_socket.poll(_POLL_MS, zmq.POLLIN):
                if timeout and (time.monotonic() - last_msg) * 1000 >= timeout:
                    if self.__pattern >= 2:
                        logger.warning("Connection Timeout. Exiting!")
                        return
                    logger.warning("No response from Server(s), Reconnecting again...")
                    self.__msg_socket.close(linger=0)
                    self.__retries_left -= 1
                    if self.__retries_left <= 0:
                        logger.error(
                            "All Servers seems to be offline, Abandoning!"
                            if self.__multiserver_mode
                            else "Server seems to be offline, Abandoning!"
                        )
                        self.__abandoned = True
                        return
                    self.__msg_socket = self.__make_socket()
                    last_msg = time.monotonic()
                continue

            try:
                parts = self.__msg_socket.recv_multipart(
                    flags=zmq.NOBLOCK, copy=self.__msg_copy, track=self.__msg_track
                )
            except zmq.Again:
                # nothing to read yet
                continue
            last_msg = time.monotonic()
            self.__retries_left = self.__max_retries

            msg_json = json.loads(bytes(parts[0]))

            # check if terminate_flag` received
            if msg_json.get("terminate_flag"):
                if self.__pattern == 1:
                    # send back server the info about termination
                    self.__msg_socket.send_string(
                        "Termination signal successfully received at Client's end!"
                    )
                if self.__multiserver_mode:
                    if msg_json.get("port") in self.__port_buffer:
                        self.__port_buffer.remove(msg_json["port"])
                        self.__logging and logger.warning(
                            "Termination signal received from Server at port: {}!".format(
                                msg_json["port"]
                            )
                        )
                    # exit when termination signal received from all servers
                    if not self.__port_buffer:
                        self.__logging and logger.info(
                            "Termination signal received from all Servers!!!"
                        )
                        return
                else:
                    self.__logging and logger.info("Termination signal received from server!")
                    return
                continue

            # handle replies in synchronous patterns (before decoding, REP must answer every request)
            if self.__pattern < 2:
                if self.__bi_mode or self.__multiclient_mode:
                    self.__send_return_data()
                else:
                    self.__msg_socket.send_string(
                        "Data received on device: {} !".format(self.__id)
                    )
            elif self.__return_data is not None:
                logger.warning("`return_data` is disabled for this pattern!")

            # decode frame, one bad frame costs one frame
            try:
                frame = self.__decode(
                    parts[1],
                    msg_json["compression"],
                    msg_json["dtype"],
                    msg_json["shape"],
                )
            except Exception as e:
                logger.error("Dropped an invalid frame: {}".format(e))
                continue

            if self.__multiserver_mode:
                # save the unique port addresses
                if msg_json["port"] not in self.__port_buffer:
                    self.__port_buffer.append(msg_json["port"])
                self.__put((msg_json["port"], msg_json["message"], frame))
            elif self.__bi_mode:
                self.__put((msg_json["message"], frame))
            else:
                self.__put(frame)

    def __send_return_data(self):
        """
        Sends `return_data` (or an empty reply) back to the Server in Bidirectional/Multi-Client mode.
        """
        return_data = self.__return_data
        return_dict = {"port": self.__port} if self.__multiclient_mode else {}
        return_dict["return_type"] = type(return_data).__name__
        if isinstance(return_data, np.ndarray):
            # copy guards against the user mutating `return_data` meanwhile
            header, payload = self.__encode(np.copy(return_data))
            return_dict.update(
                {
                    "compression": header["compression"],
                    "array_dtype": header["dtype"],
                    "array_shape": header["shape"],
                    "data": None,
                }
            )
            self.__msg_socket.send_multipart(
                [json.dumps(return_dict).encode(), payload],
                flags=self.__msg_flag,
                copy=self.__msg_copy,
                track=self.__msg_track,
            )
        else:
            return_dict["data"] = return_data
            self.__msg_socket.send_json(return_dict, self.__msg_flag)

    def recv(self, return_data: Any = None) -> Any:
        """
        A Receiver end method, that extracts received frames synchronously from monitored queue, while maintaining a
        fixed-length frame buffer in the memory, and blocks the thread if the queue is full.

        Parameters:
            return_data (any): inputs JSON-serializable return data _(or a `numpy.ndarray` frame)_, for sending back to Server
                in Bidirectional/Multi-Client mode. It stays set until replaced, and is sent with the reply to the **next** received frame.

        **Returns:** A n-dimensional numpy array, `(message, frame)` tuple in Bidirectional Mode, `(port, message, frame)` tuple in
        Multi-Server Mode, or `None` once the stream has ended. Raises `RuntimeError` if the receive thread failed.
        """
        # check whether `receive mode` is activated
        if not (self.__receive_mode):
            # raise value error and exit
            self.__terminate = True
            raise ValueError(
                "[NetGear:ERROR] :: `recv()` function cannot be used while receive_mode is disabled. Kindly refer vidgear docs!"
            )

        # handle Bidirectional return data
        if (self.__bi_mode or self.__multiclient_mode) and return_data is not None:
            self.__return_data = return_data

        try:
            while True:
                try:
                    item = self.__queue.get(timeout=_POLL_MS / 1000)
                except queue.Empty:
                    if self.__terminate or not self.__thread.is_alive():
                        return None
                    continue
                if item is _END:
                    self.__terminate = True
                    return None
                if isinstance(item, BaseException):
                    self.__terminate = True
                    raise RuntimeError(
                        "[NetGear:ERROR] :: Receive thread failed: {}".format(item)
                    ) from item
                return item
        except KeyboardInterrupt:
            self.__terminate = True
            return None

    def send(self, frame: NDArray | None, message: Any = None) -> Any:
        """
        A Server end method, that sends the data and frames over the network to Client(s).

        Parameters:
            frame (numpy.ndarray): inputs numpy array(frame). `None` signals end of stream to Client(s).
            message (any): input for sending additional JSON-serializable data _(of any datatype except `numpy.ndarray`)_ to Client(s).

        **Returns:** Data from Client(s) in Bidirectional/Multi-Client modes, otherwise None-type.
        """
        # check whether `receive_mode` is disabled
        if self.__receive_mode:
            # raise value error and exit
            self.__terminate = True
            raise ValueError(
                "[NetGear:ERROR] :: `send()` function cannot be used while receive_mode is enabled. Kindly refer vidgear docs!"
            )

        # `None` frame means end of stream
        if frame is None:
            self.__send_terminate()
            return None

        if message is not None and isinstance(message, np.ndarray):
            logger.warning(
                "Skipped unsupported `message` of datatype: {}!".format(
                    type(message).__name__
                )
            )
            message = None

        header, payload = self.__encode(frame)
        # prepare the exclusive json dict
        msg_dict = {"port": self.__port} if self.__multiserver_mode else {}
        msg_dict.update(
            {
                "terminate_flag": False,
                "compression": header["compression"],
                "message": message,
                "pattern": str(self.__pattern),
                "dtype": header["dtype"],
                "shape": header["shape"],
            }
        )
        try:
            self.__msg_socket.send_multipart(
                [json.dumps(msg_dict).encode(), payload],
                flags=self.__msg_flag,
                copy=self.__msg_copy,
                track=self.__msg_track,
            )
        except zmq.Again as e:
            if self.__pattern == 3 and not self.__msg_flag:
                raise RuntimeError(
                    "[NetGear:ERROR] :: Client(s) seems to be offline, `send()` timed out!"
                ) from e
            raise

        # asynchronous patterns don't wait for confirmation
        if self.__pattern >= 2:
            return None

        if not self.__msg_socket.poll(self.__request_timeout, zmq.POLLIN):
            self.__reconnect()
            return None
        self.__retries_left = self.__max_retries

        # check if Bidirectional data transmission is enabled
        if not (self.__bi_mode or self.__multiclient_mode):
            recv_confirmation = self.__msg_socket.recv()
            self.__logging and logger.debug(recv_confirmation)
            return None

        parts = self.__msg_socket.recv_multipart(
            flags=self.__msg_flag, copy=self.__msg_copy, track=self.__msg_track
        )
        recv_json = json.loads(bytes(parts[0]))
        # save the unique port addresses
        if self.__multiclient_mode and recv_json["port"] not in self.__port_buffer:
            self.__port_buffer.append(recv_json["port"])
        if recv_json["return_type"] == "ndarray":
            recvd_data = self.__decode(
                parts[1],
                recv_json["compression"],
                recv_json["array_dtype"],
                recv_json["array_shape"],
            )
        else:
            recvd_data = recv_json["data"]
        return (recv_json["port"], recvd_data) if self.__multiclient_mode else recvd_data

    def __reconnect(self):
        """
        Lazy-Pirate: rebuilds the sender socket after a missed reply, raising when retries are exhausted.
        """
        logger.warning("No response from Client(s), Reconnecting again...")
        # Socket is confused. Close and remove it.
        self.__msg_socket.close(linger=0)
        self.__retries_left -= 1
        if self.__retries_left <= 0:
            logger.error("Client(s) failed to respond on repeated attempts.")
            self.__terminate = True
            self.__abandoned = True
            raise RuntimeError(
                "[NetGear:ERROR] :: Client(s) seems to be offline, Abandoning!"
            )
        self.__msg_socket = self.__make_socket()

    def __send_terminate(self):
        """
        Sends the termination flag to Client(s), best-effort and bounded in time.
        """
        if self.__terminate_sent or self.__abandoned:
            return
        self.__terminate_sent = True

        term_dict = {"terminate_flag": True}
        if self.__multiserver_mode:
            term_dict["port"] = self.__port
        # number of termination messages to send
        if not self.__multiclient_mode or self.__pattern == 2:
            count = 1  # single peer, or PUB broadcast
        elif self.__pattern == 3:
            count = len(self.__port)  # PUSH round-robins one to each client
        else:
            count = len(self.__port_buffer)  # REQ: only clients that ever replied
        try:
            for _ in range(count):
                # wait (bounded) for queue space, a dead peer must not block `close()`
                if not self.__msg_socket.poll(self.__request_timeout // 5, zmq.POLLOUT):
                    raise zmq.Again()
                self.__msg_socket.send_json(term_dict, zmq.NOBLOCK)
                self.__terminate_delivered = True
                # check for confirmation if available within 1/5 timeout
                if self.__pattern == 1 and self.__msg_socket.poll(
                    self.__request_timeout // 5, zmq.POLLIN
                ):
                    self.__msg_socket.recv()
        except zmq.ZMQError as e:
            self.__logging and logger.debug("Termination signal not delivered: {}".format(e))

    def close(self, kill: bool = False) -> None:
        """
        Safely terminates the threads, and NetGear resources.

        Parameters:
            kill (bool): don't wait for the receive thread to finish before releasing the ZMQ context _(receive mode only)_.
        """
        self.__logging and logger.debug(
            "Terminating various {} Processes.".format(
                "Receive Mode" if self.__receive_mode else "Send Mode"
            )
        )
        if self.__receive_mode:
            # signal the receive thread, which owns and closes the socket
            self.__terminate = True
            if self.__z_auth is not None:
                self.__logging and logger.debug("Terminating Authenticator Thread.")
                self.__z_auth.stop()
                self.__z_auth = None
            if self.__thread is not None:
                self.__thread.join(timeout=_POLL_MS / 1000 if kill else 5.0)
                self.__thread.is_alive() and logger.warning(
                    "Receive thread still running, releasing ZMQ context anyway!"
                )
                self.__thread = None
        else:
            # send termination flag to Client(s), unless already done
            self.__send_terminate()
            self.__terminate = True
            # give queued messages (terminate flag included) a bounded time to flush
            self.__msg_socket.close(
                linger=self.__request_timeout if self.__terminate_delivered else 0
            )
        # release ZMQ context
        self.__msg_context.destroy(linger=0)
        self.__logging and logger.debug("Terminated Successfully!")

__init__(address=None, port=None, protocol=None, pattern=0, receive_mode=False, logging=False, **options) ¶

This constructor method initializes the object state and attributes of the NetGear class.

Parameters:

Name Type Description Default
address str

sets the receiver's network address (or ipc path).

None
port str

sets the valid Network Port of the Server/Client.

None
protocol str

sets the valid messaging protocol between Server/Client.

None
pattern int

sets the supported messaging pattern(flow of communication) between Server/Client

0
receive_mode bool

select the Netgear's Mode of operation.

False
logging bool

enables/disables logging.

False
options dict

provides the flexibility to alter various NetGear internal properties.

{}
Source code in vidgear/gears/netgear.py
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
def __init__(
    self,
    address: str | None = None,
    port: str | None = None,
    protocol: str | None = None,
    pattern: int = 0,
    receive_mode: bool = False,
    logging: bool = False,
    **options: Any,
):
    """
    This constructor method initializes the object state and attributes of the NetGear class.

    Parameters:
        address (str): sets the receiver's network address (or `ipc` path).
        port (str): sets the valid Network Port of the Server/Client.
        protocol (str): sets the valid messaging protocol between Server/Client.
        pattern (int): sets the supported messaging pattern(flow of communication) between Server/Client
        receive_mode (bool): select the Netgear's Mode of operation.
        logging (bool): enables/disables logging.
        options (dict): provides the flexibility to alter various NetGear internal properties.
    """
    # enable logging if specified
    self.__logging = logging if isinstance(logging, bool) else False

    # print current version
    logcurr_vidgear_ver(logging=self.__logging)

    # raise error(s) for critical Class imports
    import_dependency_safe(
        "zmq" if zmq is None else "", min_version="4.0", pkg_name="pyzmq"
    )
    import_dependency_safe(
        "simplejpeg" if simplejpeg is None else "", error="log", min_version="1.9.0"
    )

    # define valid messaging patterns => `0`: PAIR, `1`:(REQ, REP), `2`:(PUB, SUB), `3`:(PUSH, PULL)
    valid_messaging_patterns = {
        0: (zmq.PAIR, zmq.PAIR),
        1: (zmq.REQ, zmq.REP),
        2: (zmq.PUB, zmq.SUB),
        3: (zmq.PUSH, zmq.PULL),
    }

    # Handle messaging pattern
    if not (isinstance(pattern, int) and pattern in valid_messaging_patterns):
        logger.warning(
            "Invalid pattern `{}`, Defaulting to `zmq.PAIR`! Kindly refer Docs for more Information.".format(
                pattern
            )
        )
        pattern = 0
    self.__pattern = pattern

    # Handle messaging protocol
    if protocol not in ["tcp", "ipc"]:
        protocol is not None and logger.warning(
            "Protocol `{}` is not supported. Defaulting to `tcp` protocol!".format(
                protocol
            )
        )
        protocol = "tcp"

    # Handle connection params
    self.__msg_flag = 0  # handles connection flags
    self.__msg_copy = False  # handles whether to copy data
    self.__msg_track = False  # handles whether to track packets

    # define Secure Mode
    self.__z_auth = None
    valid_security_mech = {0: "Grasslands", 1: "StoneHouse", 2: "IronHouse"}
    self.__secure_mode = 0  # handles ZMQ security layer status
    self.__curve_keys = None  # (secret, public, server_public) CURVE keys
    allowed_ips = None  # optional ZAP IP whitelist (receiver only)
    overwrite_cert = False  # checks if certificates overwriting allowed
    custom_cert_location = ""  # handles custom ZMQ certificates path

    # define SSH Tunneling Mode
    self.__ssh_tunnel_mode = None  # handles ssh_tunneling mode state
    self.__ssh_tunnel_pwd = None
    self.__ssh_tunnel_keyfile = None

    # define exclusive modes
    self.__multiserver_mode = False
    self.__multiclient_mode = False
    self.__bi_mode = False

    # define frame-compression handler (enabled by default if simplejpeg is installed)
    self.__jpeg_compression = simplejpeg is not None
    self.__jpeg_compression_quality = 90  # 90% quality
    self.__jpeg_compression_fastdct = True  # fastest DCT on by default
    self.__jpeg_compression_fastupsample = False  # fastupsample off by default
    self.__jpeg_compression_colorspace = "BGR"  # use BGR colorspace by default
    self.__jpeg_compression_colorsubsampling = "422"  # chroma subsampling
    self.__raw_fallback_warned = False  # warn once about uncompressible frames

    # define receiver return data handler
    self.__return_data = None

    # generate 8-digit random system id
    self.__id = "".join(
        secrets.choice(string.ascii_uppercase + string.digits) for i in range(8)
    )

    # define termination flags
    self.__terminate = False
    self.__terminate_sent = False
    self.__terminate_delivered = False  # terminate flag queued for sending
    self.__abandoned = False  # all reconnection attempts failed

    # reliability settings
    self.__max_retries = 3  # synchronous patterns only
    self.__request_timeout = 4000  # 4 secs (also `send()` timeout for pattern 3)
    self.__receive_timeout = None  # patterns 2 & 3 receiver only
    self.__send_hwm = 4  # pattern 3 sender queue length

    # Handle user-defined options dictionary values
    # reformat dictionary
    options = {str(k).strip(): v for k, v in options.items()}

    # loop over dictionary key & values and assign to global variables if valid
    for key, value in options.items():
        # handle multi-server mode
        if key == "multiserver_mode" and isinstance(value, bool):
            if value and pattern == 0:
                logger.critical("Multi-Server Mode is disabled!")
                raise ValueError(
                    "[NetGear:ERROR] :: `{}` pattern is not valid when Multi-Server Mode is enabled. Kindly refer Docs for more Information.".format(
                        pattern
                    )
                )
            self.__multiserver_mode = value

        # handle multi-client mode
        elif key == "multiclient_mode" and isinstance(value, bool):
            if value and pattern == 0:
                logger.critical("Multi-Client Mode is disabled!")
                raise ValueError(
                    "[NetGear:ERROR] :: `{}` pattern is not valid when Multi-Client Mode is enabled. Kindly refer Docs for more Information.".format(
                        pattern
                    )
                )
            self.__multiclient_mode = value

        # handle bidirectional mode
        elif key == "bidirectional_mode" and isinstance(value, bool):
            if value and pattern >= 2:
                logger.warning("Bidirectional data transmission is disabled!")
                raise ValueError(
                    "[NetGear:ERROR] :: `{}` pattern is not valid when Bidirectional Mode is enabled. Kindly refer Docs for more Information!".format(
                        pattern
                    )
                )
            self.__bi_mode = value

        # handle secure mode
        elif (
            key == "secure_mode"
            and isinstance(value, int)
            and (value in valid_security_mech)
        ):
            self.__secure_mode = value
        elif key == "custom_cert_location" and isinstance(value, str):
            # verify custom auth certificates path for secure mode
            custom_cert_location = os.path.abspath(value)
            assert os.path.isdir(
                custom_cert_location
            ), "[NetGear:ERROR] :: `custom_cert_location` value must be the path to a valid directory!"
            assert check_WriteAccess(
                custom_cert_location,
                is_windows=(os.name == "nt"),
                logging=self.__logging,
            ), "[NetGear:ERROR] :: Permission Denied!, cannot write ZMQ authentication certificates to '{}' directory!".format(
                value
            )
        elif key == "overwrite_cert" and isinstance(value, bool):
            # enable/disable auth certificate overwriting in secure mode
            overwrite_cert = value
        elif key == "allowed_ips" and isinstance(value, (list, tuple)):
            # optional IP whitelist for Secure Mode (receiver only)
            allowed_ips = [str(ip) for ip in value]

        # handle ssh-tunneling mode
        elif key == "ssh_tunnel_mode" and isinstance(value, str):
            self.__ssh_tunnel_mode = value.strip()
        elif key == "ssh_tunnel_pwd" and isinstance(value, str):
            self.__ssh_tunnel_pwd = value
        elif key == "ssh_tunnel_keyfile" and isinstance(value, str):
            # user's SSH private key (`ssh -i`)
            self.__ssh_tunnel_keyfile = value if os.path.isfile(value) else None
            if self.__ssh_tunnel_keyfile is None:
                logger.warning(
                    "Discarded invalid or non-existential SSH Tunnel Key-file at {}!".format(
                        value
                    )
                )

        # handle jpeg compression
        elif key == "jpeg_compression" and simplejpeg is not None:
            if isinstance(value, bool):
                self.__jpeg_compression = value
            elif (
                isinstance(value, str) and value.strip().upper() in _JPEG_CHANNELS
            ):
                # set encoding colorspace and enable frame-compression
                self.__jpeg_compression_colorspace = value.strip().upper()
                self.__jpeg_compression = True
            else:
                logger.warning(
                    "Skipped invalid `jpeg_compression` value: {}!".format(value)
                )
        elif key == "jpeg_compression_quality" and isinstance(value, (int, float)):
            # set valid jpeg quality
            if value >= 10 and value <= 100:
                self.__jpeg_compression_quality = int(value)
            else:
                logger.warning("Skipped invalid `jpeg_compression_quality` value!")
        elif key == "jpeg_compression_fastdct" and isinstance(value, bool):
            self.__jpeg_compression_fastdct = value
        elif key == "jpeg_compression_fastupsample" and isinstance(value, bool):
            self.__jpeg_compression_fastupsample = value
        elif key == "jpeg_compression_colorsubsampling":
            if str(value).strip() in _JPEG_SUBSAMPLINGS:
                self.__jpeg_compression_colorsubsampling = str(value).strip()
            else:
                logger.warning(
                    "Skipped invalid `jpeg_compression_colorsubsampling` value!"
                )

        # assign maximum retries in synchronous patterns
        elif key == "max_retries" and isinstance(value, int) and pattern < 2:
            if value >= 1:
                self.__max_retries = value
            else:
                logger.warning("Invalid `max_retries` value skipped!")

        # assign request timeout in synchronous patterns, and send timeout in pattern 3
        elif (
            key == "request_timeout"
            and isinstance(value, (int, float))
            and pattern in (0, 1, 3)
        ):
            if value >= 4:
                self.__request_timeout = int(value * 1000)  # to milliseconds
            else:
                logger.warning("Invalid `request_timeout` value skipped!")

        # assign receiver timeout for asynchronous patterns
        elif (
            key in ["receive_timeout", "subscriber_timeout"]
            and isinstance(value, (int, float))
            and pattern >= 2
        ):
            if value > 0:
                self.__receive_timeout = int(value * 1000)  # to milliseconds
            else:
                logger.warning("Invalid `{}` value skipped!".format(key))

        # assign sender queue length for pattern 3
        elif key == "send_hwm" and isinstance(value, int) and pattern == 3:
            if value >= 1:
                self.__send_hwm = value
            else:
                logger.warning("Invalid `send_hwm` value skipped!")

        # handle ZMQ flags
        elif key == "flag" and isinstance(value, int):
            self.__msg_flag = value
            self.__msg_flag and logger.warning(
                "The flag optional value is set to `1` (NOBLOCK) for this run. `send()` may raise `zmq.Again`, and NetGear might not terminate gracefully."
            )
        elif key == "copy" and isinstance(value, bool):
            self.__msg_copy = value
        elif key == "track" and isinstance(value, bool):
            self.__msg_track = value
            self.__msg_copy and self.__msg_track and logger.info(
                "The `track` optional value will be ignored for this run because `copy=True` is also defined."
            )

    # Handle ssh tunneling if enabled
    if self.__ssh_tunnel_mode is not None:
        if receive_mode:
            # SSH Tunnel Mode only available for server mode
            logger.error("SSH Tunneling cannot be enabled for Client-end!")
            self.__ssh_tunnel_mode = None
        elif protocol != "tcp":
            raise ValueError(
                "[NetGear:ERROR] :: SSH Tunneling Mode only supports `tcp` protocol!"
            )
        else:
            # check if SSH server is reachable
            ssh_address, _, ssh_port = self.__ssh_tunnel_mode.partition(":")
            ssh_ip = ssh_address.rpartition("@")[2]
            assert check_open_port(
                ssh_ip, port=int(ssh_port or 22)
            ), "[NetGear:ERROR] :: Host `{}` is not available for SSH Tunneling at port-{}!".format(
                ssh_address, ssh_port or 22
            )

    # Handle multiple exclusive modes if enabled
    if self.__multiclient_mode and self.__multiserver_mode:
        raise ValueError(
            "[NetGear:ERROR] :: Multi-Client and Multi-Server Mode cannot be enabled simultaneously!"
        )
    elif (self.__multiserver_mode or self.__multiclient_mode) and (
        self.__ssh_tunnel_mode
    ):
        raise ValueError(
            "[NetGear:ERROR] :: SSH Tunneling and {} Mode cannot be enabled simultaneously. Kindly refer docs!".format(
                "Multi-Server" if self.__multiserver_mode else "Multi-Client"
            )
        )
    self.__bi_mode and self.__logging and logger.debug(
        "Bidirectional Data Transmission is enabled for this connection!"
    )
    self.__ssh_tunnel_mode and self.__logging and logger.debug(
        "SSH Tunneling is enabled for host:`{}`.".format(self.__ssh_tunnel_mode)
    )

    # initialize and assign receive mode to global variable
    self.__receive_mode = receive_mode
    self.__zmq_type = valid_messaging_patterns[pattern][
        1 if receive_mode else 0
    ]

    # validate ports for exclusive modes:
    # list of ports where the socket binds/connects to many peers, single port otherwise
    many_ports = (self.__multiserver_mode and receive_mode) or (
        self.__multiclient_mode and not receive_mode
    )
    if self.__multiserver_mode or self.__multiclient_mode:
        mode_name = "Multi-Server" if self.__multiserver_mode else "Multi-Client"
        if many_ports and not isinstance(port, (tuple, list)):
            raise ValueError(
                "[NetGear:ERROR] :: Incorrect port value! Kindly provide a list/tuple of {} ports while {} mode is enabled. For more information refer VidGear docs.".format(
                    "Server" if self.__multiserver_mode else "Client", mode_name
                )
            )
        if not many_ports and (port is None or isinstance(port, (tuple, list))):
            raise ValueError(
                "[NetGear:ERROR] :: Kindly provide a unique & valid port value at {}-end. For more information refer VidGear docs.".format(
                    "Client" if receive_mode else "Server"
                )
            )
        logger.debug("Enabling {} Mode at PORT(s): {}!".format(mode_name, port))
        # track unique ports of connected peers
        self.__port_buffer = []
        self.__port = port
    else:
        port = "5555" if port is None else port

    # build endpoint(s): the receiver binds, the sender connects
    suffix_port = self.__multiserver_mode or self.__multiclient_mode
    self.__endpoints = [
        zmq_endpoint(protocol, address, pt, bind=receive_mode, suffix_port=suffix_port)
        for pt in (port if many_ports else [port])
    ]

    # define ZMQ messaging context (per-instance, so `close()` can't
    # tear down sockets of other NetGear instances in this process)
    self.__msg_context = zmq.Context()
    self.__msg_context.linger = 0

    # Handle Secure mode
    if self.__secure_mode > 0:
        if not zmq.has("curve"):
            raise RuntimeError(
                "[NetGear:ERROR] :: Secure Mode requires libzmq built with CURVE support!"
            )
        if receive_mode and overwrite_cert:
            overwrite_cert = False
            logger.warning(
                "Overwriting ZMQ Authentication certificates is disabled for Client's end!"
            )
        else:
            overwrite_cert and self.__logging and logger.info(
                "Overwriting ZMQ Authentication certificates over previous ones!"
            )
        self.__curve_keys, auth_publickeys_dir = load_curve_keys(
            custom_cert_location or os.path.join(expanduser("~"), ".vidgear"),
            receive_mode,
            overwrite=overwrite_cert,
            logging=logging,
        )
        if receive_mode:
            # the receiver binds and is the CURVE server: ZAP authentication only applies here
            self.__z_auth = ThreadAuthenticator(self.__msg_context)
            self.__z_auth.start()
            if allowed_ips:
                # whitelist peer (sender) IPs
                self.__z_auth.allow(*allowed_ips)
            self.__z_auth.configure_curve(
                domain="*",
                location=(
                    auth_publickeys_dir
                    if self.__secure_mode == 2
                    else auth.CURVE_ALLOW_ANY
                ),
            )
        else:
            allowed_ips and logger.warning(
                "`allowed_ips` is only used at the receiver's (Client) end!"
            )

    # create the socket
    try:
        self.__msg_socket = self.__make_socket()
    except Exception as e:
        logger.exception(str(e))
        self.__secure_mode and logger.critical(
            "Failed to activate Secure Mode: `{}` for this connection!".format(
                valid_security_mech[self.__secure_mode]
            )
        )
        self.__z_auth is not None and self.__z_auth.stop()
        self.__msg_context.destroy(linger=0)
        raise RuntimeError(
            "[NetGear:ERROR] :: {} Mode failed to {} address: {} with pattern: {}! Kindly recheck all parameters.".format(
                "Receive" if receive_mode else "Send",
                "bind" if receive_mode else "connect",
                self.__endpoints,
                pattern,
            )
        ) from e

    # log progress
    if self.__logging:
        logger.debug(
            "Successfully {} to address: {} with pattern: {}.".format(
                "bound" if receive_mode else "connected",
                self.__endpoints,
                pattern,
            )
        )
        if pattern < 2:
            logger.debug(
                "Reliable transmission is enabled for this pattern with max-retries: {} and timeout: {} secs.".format(
                    self.__max_retries, self.__request_timeout / 1000
                )
            )
        elif self.__receive_timeout and receive_mode:
            logger.debug(
                "Timeout: {} secs is enabled for this system.".format(
                    self.__receive_timeout / 1000
                )
            )
        self.__jpeg_compression and logger.debug(
            "JPEG Frame-Compression is activated for this connection with Colorspace:`{}`, Quality:`{}`%, Subsampling:`{}`, Fastdct:`{}`, and Fastupsample:`{}`.".format(
                self.__jpeg_compression_colorspace,
                self.__jpeg_compression_quality,
                self.__jpeg_compression_colorsubsampling,
                self.__jpeg_compression_fastdct,
                self.__jpeg_compression_fastupsample,
            )
        )
        self.__secure_mode and logger.debug(
            "Enabled ZMQ Security Mechanism: `{}` for this connection.".format(
                valid_security_mech[self.__secure_mode]
            )
        )
        logger.debug("Unique System ID is {}.".format(self.__id))

    # retry budget, reset after every successful exchange
    self.__retries_left = self.__max_retries

    if receive_mode:
        # threaded queue mode: the handler thread owns the socket from now on
        self.__queue = queue.Queue(maxsize=96)
        self.__thread = Thread(target=self.__recv_handler, name="NetGear", daemon=True)
        self.__thread.start()
        self.__logging and logger.debug("Receive Mode is now activated.")
    else:
        self.__logging and logger.debug(
            "Send Mode is successfully activated and ready to send data."
        )

close(kill=False) ¶

Safely terminates the threads, and NetGear resources.

Parameters:

Name Type Description Default
kill bool

don't wait for the receive thread to finish before releasing the ZMQ context (receive mode only).

False
Source code in vidgear/gears/netgear.py
def close(self, kill: bool = False) -> None:
    """
    Safely terminates the threads, and NetGear resources.

    Parameters:
        kill (bool): don't wait for the receive thread to finish before releasing the ZMQ context _(receive mode only)_.
    """
    self.__logging and logger.debug(
        "Terminating various {} Processes.".format(
            "Receive Mode" if self.__receive_mode else "Send Mode"
        )
    )
    if self.__receive_mode:
        # signal the receive thread, which owns and closes the socket
        self.__terminate = True
        if self.__z_auth is not None:
            self.__logging and logger.debug("Terminating Authenticator Thread.")
            self.__z_auth.stop()
            self.__z_auth = None
        if self.__thread is not None:
            self.__thread.join(timeout=_POLL_MS / 1000 if kill else 5.0)
            self.__thread.is_alive() and logger.warning(
                "Receive thread still running, releasing ZMQ context anyway!"
            )
            self.__thread = None
    else:
        # send termination flag to Client(s), unless already done
        self.__send_terminate()
        self.__terminate = True
        # give queued messages (terminate flag included) a bounded time to flush
        self.__msg_socket.close(
            linger=self.__request_timeout if self.__terminate_delivered else 0
        )
    # release ZMQ context
    self.__msg_context.destroy(linger=0)
    self.__logging and logger.debug("Terminated Successfully!")

recv(return_data=None) ¶

A Receiver end method, that extracts received frames synchronously from monitored queue, while maintaining a fixed-length frame buffer in the memory, and blocks the thread if the queue is full.

Parameters:

Name Type Description Default
return_data any

inputs JSON-serializable return data (or a numpy.ndarray frame), for sending back to Server in Bidirectional/Multi-Client mode. It stays set until replaced, and is sent with the reply to the next received frame.

None

Returns: A n-dimensional numpy array, (message, frame) tuple in Bidirectional Mode, (port, message, frame) tuple in Multi-Server Mode, or None once the stream has ended. Raises RuntimeError if the receive thread failed.

Source code in vidgear/gears/netgear.py
def recv(self, return_data: Any = None) -> Any:
    """
    A Receiver end method, that extracts received frames synchronously from monitored queue, while maintaining a
    fixed-length frame buffer in the memory, and blocks the thread if the queue is full.

    Parameters:
        return_data (any): inputs JSON-serializable return data _(or a `numpy.ndarray` frame)_, for sending back to Server
            in Bidirectional/Multi-Client mode. It stays set until replaced, and is sent with the reply to the **next** received frame.

    **Returns:** A n-dimensional numpy array, `(message, frame)` tuple in Bidirectional Mode, `(port, message, frame)` tuple in
    Multi-Server Mode, or `None` once the stream has ended. Raises `RuntimeError` if the receive thread failed.
    """
    # check whether `receive mode` is activated
    if not (self.__receive_mode):
        # raise value error and exit
        self.__terminate = True
        raise ValueError(
            "[NetGear:ERROR] :: `recv()` function cannot be used while receive_mode is disabled. Kindly refer vidgear docs!"
        )

    # handle Bidirectional return data
    if (self.__bi_mode or self.__multiclient_mode) and return_data is not None:
        self.__return_data = return_data

    try:
        while True:
            try:
                item = self.__queue.get(timeout=_POLL_MS / 1000)
            except queue.Empty:
                if self.__terminate or not self.__thread.is_alive():
                    return None
                continue
            if item is _END:
                self.__terminate = True
                return None
            if isinstance(item, BaseException):
                self.__terminate = True
                raise RuntimeError(
                    "[NetGear:ERROR] :: Receive thread failed: {}".format(item)
                ) from item
            return item
    except KeyboardInterrupt:
        self.__terminate = True
        return None

send(frame, message=None) ¶

A Server end method, that sends the data and frames over the network to Client(s).

Parameters:

Name Type Description Default
frame ndarray

inputs numpy array(frame). None signals end of stream to Client(s).

required
message any

input for sending additional JSON-serializable data (of any datatype except numpy.ndarray) to Client(s).

None

Returns: Data from Client(s) in Bidirectional/Multi-Client modes, otherwise None-type.

Source code in vidgear/gears/netgear.py
def send(self, frame: NDArray | None, message: Any = None) -> Any:
    """
    A Server end method, that sends the data and frames over the network to Client(s).

    Parameters:
        frame (numpy.ndarray): inputs numpy array(frame). `None` signals end of stream to Client(s).
        message (any): input for sending additional JSON-serializable data _(of any datatype except `numpy.ndarray`)_ to Client(s).

    **Returns:** Data from Client(s) in Bidirectional/Multi-Client modes, otherwise None-type.
    """
    # check whether `receive_mode` is disabled
    if self.__receive_mode:
        # raise value error and exit
        self.__terminate = True
        raise ValueError(
            "[NetGear:ERROR] :: `send()` function cannot be used while receive_mode is enabled. Kindly refer vidgear docs!"
        )

    # `None` frame means end of stream
    if frame is None:
        self.__send_terminate()
        return None

    if message is not None and isinstance(message, np.ndarray):
        logger.warning(
            "Skipped unsupported `message` of datatype: {}!".format(
                type(message).__name__
            )
        )
        message = None

    header, payload = self.__encode(frame)
    # prepare the exclusive json dict
    msg_dict = {"port": self.__port} if self.__multiserver_mode else {}
    msg_dict.update(
        {
            "terminate_flag": False,
            "compression": header["compression"],
            "message": message,
            "pattern": str(self.__pattern),
            "dtype": header["dtype"],
            "shape": header["shape"],
        }
    )
    try:
        self.__msg_socket.send_multipart(
            [json.dumps(msg_dict).encode(), payload],
            flags=self.__msg_flag,
            copy=self.__msg_copy,
            track=self.__msg_track,
        )
    except zmq.Again as e:
        if self.__pattern == 3 and not self.__msg_flag:
            raise RuntimeError(
                "[NetGear:ERROR] :: Client(s) seems to be offline, `send()` timed out!"
            ) from e
        raise

    # asynchronous patterns don't wait for confirmation
    if self.__pattern >= 2:
        return None

    if not self.__msg_socket.poll(self.__request_timeout, zmq.POLLIN):
        self.__reconnect()
        return None
    self.__retries_left = self.__max_retries

    # check if Bidirectional data transmission is enabled
    if not (self.__bi_mode or self.__multiclient_mode):
        recv_confirmation = self.__msg_socket.recv()
        self.__logging and logger.debug(recv_confirmation)
        return None

    parts = self.__msg_socket.recv_multipart(
        flags=self.__msg_flag, copy=self.__msg_copy, track=self.__msg_track
    )
    recv_json = json.loads(bytes(parts[0]))
    # save the unique port addresses
    if self.__multiclient_mode and recv_json["port"] not in self.__port_buffer:
        self.__port_buffer.append(recv_json["port"])
    if recv_json["return_type"] == "ndarray":
        recvd_data = self.__decode(
            parts[1],
            recv_json["compression"],
            recv_json["array_dtype"],
            recv_json["array_shape"],
        )
    else:
        recvd_data = recv_json["data"]
    return (recv_json["port"], recvd_data) if self.__multiclient_mode else recvd_data

Â