oksunp / vvopensource

Automatically exported from code.google.com/p/vvopensource
0 stars 0 forks source link

TCP support? #26

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Is there any way to use TCP connections instead of UDP?  

Original issue reported on code.google.com by joand...@gmail.com on 12 Nov 2011 at 8:24

GoogleCodeExporter commented 8 years ago
In case somebody else is interested, I managed to send a OSCMessage via TCP 
using a NSOutputStream like this:

    // Init socket
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)a, 9000, &readStream, &writeStream);
    inputStream = (NSInputStream *)readStream;
    outputStream = (NSOutputStream *)writeStream;
    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [inputStream open];
    [outputStream open];

    ...

    // Send a packet: (according OSC spec, in TCP mode, first four bytes are packet size in bytes)
    OSCMessage *msg = [OSCMessage createWithAddress:@"/osc/test/message"];
    OSCPacket *p = [OSCPacket createWithContent:msg];
    uint32_t bufferLength = [p bufferLength];
    unsigned char *payload = [p payload];
    uint32_t finalSize = bufferLength + 4;
    unsigned char s[finalSize];
    unsigned char b[4];
    b[0] = bufferLength >> 24;
    b[1] = bufferLength >> 16;
    b[2] = bufferLength >> 8;
    b[3] = bufferLength;
    memcpy (s, b, 4);
    memcpy (s + 4, payload, bufferLength);
    [outputStream write:s maxLength:finalSize];

But I'm not sure if this is the best (faster) way of doing it, specially adding 
the first 4 bytes with the packet size. Any thoughts?

Thanks!

Original comment by joand...@gmail.com on 15 Nov 2011 at 11:29