Пример #1
0
void UDPSocket::onError(const scy::Error& error) 
{        
    ErrorS(this) << "Error: " << error.message << endl;    
    //emitError(error);
    onSocketError(error);
    close(); // close on error
}
Пример #2
0
void PacketStream::setup()
{
    try {
        std::lock_guard<std::mutex> guard(_mutex);

        // Setup the processor chain
        PacketProcessor* lastProc = nullptr;
        PacketProcessor* thisProc = nullptr;
        for (auto& proc : _processors) {
            thisProc = reinterpret_cast<PacketProcessor*>(proc->ptr);
            if (lastProc) {
                lastProc->getEmitter() +=
                    slot(thisProc, &PacketProcessor::process);
            }
            lastProc = thisProc;
        }

        // The last processor will emit the packet to the application
        if (lastProc)
            lastProc->getEmitter() += slot(this, &PacketStream::emit);

        // Attach source emitters to the PacketStream::process method
        for (auto& source : _sources) {
            source->ptr->getEmitter() += slot(this, &PacketStream::process);
        }
    } catch (std::exception& exc) {
        ErrorS(this) << "Cannot start stream: " << exc.what() << endl;
        setState(this, PacketStreamState::Error); //, exc.what()
        throw exc;
    }
}
Пример #3
0
int ConnectionAdapter::send(const char* data, std::size_t len, int flags)
{
    TraceS(this) << "Send: " << len << endl;

    try {
        // Send headers on initial send
        if (_connection.shouldSendHeader()) {
            int res = _connection.sendHeader();

            // The initial packet may be empty to push the headers through
            if (len == 0)
                return res;
        }

        // Other packets should not be empty
        assert(len > 0);

        // Send body / chunk
        return SocketAdapter::send(data, len, flags);
    }
    catch (std::exception& exc) {
        ErrorS(this) << "Send error: " << exc.what() << endl;

        // Swallow the exception, the socket error will
        // cause the connection to close on next iteration.
    }

    return -1;
}
Пример #4
0
void Roster::update(const json::Value& data, bool whiny)
{
    if (data.isObject() &&
        data.isMember("id") &&
        data.isMember("user") &&
        data.isMember("name") //&&
        //data.isMember("type")
        ) {
        TraceS(this) << "Updating: " << json::stringify(data, true) << endl;
        std::string id = data["id"].asString();
        Peer* peer = get(id, false);
        if (!peer) {
            peer = new Peer(data);
            add(id, peer);
        } else
            static_cast<json::Value&>(*peer) = data;
    }
    else if (data.isArray()) {
        for (auto it = data.begin(); it != data.end(); it++) {
            update(*it, whiny);
        }
    }
    else {
        std::string error("Bad presence data: " + json::stringify(data));
        ErrorS(this) << error << endl;
        if (whiny)
            throw std::runtime_error(error);
    }
}
bool DeviceManager::getAudioDevices(bool input, std::vector<Device>& devs)
{
    devs.clear();

#if defined(ANDROID)
    // Under Android, we don't access the device file directly.
    // Arbitrary use 0 for the mic and 1 for the output.
    // These ids are used in MediaEngine::SetSoundDevices(in, out);
    // The strings are for human consumption.
    if (input) {
        devs.push_back(Device("audioin", "audiorecord", 0));
    } else {
        devs.push_back(Device("audioout", "audiotrack", 1));
    }
    return true;
#elif defined(HAVE_RTAUDIO)

    // Since we are using RtAudio for audio capture it's best to
    // use RtAudio to enumerate devices to ensure indexes match.
    RtAudio audio;

    // Determine the number of devices available
    auto ndevices = audio.getDeviceCount();
    TraceS(this) << "Get audio devices: " << ndevices << endl;

    // Scan through devices for various capabilities
    RtAudio::DeviceInfo info;
    for (unsigned i = 0; i <= ndevices; i++) {
        try {
            info = audio.getDeviceInfo(i);    // may throw RtAudioError

            TraceS(this) << "Device:"
                << "\n\tName: " << info.name
                << "\n\tOutput Channels: " << info.outputChannels
                << "\n\tInput Channels: " << info.inputChannels
                << "\n\tDuplex Channels: " << info.duplexChannels
                << "\n\tDefault Output: " << info.isDefaultOutput
                << "\n\tDefault Input: " << info.isDefaultInput
                << "\n\tProbed: " << info.probed
                << endl;

            if (info.probed == true && (
                (input && info.inputChannels > 0) ||
                (!input && info.outputChannels > 0))) {

                TraceS(this) << "Adding device: " << info.name << endl;
                Device dev((input ? "audioin" : "audioout"), i, info.name, "",
                    (input ? info.isDefaultInput : info.isDefaultOutput));
                devs.push_back(dev);
            }
        }
        catch (RtAudioError& e) {
            ErrorS(this) << "Cannot probe audio device: " << e.getMessage() << endl;
        }
    }

    return filterDevices(devs, kFilteredAudioDevicesName);
#endif
}
Пример #6
0
void TCPSocket::onAcceptConnection(uv_stream_t*, int status) 
{        
    if (status == 0) {
        TraceS(this) << "On accept connection" << endl;
        acceptConnection();
    }
    else
        ErrorS(this) << "Accept connection failed" << endl;
}
Пример #7
0
int UDPSocket::send(const char* data, std::size_t len,
                    const Address& peerAddress, int /* flags */)
{
    TraceS(this) << "Send: " << len << ": " << peerAddress << endl;
    assert(Thread::currentID() == tid());
    // assert(len <= net::MAX_UDP_PACKET_SIZE);

    if (_peer.valid() && _peer != peerAddress) {
        ErrorS(this) << "Peer not authorized: " << peerAddress << endl;
        return -1;
    }

    if (!peerAddress.valid()) {
        ErrorS(this) << "Peer not valid: " << peerAddress << endl;
        return -1;
    }

    int r;
    auto sr = new internal::SendRequest;
    sr->buf = uv_buf_init((char*)data, len); // TODO: memcpy data?
    r = uv_udp_send(&sr->req, ptr<uv_udp_t>(), &sr->buf, 1, peerAddress.addr(),
                    UDPSocket::afterSend);

#if 0
    switch (peerAddress.af()) {
    case AF_INET:
        r = uv_udp_send(&sr->req, ptr<uv_udp_t>(), &sr->buf, 1, peerAddress.addr(), UDPSocket::afterSend);
        break;
    case AF_INET6:
        r = uv_udp_send6(&sr->req, ptr<uv_udp_t>(), &sr->buf, 1,
            *reinterpret_cast<const sockaddr_in6*>(peerAddress.addr()), UDPSocket::afterSend);
        break;
    default:
        throw std::runtime_error("Unexpected address family");
    }
#endif
    if (r) {
        ErrorS(this) << "Send failed: " << uv_err_name(r) << endl;
        setUVError("Invalid UDP socket", r);
    }

    // R is -1 on error, otherwise return len
    return r ? r : len;
}
Пример #8
0
void PacketStream::assertCanModify()
{
    if (stateEquals(PacketStreamState::Locked) ||
        stateEquals(PacketStreamState::Stopping) ||
        stateEquals(PacketStreamState::Active)) {
        ErrorS(this) << "Cannot modify an " << state() << " packet stream"
                     << endl;
        assert(0 && "cannot modify active packet stream");
        throw std::runtime_error("Cannot modify an active packet stream.");
    }
}
Пример #9
0
void PacketStream::handleException(std::exception& exc)
{
    ErrorS(this) << "Error: " << exc.what() << endl;

    // Set the stream Error state. No need for queueState
    // as we are currently inside the processor context.
    setState(this, PacketStreamState::Error); //, exc.what()

    // Capture the exception so it can be rethrown elsewhere.
    // The Error signal will be sent on next call to emit()
    _error = std::current_exception();
    Error.emit(*this, _error);

    //_syncError = true;
    if (_closeOnError) {
        TraceS(this) << "Close on error" << endl;
        this->close();
    }
}
Пример #10
0
bool TCPConnectionPair::doPeerConnect(const net::Address& peerAddr)
{
    try {
        assert(!transactionID.empty());
        peer = std::make_shared<net::TCPSocket>();
        peer->opaque = this;
        peer->Close += slot(this, &TCPConnectionPair::onConnectionClosed);

        // Start receiving early media
        peer->Recv += slot(this, &TCPConnectionPair::onPeerDataReceived);

        // Connect request specific events
        peer->Connect += slot(this, &TCPConnectionPair::onPeerConnectSuccess);
        peer->Error += slot(this, &TCPConnectionPair::onPeerConnectError);

        client->connect(peerAddr);
    } catch (std::exception& exc) {
        ErrorS(this) << "Peer connect error: " << exc.what() << endl;
        assert(0);
        return false;
    }
    return true;
}
Пример #11
0
void UDPSocket::onClose() 
{        
    ErrorS(this) << "On close" << endl;    
    //emitClose();
    onSocketClose();
}