예제 #1
0
bool yarp::os::impl::UdpCarrier::expectReplyToHeader(ConnectionState& proto) {
    // I am the sender
    int myPort = proto.getStreams().getLocalAddress().getPort();
    ConstString myName = proto.getStreams().getLocalAddress().getHost();
    ConstString altName = proto.getStreams().getRemoteAddress().getHost();

    int altPort = readYarpInt(proto);

    if (altPort==-1) {
        return false;
    }

    DgramTwoWayStream *stream = new DgramTwoWayStream();
    yAssert(stream!=YARP_NULLPTR);

    proto.takeStreams(YARP_NULLPTR); // free up port from tcp
    bool ok =
        stream->open(Contact(myName,myPort),Contact(altName,altPort));
    if (!ok) {
        delete stream;
        return false;
    }
    proto.takeStreams(stream);
    return true;
}
예제 #2
0
bool yarp::os::impl::UdpCarrier::respondToHeader(ConnectionState& proto) {
    // I am the receiver

    // issue: need a fresh port number...
    DgramTwoWayStream *stream = new DgramTwoWayStream();
    yAssert(stream!=YARP_NULLPTR);

    Contact remote = proto.getStreams().getRemoteAddress();
    bool ok = stream->open(remote);
    if (!ok) {
        delete stream;
        return false;
    }

    int myPort = stream->getLocalAddress().getPort();
    writeYarpInt(myPort,proto);
    proto.takeStreams(stream);

    return true;
}
예제 #3
0
void DgramTwoWayStream::interrupt() {
    bool act = false;
    mutex.wait();
    if ((!closed) && (!interrupting) && happy) {
        act = true;
        interrupting = true;
        closed = true;
    }
    mutex.post();

    if (act) {
        if (reader) {
            int ct = 3;
            while (happy && ct>0) {
                ct--;
                DgramTwoWayStream tmp;
                if (mgram) {
                    YARP_DEBUG(Logger::get(),
                               String("* mcast interrupt, interface ") +
                               restrictInterfaceIp.toString().c_str()
                               );
                    tmp.join(localAddress,true,
                             restrictInterfaceIp);
                } else {
                    YARP_DEBUG(Logger::get(),"* dgram interrupt");
                    tmp.open(Contact(localAddress.getHost(),0),
                             localAddress);
                }
                YARP_DEBUG(Logger::get(),
                           String("* interrupt state ") +
                           NetType::toString(interrupting) + " " +
                           NetType::toString(closed) + " " +
                           NetType::toString(happy) + " ");
                ManagedBytes empty(10);
                for (size_t i=0; i<empty.length(); i++) {
                    empty.get()[i] = 0;
                }

                // don't want this message getting into a valid packet
                tmp.pct = -1;

                tmp.write(empty.bytes());
                tmp.flush();
                tmp.close();
                if (happy) {
                    yarp::os::Time::delay(0.25);
                }
            }
            YARP_DEBUG(Logger::get(),"dgram interrupt done");
        }
        mutex.wait();
        interrupting = false;
        mutex.post();
    } else {
        // wait for interruption to be done
        if (interrupting) {
            while (interrupting) {
                YARP_DEBUG(Logger::get(),
                           "waiting for dgram interrupt to be finished...");
                yarp::os::Time::delay(0.1);
            }
        }
    }

}