Skip to content
This repository
tree: a4fca2c402
Fetching contributors…

Cannot retrieve contributors at this time

file 804 lines (672 sloc) 19.38 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 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 688 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 715 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 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
/*
SuperCollider real time audio synthesis system
Copyright (c) 2002 James McCartney. All rights reserved.
http://www.audiosynth.com

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "SC_Endian.h"
#include "SC_Lock.h"
#include "SC_HiddenWorld.h"
#include "SC_WorldOptions.h"
#include "sc_msg_iter.h"
#include <ctype.h>
#include <stdexcept>
#include <stdarg.h>

#include <sys/types.h>
#include "OSC_Packet.h"

#ifdef _WIN32
# include <winsock2.h>
typedef int socklen_t;
# define bzero( ptr, count ) memset( ptr, 0, count )
#else
#include <netinet/tcp.h>
#endif

#if defined(__linux__) || defined(__FreeBSD__)
#include <errno.h>
#include <unistd.h>
#endif

#if defined(SC_IPHONE) || defined(__APPLE__)
#include <errno.h>
#endif

#ifdef USE_RENDEZVOUS
#include "Rendezvous.h"
#endif

bool ProcessOSCPacket(World *inWorld, OSC_Packet *inPacket);

namespace {

//////////////////////////////////////////////////////////////////////////////////////////////////////////

class SC_CmdPort
{
protected:
pthread_t mThread;
struct World *mWorld;

void Start();
virtual ReplyFunc GetReplyFunc()=0;
public:
SC_CmdPort(struct World *inWorld);
virtual ~SC_CmdPort() {}

virtual void* Run()=0;
};

//////////////////////////////////////////////////////////////////////////////////////////////////////////

class SC_ComPort : public SC_CmdPort
{
protected:
int mPortNum;
int mSocket;
struct sockaddr_in mBindSockAddr;

#ifdef USE_RENDEZVOUS
pthread_t mRendezvousThread;
#endif

public:
SC_ComPort(struct World *inWorld, int inPortNum);
virtual ~SC_ComPort();

int Socket() { return mSocket; }

int PortNum() const { return mPortNum; }
#ifdef USE_RENDEZVOUS
// default implementation does nothing (this is correct for
// SC_TcpConnectionPort). Subclasses may override.
virtual void PublishToRendezvous() { };
#endif
};

//////////////////////////////////////////////////////////////////////////////////////////////////////////

const size_t kMaxUDPSize = 65535;

class SC_UdpInPort : public SC_ComPort
{
protected:
struct sockaddr_in mReplySockAddr;
unsigned char mReadBuf[kMaxUDPSize];
virtual ReplyFunc GetReplyFunc();

public:
SC_UdpInPort(struct World *inWorld, int inPortNum);
~SC_UdpInPort();

int PortNum() const { return mPortNum; }

void* Run();
#ifdef USE_RENDEZVOUS
virtual void PublishToRendezvous();
#endif

};

//////////////////////////////////////////////////////////////////////////////////////////////////////////

class SC_TcpInPort : public SC_ComPort
{
SC_Semaphore mConnectionAvailable;
int mBacklog;

protected:
virtual ReplyFunc GetReplyFunc();

public:
SC_TcpInPort(struct World *inWorld, int inPortNum, int inMaxConnections, int inBacklog);

virtual void* Run();

void ConnectionTerminated();
#ifdef USE_RENDEZVOUS
virtual void PublishToRendezvous();
#endif
};

//////////////////////////////////////////////////////////////////////////////////////////////////////////

class SC_TcpConnectionPort : public SC_ComPort
{
SC_TcpInPort *mParent;
unsigned char mReadBuf[kMaxUDPSize];

protected:
virtual ReplyFunc GetReplyFunc();

public:
SC_TcpConnectionPort(struct World *inWorld, SC_TcpInPort *inParent, int inSocket);
virtual ~SC_TcpConnectionPort();

virtual void* Run();
};



int recvall(int socket, void *msg, size_t len);
int sendallto(int socket, const void *msg, size_t len, struct sockaddr *toaddr, int addrlen);
int sendall(int socket, const void *msg, size_t len);

static void dumpOSCmsg(int inSize, char* inData)
{
int size;
const char *data;

if (inData[0]) {
const char *addr = inData;
if (strcmp(addr, "/status") == 0) // skip /status messages
return;

data = OSCstrskip(inData);
size = inSize - (data - inData);
scprintf("[ \"%s\",", addr);
} else {
scprintf("[ %d,", OSCint(inData));
data = inData + 4;
size = inSize - 4;
}

sc_msg_iter msg(size, data);

while (msg.remain())
{
char c = msg.nextTag('i');
switch(c)
{
case 'i' :
scprintf(" %d", msg.geti());
break;
case 'f' :
scprintf(" %g", msg.getf());
break;
case 'd' :
scprintf(" %g", msg.getd());
break;
case 's' :
scprintf(" \"%s\"", msg.gets());
break;
case 'b' :
scprintf(" DATA[%d]", msg.getbsize());
msg.skipb();
break;
case '[' :
scprintf("[");
msg.count++;
break;
case ']' :
scprintf("]");
msg.count++;
break;
default :
scprintf(" !unknown tag '%c' 0x%02x !", isprint(c)?c:'?', (unsigned char)c & 255);
goto leave;
}
if (msg.remain() && (c!= '[')) scprintf(",");
}
leave:
scprintf(" ]");
}

void hexdump(int size, char* data)
{
char ascii[20];
int padsize = (size + 15) & -16;
scprintf("size %d\n", size);
for (int i=0; i<padsize; ++i)
{
if ((i&15)==0)
{
scprintf("%4d ", i);
}
if (i >= size)
{
scprintf(" ");
ascii[i&15] = 0;
}
else
{
scprintf("%02x ", (unsigned char)data[i] & 255);

if (isprint(data[i])) ascii[i&15] = data[i];
else ascii[i&15] = '.';
}
if ((i&15)==15)
{
ascii[16] = 0;
scprintf(" |%s|\n", ascii);
}
else if ((i&3)==3)
{
scprintf(" ");
}
}
scprintf("\n");
}

static void dumpOSC(int mode, int size, char* inData)
{
if (mode & 1)
{
if (strcmp(inData, "#bundle") == 0)
{
char* data = inData + 8;
scprintf("[ \"#bundle\", %lld, ", OSCtime(data));
data += 8;
char* dataEnd = inData + size;
while (data < dataEnd) {
int32 msgSize = OSCint(data);
data += sizeof(int32);
scprintf("\n ");
dumpOSCmsg(msgSize, data);
data += msgSize;
if (data < dataEnd) scprintf(",");
}
scprintf("\n]\n");
}
else
{
dumpOSCmsg(size, inData);
scprintf("\n");
}
}

if (mode & 2) hexdump(size, inData);
}

}

SC_DLLEXPORT_C bool World_SendPacketWithContext(World *inWorld, int inSize, char *inData, ReplyFunc inFunc, void *inContext)
{
bool result = false;
if (inSize > 0) {
if (inWorld->mDumpOSC) dumpOSC(inWorld->mDumpOSC, inSize, inData);

OSC_Packet* packet = (OSC_Packet*)malloc(sizeof(OSC_Packet));
char *data = (char*)malloc(inSize);
packet->mReplyAddr.mSockAddr.sin_addr.s_addr = 0;
packet->mReplyAddr.mSockAddr.sin_port = 0;
packet->mReplyAddr.mReplyFunc = inFunc;
packet->mReplyAddr.mReplyData = inContext;
packet->mSize = inSize;
packet->mData = data;
packet->mReplyAddr.mSocket = 0;
memcpy(data, inData, inSize);

result = ProcessOSCPacket(inWorld, packet);
}
return result;
}

SC_DLLEXPORT_C bool World_SendPacket(World *inWorld, int inSize, char *inData, ReplyFunc inFunc)
{
return World_SendPacketWithContext(inWorld, inSize, inData, inFunc, 0);
}

SC_DLLEXPORT_C int World_OpenUDP(struct World *inWorld, int inPort)
{
try {
new SC_UdpInPort(inWorld, inPort);
return true;
} catch (std::exception& exc) {
scprintf("Exception in World_OpenUDP: %s\n", exc.what());
} catch (...) {
}
return false;
}

SC_DLLEXPORT_C int World_OpenTCP(struct World *inWorld, int inPort, int inMaxConnections, int inBacklog)
{
try {
new SC_TcpInPort(inWorld, inPort, inMaxConnections, inBacklog);
return true;
} catch (std::exception& exc) {
scprintf("Exception in World_OpenTCP: %s\n", exc.what());
} catch (...) {
}
return false;
}

void set_real_time_priority(pthread_t thread)
{
int policy;
struct sched_param param;

pthread_getschedparam (thread, &policy, &param);
#ifdef __linux__
policy = SCHED_FIFO;
const char* env = getenv("SC_SCHED_PRIO");
// jack uses a priority of 10 in realtime mode, so this is a good default
const int defprio = 5;
const int minprio = sched_get_priority_min(policy);
const int maxprio = sched_get_priority_max(policy);
const int prio = env ? atoi(env) : defprio;
param.sched_priority = sc_clip(prio, minprio, maxprio);
#else
policy = SCHED_RR; // round-robin, AKA real-time scheduling
param.sched_priority = 63; // you'll have to play with this to see what it does
#endif
pthread_setschedparam (thread, policy, &param);
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace {

SC_CmdPort::SC_CmdPort(struct World *inWorld) : mWorld(inWorld)
{
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////

SC_ComPort::SC_ComPort(struct World *inWorld, int inPortNum)
: SC_CmdPort(inWorld), mPortNum(inPortNum), mSocket(-1)
{
}

SC_ComPort::~SC_ComPort()
{
#ifdef _WIN32
    if (mSocket != -1) closesocket(mSocket);
#else
    if (mSocket != -1) close(mSocket);
#endif
}

void* com_thread_func(void* arg);
void* com_thread_func(void* arg)
{
    SC_CmdPort *thread = (SC_CmdPort*)arg;
    void* result = thread->Run();
    return result;
}

void SC_CmdPort::Start()
{
    pthread_create (&mThread, NULL, com_thread_func, (void*)this);
    set_real_time_priority(mThread);
}

#ifdef USE_RENDEZVOUS
void* rendezvous_thread_func(void* arg);
void* rendezvous_thread_func(void* arg)
{
SC_ComPort* port = reinterpret_cast<SC_ComPort*>(arg);
port->PublishToRendezvous();
return NULL;
}

void SC_UdpInPort::PublishToRendezvous()
{
PublishPortToRendezvous(kSCRendezvous_UDP, htons(mPortNum));
}

void SC_TcpInPort::PublishToRendezvous()
{
PublishPortToRendezvous(kSCRendezvous_TCP, htons(mPortNum));
}

#endif

//////////////////////////////////////////////////////////////////////////////////////////////////////////

SC_UdpInPort::SC_UdpInPort(struct World *inWorld, int inPortNum)
: SC_ComPort(inWorld, inPortNum)
{
if ((mSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
throw std::runtime_error("failed to create udp socket\n");
}

{
int bufsize = 65536;
#ifdef _WIN32
setsockopt(mSocket, SOL_SOCKET, SO_SNDBUF, (char*)&bufsize, sizeof(bufsize));
#else
setsockopt(mSocket, SOL_SOCKET, SO_SNDBUF, &bufsize, sizeof(bufsize));
#endif
}

//scprintf("@@@ sizeof(ReplyAddress) %d\n", sizeof(ReplyAddress));

bzero((char *)&mBindSockAddr, sizeof(mBindSockAddr));
mBindSockAddr.sin_family = AF_INET;
mBindSockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
mBindSockAddr.sin_port = htons(mPortNum);

if (bind(mSocket, (struct sockaddr *)&mBindSockAddr, sizeof(mBindSockAddr)) < 0) {
throw std::runtime_error("unable to bind udp socket\n");
}

Start();

#ifdef USE_RENDEZVOUS
if(inWorld->mRendezvous){
pthread_create(&mRendezvousThread,
NULL,
rendezvous_thread_func,
(void*)this);
}
#endif
}

SC_UdpInPort::~SC_UdpInPort()
{
#ifdef _WIN32
    if (mSocket != -1) closesocket(mSocket);
#else
if (mSocket != -1) close(mSocket);
#endif
}


}

//////////////////////////////////////////////////////////////////////////////////////////////////////////

static void DumpReplyAddress(ReplyAddress *inReplyAddress)
{
scprintf("mSockAddrLen %d\n", inReplyAddress->mSockAddrLen);
scprintf("mSocket %d\n", inReplyAddress->mSocket);
#ifdef __APPLE__
scprintf("mSockAddr.sin_len %d\n", inReplyAddress->mSockAddr.sin_len);
#endif
scprintf("mSockAddr.sin_family %d\n", inReplyAddress->mSockAddr.sin_family);
scprintf("mSockAddr.sin_port %d\n", inReplyAddress->mSockAddr.sin_port);
scprintf("mSockAddr.sin_addr.s_addr %d\n", inReplyAddress->mSockAddr.sin_addr.s_addr);
scprintf("mReplyFunc %p\n", (intptr_t)inReplyAddress->mReplyFunc);
}

/*
int32 GetHash(ReplyAddress *inReplyAddress)
{
int32 hash = Hash(inReplyAddress->mSockAddr.sin_addr.s_addr);

hash += (inReplyAddress->mSockAddr.sin_len << 24)
| (inReplyAddress->mSockAddr.sin_family << 16)
| inReplyAddress->mSockAddr.sin_port;

return Hash(hash);
}

ReplyAddress GetKey(ReplyAddress *inReplyAddress)
{
return *inReplyAddress
}
*/

namespace {

void udp_reply_func(struct ReplyAddress *addr, char* msg, int size)
{
int total = sendallto(addr->mSocket, msg, size, (sockaddr*)&addr->mSockAddr, addr->mSockAddrLen);
if (total < size) DumpReplyAddress(addr);
}

ReplyFunc SC_UdpInPort::GetReplyFunc()
{
return udp_reply_func;
}

void* SC_UdpInPort::Run()
{
OSC_Packet *packet = 0;
while (true) {
if (!packet) {
// preallocate packet before we need it.
packet = (OSC_Packet*)malloc(sizeof(OSC_Packet));
}

packet->mReplyAddr.mSockAddrLen = sizeof(sockaddr_in);
#ifdef _WIN32
int size = recvfrom(mSocket, (char *)mReadBuf, kMaxUDPSize , 0,
#else
int size = recvfrom(mSocket, mReadBuf, kMaxUDPSize , 0,
#endif
(struct sockaddr *) &packet->mReplyAddr.mSockAddr, (socklen_t*)&packet->mReplyAddr.mSockAddrLen);

if (size > 0) {
char *data = (char*)malloc(size);
memcpy(data, mReadBuf, size);
if (mWorld->mDumpOSC) dumpOSC(mWorld->mDumpOSC, size, data);

packet->mReplyAddr.mReplyFunc = udp_reply_func;
packet->mReplyAddr.mReplyData = 0;
packet->mSize = size;
packet->mData = data;
packet->mReplyAddr.mSocket = mSocket;

if (!ProcessOSCPacket(mWorld, packet))
{
scprintf("command FIFO full\n");
free(data);
free(packet);
}
packet = 0;
data = 0;
}
}
return 0;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////

SC_TcpInPort::SC_TcpInPort(struct World *inWorld, int inPortNum, int inMaxConnections, int inBacklog)
: SC_ComPort(inWorld, inPortNum), mConnectionAvailable(inMaxConnections),
        mBacklog(inBacklog)
{
    if((mSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        throw std::runtime_error("failed to create tcp socket\n");
    }

//const int on = 1;
//setsockopt( mSocket, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));

    bzero((char *)&mBindSockAddr, sizeof(mBindSockAddr));
    mBindSockAddr.sin_family = AF_INET;
    mBindSockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    mBindSockAddr.sin_port = htons(mPortNum);

    if(bind(mSocket, (struct sockaddr *)&mBindSockAddr, sizeof(mBindSockAddr)) < 0)
    {
        throw std::runtime_error("unable to bind tcp socket\n");
    }

    if(listen(mSocket, mBacklog) < 0)
    {
        throw std::runtime_error("unable to listen tcp socket\n");
    }

    Start();
#ifdef USE_RENDEZVOUS
if(inWorld->mRendezvous){
pthread_create(&mRendezvousThread,
NULL,
rendezvous_thread_func,
(void*)this);
}
#endif
}

void* SC_TcpInPort::Run()
{
    while (true)
    {
        mConnectionAvailable.Acquire();
        struct sockaddr_in address; /* Internet socket address stuct */
        socklen_t addressSize=sizeof(struct sockaddr_in);
        int socket = accept(mSocket,(struct sockaddr*)&address,&addressSize);
        if (socket < 0) {
         mConnectionAvailable.Release();
        } else {
         new SC_TcpConnectionPort(mWorld, this, socket);
        }
    }
    return 0;
}

void SC_TcpInPort::ConnectionTerminated()
{
        mConnectionAvailable.Release();
}

ReplyFunc SC_TcpInPort::GetReplyFunc()
{
return null_reply_func;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////

SC_TcpConnectionPort::SC_TcpConnectionPort(struct World *inWorld, SC_TcpInPort *inParent, int inSocket)
    : SC_ComPort(inWorld, 0), mParent(inParent)
{
    mSocket = inSocket;

#ifdef _WIN32
const char on = 1;
setsockopt( mSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&on, sizeof(on));
#else
const int on = 1;
setsockopt( mSocket, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
#endif

    Start();
}

SC_TcpConnectionPort::~SC_TcpConnectionPort()
{
#ifdef _WIN32
closesocket(mSocket);
#else
close(mSocket);
#endif
    mParent->ConnectionTerminated();
}

void tcp_reply_func(struct ReplyAddress *addr, char* msg, int size);
void tcp_reply_func(struct ReplyAddress *addr, char* msg, int size)
{
    uint32 u ;
    // Write size as 32bit unsigned network-order integer
u = size;
    u = htonl ( u ) ;
    sendall ( addr->mSocket , &u , 4 ) ;
    // Write message.
    sendall ( addr->mSocket , msg , size ) ;
}


ReplyFunc SC_TcpConnectionPort::GetReplyFunc()
{
return tcp_reply_func;
}

void* SC_TcpConnectionPort::Run()
{
const int kMaxPasswordLen = 32;
char buf[kMaxPasswordLen];
OSC_Packet *packet = 0;
// wait for login message
int32 size;
int32 msglen;

// first message must be the password. 4 tries.
bool validated = mWorld->hw->mPassword[0] == 0;
for (int i=0; !validated && i<4; ++i) {
size = recvall(mSocket, &msglen, sizeof(int32) );
if (size < 0) goto leave;

msglen = ntohl(msglen);
if (msglen > kMaxPasswordLen) break;

size = recvall(mSocket, buf, msglen);
if (size < 0) goto leave;

validated = strcmp(buf, mWorld->hw->mPassword) == 0;
#ifdef _WIN32
if (!validated) Sleep(i+1); // thwart cracking.
#else
if (!validated) sleep(i+1); // thwart cracking.
#endif
}

if (validated) {
while (true) {
if (!packet) {
packet = (OSC_Packet*)malloc(sizeof(OSC_Packet));
}
size = recvall(mSocket, &msglen, sizeof(int32));
if (size != sizeof(int32)) goto leave;

// sk: msglen is in network byte order
msglen = ntohl(msglen);

char *data = (char*)malloc(msglen);
size = recvall(mSocket, data, msglen);
if (size < msglen) goto leave;

if (mWorld->mDumpOSC) dumpOSC(mWorld->mDumpOSC, size, data);

packet->mReplyAddr.mReplyFunc = tcp_reply_func;
packet->mReplyAddr.mReplyData = 0;
packet->mSize = msglen;
packet->mData = data;
packet->mReplyAddr.mSocket = mSocket;
if (!ProcessOSCPacket(mWorld, packet)) {
scprintf("command FIFO full\n");
free(data);
free(packet);
}
packet = 0;
}
}
leave:
    delete this; // ohh this could cause a crash if a reply tries to access it..
    return 0;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////

int recvall(int socket, void *msg, size_t len)
{
int total = 0;
while (total < (int)len)
{
#ifdef _WIN32
int numbytes = recv(socket, (char*)msg, len - total, 0);
#else
int numbytes = recv(socket, msg, len - total, 0);
#endif
if (numbytes <= 0) return total;
total += numbytes;
msg = (void*)((char*)msg + numbytes);
}
return total;
}

int sendallto(int socket, const void *msg, size_t len, struct sockaddr *toaddr, int addrlen)
{
int total = 0;
while (total < (int)len)
{
#ifdef _WIN32
int numbytes = sendto(socket, (char*)msg, len - total, 0, toaddr, addrlen);
#else
int numbytes = sendto(socket, msg, len - total, 0, toaddr, addrlen);
#endif
if (numbytes < 0) {
scprintf("sendallto errno %d %s\n", errno, strerror(errno));
return total;
}
total += numbytes;
msg = (void*)((char*)msg + numbytes);
}
return total;
}

int sendall(int socket, const void *msg, size_t len)
{
int total = 0;
while (total < (int)len)
{
#ifdef _WIN32
int numbytes = send(socket, (const char*)msg, len - total, 0);
#else
int numbytes = send(socket, msg, len - total, 0);
#endif
if (numbytes < 0) return total;
total += numbytes;
msg = (void*)((char*)msg + numbytes);
}
return total;
}

}
Something went wrong with that request. Please try again.