Class: SRT::Socket

Inherits:
Object
  • Object
show all
Defined in:
ext/rbsrt/rbsrt.c

Instance Method Summary collapse

Constructor Details

#initializeObject



1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
# File 'ext/rbsrt/rbsrt.c', line 1293

VALUE rbsrt_socket_initialize(VALUE self)
{
    RBSRT_DEBUG_PRINT("initialize socket");

    RBSRT_SOCKET_UNWRAP(self, socket);

    socket->socket = srt_create_socket();

    int livemode = SRTT_LIVE;

    srt_setsockflag(socket->socket, SRTO_TRANSTYPE, &livemode, sizeof(livemode)); // set live mode

    return self;
}

Instance Method Details

#acceptObject



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
# File 'ext/rbsrt/rbsrt.c', line 689

VALUE rbsrt_socket_accept(VALUE self)
{
    RBSRT_DEBUG_PRINT("server accept");

    RBSRT_SOCKET_BASE_UNWRAP(self, socket);

    struct sockaddr_storage remote_address;
    int addr_size = sizeof(remote_address);

    SRTSOCKET accepted_socket = srt_accept(socket->socket, (struct sockaddr *)&remote_address, &addr_size);

    if (accepted_socket == SRT_ERROR)
    {
        rbsrt_raise_last_srt_error();

        return Qnil;
    }

    VALUE rbclient = rb_class_new_instance(0, NULL, mSRTSocketKlass);

    RBSRT_SOCKET_UNWRAP(rbclient, client);

    client->socket = accepted_socket;
    
    return rbclient;
}

#bind(address, port) ⇒ Object



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
# File 'ext/rbsrt/rbsrt.c', line 716

VALUE rbsrt_socket_bind(VALUE self, VALUE address, VALUE port)
{
    int status;
    struct sockaddr_in sa;
    // int livemode = SRTT_LIVE;

    Check_Type(address, T_STRING);
    Check_Type(port, T_STRING);

    RBSRT_SOCKET_BASE_UNWRAP(self, socket);

    // get address info
    // TODO: Handle domain names

    sa.sin_family = AF_INET;
    sa.sin_port = htons(atoi(StringValuePtr(port)));
    
    if (inet_pton(AF_INET, StringValuePtr(address), &sa.sin_addr) != 1)
    {
        rb_raise(rb_eArgError, "bad adddress: %s", strerror(errno));
        
        return Qfalse;
    }

    
    // bind socket

    status = srt_bind(socket->socket, (struct sockaddr *)&sa, sizeof(sa));
    
    if (status == SRT_ERROR)
    {
        rbsrt_raise_last_srt_error();
        
        return Qfalse;
    }

    return Qtrue;
}

#connect(host, port) ⇒ Object

MARK: Connecting



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
# File 'ext/rbsrt/rbsrt.c', line 613

VALUE rbsrt_socket_connect(VALUE self, VALUE host, VALUE port)
{
    Check_Type(host, T_STRING);
    Check_Type(port, T_STRING);

    RBSRT_DEBUG_PRINT("socket connect: host=%s, port=%s", StringValuePtr(host), StringValuePtr(port));

    RBSRT_SOCKET_BASE_UNWRAP(self, socket);

    VALUE retval = Qfalse;

    int status;
    struct addrinfo hints;
    struct addrinfo *servinfo;
    struct addrinfo *p;
    
    int ipv6only = 0;
    int ipv4only = 0;


    memset(&hints, 0, sizeof hints);
    
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_DGRAM;
    
    if ((status = getaddrinfo(StringValuePtr(host), StringValuePtr(port), &hints, &servinfo)) != 0)
    {
        rb_raise(rbsrt_eStandardError, "failed to get address info: %s", gai_strerror(status));
    }

    srt_clearlasterror();

    for(p = servinfo;p != NULL; p = p->ai_next) 
    {
        if ((p->ai_family == AF_INET && ipv6only) || (p->ai_family == AF_INET6 && ipv4only))
        {
            continue;
        }

        if (srt_connect(socket->socket, p->ai_addr, p->ai_addrlen) == SRT_ERROR)
        {
            RBSRT_DEBUG_PRINT("failed to connect socket: %s", srt_getlasterror_str());

            continue;
        }

        #if RBSRT_DEBUG
        char ipstr[INET6_ADDRSTRLEN];
        inet_ntop(
                p->ai_family, 
                (p->ai_family == AF_INET ? &(((struct sockaddr_in *)p->ai_addr)->sin_addr) : &(((struct sockaddr_in6 *)p->ai_addr)->sin6_addr)),
                ipstr,
                sizeof ipstr);

        RBSRT_DEBUG_PRINT("socket connected: ip=%s", ipstr);
        #endif

        // connected

        retval = Qtrue;

        break;
    }

    // complete

    freeaddrinfo(servinfo);

    if (retval != Qtrue)
    {
        rbsrt_raise_last_srt_error();
    }

    return retval;
}

#listen(nbacklog) ⇒ Object



755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
# File 'ext/rbsrt/rbsrt.c', line 755

VALUE rbsrt_socket_listen(VALUE self, VALUE nbacklog)
 {
    Check_Type(nbacklog, T_FIXNUM);

    RBSRT_SOCKET_BASE_UNWRAP(self, socket);
    
    int status = srt_listen(socket->socket, FIX2INT(nbacklog));
    
    if (status == SRT_ERROR)
    {
       rbsrt_raise_last_srt_error();
        
        return Qfalse;
    }

    return Qtrue;
}