Class: SRT::Server

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

Instance Method Summary collapse

Constructor Details

#initialize(address, port) ⇒ Object



1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
# File 'ext/rbsrt/rbsrt.c', line 1454

VALUE rbsrt_server_initialize(VALUE self, VALUE address, VALUE port)
{
    RBSRT_DEBUG_PRINT("server initialize");

    rbsrt_server_t *server;
    int status;
    struct sockaddr_in sa;
    int livemode = SRTT_LIVE;
    int no = 1;

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

    TypedData_Get_Struct(self, rbsrt_server_t, &rbsrt_server_rbtype, server);

    // Initialize struct

    atomic_init(&server->num_connections, 0);


    // Ruby API

    VALUE connections_by_socket = rb_hash_new();

    rb_hash_clear(connections_by_socket);
    
    rb_ivar_set(self, rb_intern("@connections_by_socket"), connections_by_socket);

    
    // create socket

    server->socket = srt_create_socket();

    if (server->socket == SRT_ERROR)
    {
        rb_raise(rb_eStandardError, "%s", srt_getlasterror_str());

        return Qfalse;
    }


    // get address info

    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;
    }
    

    // set up socket

    srt_setsockflag(server->socket, SRTO_TRANSTYPE, &livemode, sizeof(livemode)); // set live mode
    srt_setsockflag(server->socket, SRTO_RCVSYN, &no, sizeof(no)); // non-blocking mode
    srt_setsockflag(server->socket, SRTO_SNDSYN, &no, sizeof(no));
    

    // set up callback

    srt_listen_callback(server->socket, rbsrt_server_listen_callback, (void *)self);

    
    // bind server
    
    status = srt_bind(server->socket, (struct sockaddr *)&sa, sizeof(sa));
    
    if (status == SRT_ERROR)
    {
        rb_raise(rb_eStandardError, "%s", srt_getlasterror_str());
        
        return Qfalse;
    }
    
    
    // listen
    
    status = srt_listen(server->socket, 6); // TODO: Pass backlog size
    
    if (status == SRT_ERROR)
    {
        rb_raise(rb_eStandardError, "%s", srt_getlasterror_str());
        
        return Qfalse;
    }

    return self;
}

Instance Method Details

#closeObject



1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
# File 'ext/rbsrt/rbsrt.c', line 1546

VALUE rbsrt_server_close(VALUE self)
{
    RBSRT_DEBUG_PRINT("server close");

    RBSRT_SERVER_UNWRAP(self, server);

    if (srt_close(server->socket) == SRT_ERROR)
    {
        rbsrt_raise_last_srt_error();
    }

    server->flags &= ~ (RBSRT_SERVER_ACCEPTING | RBSRT_SERVER_BOUND | RBSRT_SERVER_LISTENING);
    server->flags |= RBSRT_SERVER_CLOSED;

    return Qnil;
}

#connection_countObject



1580
1581
1582
1583
1584
1585
# File 'ext/rbsrt/rbsrt.c', line 1580

VALUE rbsrt_server_connection_count(VALUE self)
{
    RBSRT_SERVER_UNWRAP(self, server);
    
    return SIZET2NUM(RBSRT_SERVER_NUM_CONNECTIONS(server));
}

#startObject



1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
# File 'ext/rbsrt/rbsrt.c', line 1587

VALUE rbsrt_server_start(VALUE self)
{
    RBSRT_DEBUG_PRINT("server start");

    rb_need_block();

    RBSRT_SERVER_UNWRAP(self, server);

    // rb_iv_set(self, rb_intern("acceptor_block"), rb_block_proc());

    server->acceptor_block = rb_block_proc();

    server->epollid = srt_epoll_create();

    int event_types = SRT_EPOLL_IN;

    srt_epoll_add_usock(server->epollid, server->socket, &event_types);

    SRT_EPOLL_EVENT events[1024];

    struct rbsrt_server_epoll_wait_args args = {
        .server = server,
        .timeout = 100,
        .num_events = 1024,
        .events = events
    };

    struct sockaddr_storage remote_address;
    int addr_size = sizeof(remote_address);
    int remote_fd;
    int running = 1;

    VALUE connections_by_socket = rb_ivar_get(self, rb_intern("@connections_by_socket"));

    while (running)
    {
        int num_events = (int)(uintptr_t)rb_thread_call_without_gvl(rbsrt_server_epoll_wait, &args, RUBY_UBF_IO, 0);

        if (num_events <= 0)
        {
            continue; // timeout
        }

        int read_buf_size = RBSRT_PAYLOAD_SIZE * 8;
        char read_buf[read_buf_size];
        int nbytes;

        // SRT_MSGCTRL msgctl;
        SRT_EPOLL_EVENT *event;
        SRTSOCKET sock;
        SRT_SOCKSTATUS status;
        rbsrt_connection_t *connection;

        VALUE rb_connection;

        int no = 0;
        int no_size = sizeof(no);
        int connection_epoll_events = SRT_EPOLL_OUT | SRT_EPOLL_IN | SRT_EPOLL_ERR;

        for (int i = 0; i < num_events; i++)
        {
            event = &args.events[i];
            sock = event->fd;
            status = srt_getsockstate(sock);
            
            switch (status)
            {
                // accept connections
                case SRTS_LISTENING:

                    remote_fd = srt_accept(sock, (struct sockaddr *)&remote_address, &addr_size);

                    srt_setsockflag(remote_fd, SRTO_RCVSYN, &no, no_size);
                    srt_setsockflag(remote_fd, SRTO_SNDSYN, &no, no_size);

                    connection = malloc(sizeof(rbsrt_connection_t));

                    memset(connection, 0, sizeof(rbsrt_connection_t));

                    rb_connection = TypedData_Make_Struct(mSRTConnectionKlass, rbsrt_connection_t, &rbsrt_connection_rbtype, connection);

                    connection->socket = remote_fd;

                    VALUE should_accept = rb_funcall_with_block(self, rb_intern("instance_exec"), 1, &rb_connection, server->acceptor_block);

                    if (RTEST(should_accept))
                    {
                        srt_epoll_add_usock(server->epollid, connection->socket, &connection_epoll_events);

                        atomic_fetch_add(&server->num_connections, 1);
                        
                        rb_hash_aset(connections_by_socket, INT2FIX(connection->socket), rb_connection);
                    }

                    else
                    {
                        atomic_fetch_sub(&server->num_connections, 1);
                        
                        srt_close(remote_fd);
                    }

                    break;

                case SRTS_CLOSED:
                case SRTS_NONEXIST:
                case SRTS_BROKEN:
                    RBSRT_DEBUG_PRINT("removing connection with socket: %d", sock);

                    srt_epoll_remove_usock(server->epollid, sock);

                    rb_connection = rb_hash_delete(connections_by_socket, INT2FIX(sock));

                    if (RTEST(rb_connection))
                    {
                        if (atomic_fetch_sub(&server->num_connections, 1) == 0)
                        {
                            DEBUG_ERROR_PRINT("removed to many connections");
                        }

                        RBSRT_DEBUG_PRINT("remove connection with socket %d, now %lu sockets", sock, RBSRT_SERVER_NUM_CONNECTIONS(server));

                        RBSRT_CONNECTION_UNWRAP(rb_connection, removed_connection);

                        if (removed_connection->at_close_block)
                        {
                            rb_funcall(removed_connection->at_close_block, rb_intern("call"), 0);

                            removed_connection->at_close_block = 0;
                        }

                        if (removed_connection->at_data_block)
                        {
                            removed_connection->at_data_block = 0;
                        }
                    }

                    break;

                case SRTS_CONNECTED:
                    if (event->events & SRT_EPOLL_IN)
                    {
                        RBSRT_DEBUG_PRINT("will read from socket %d (max bytes %d)", sock, read_buf_size);

                        nbytes = srt_recvmsg2(event->fd, read_buf, read_buf_size, NULL);

                        if (nbytes == SRT_ERROR)
                        {
                            RBSRT_DEBUG_PRINT("failed to read from socket %d: %s", sock, srt_getlasterror_str());

                            break;
                        }

                        if (nbytes > 0)
                        {
                            RBSRT_DEBUG_PRINT("received %d bytes from socket %d", nbytes, sock);

                            rb_connection = rb_hash_lookup(connections_by_socket, INT2FIX(sock));

                            if (RTEST(rb_connection))
                            {
                                RBSRT_DEBUG_PRINT("found connection for socket %d", sock);

                                TypedData_Get_Struct(rb_connection, rbsrt_connection_t, &rbsrt_connection_rbtype, connection);

                                if (connection->at_data_block)
                                {
                                    RBSRT_DEBUG_PRINT("found data handler for socket %d", sock);

                                    VALUE data = rb_str_new(read_buf, nbytes);

                                    rb_funcall(connection->at_data_block, rb_intern("call"), 1, data);
                                }
                            }
                        }
                    }

                    // TODO: Handle other events
                    break;

                default:
                    break;
            }
        }
    }
    
    return Qtrue;
}