void* SC_TcpConnectionPort::Run() { OSC_Packet *packet = 0; // wait for login message int32 size; int32 msglen; while (true) { if (!packet) { packet = (OSC_Packet*)malloc(sizeof(OSC_Packet)); } size = recvall(mSocket, &msglen, sizeof(int32)); if (size < 0) 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; packet->mReplyAddr.mReplyFunc = tcp_reply_func; packet->mSize = msglen; packet->mData = data; packet->mReplyAddr.mSocket = mSocket; ProcessOSCPacket(packet); packet = 0; } leave: delete this; // ohh this could cause a crash if a reply tries to access it.. return 0; }
void* SC_UdpInPort::Run() { char buf[kTextBufSize]; OSC_Packet *packet = 0; //printf("SC_UdpInPort::Run\n"); fflush(stdout); while (true) { if (!packet) { packet = (OSC_Packet*)malloc(sizeof(OSC_Packet)); } packet->mReplyAddr.mSockAddrLen = sizeof(sockaddr_in); int size = recvfrom(mSocket, buf, kTextBufSize , 0, (struct sockaddr *) &packet->mReplyAddr.mSockAddr, (socklen_t*)&packet->mReplyAddr.mSockAddrLen); if (size > 0) { //dumpOSC(3, size, buf); //fflush(stdout); char *data = (char*)malloc(size); packet->mReplyAddr.mReplyFunc = udp_reply_func; packet->mSize = size; packet->mData = data; packet->mReplyAddr.mSocket = mSocket; memcpy(data, buf, size); ProcessOSCPacket(packet); packet = 0; } } return 0; }
void* SC_UdpCustomInPort::Run() { OSC_Packet *packet = 0; const int fd = mSocket; const int max_fd = fd+1; mRunning.store(true); while (mRunning.load(boost::memory_order_consume)) { fd_set rfds; FD_ZERO( &rfds); FD_SET(fd, &rfds); struct timeval timeout; timeout.tv_sec = 0; timeout.tv_usec = 500000; int n = select(max_fd, &rfds, 0, 0, &timeout); if ((n > 0) && FD_ISSET(fd, &rfds)) { if (!packet) { packet = (OSC_Packet*)malloc(sizeof(OSC_Packet)); } packet->mReplyAddr.mSockAddrLen = sizeof(sockaddr_in); int size = recvfrom(mSocket, buf, kTextBufSize , 0, (struct sockaddr *) &packet->mReplyAddr.mSockAddr, (socklen_t*)&packet->mReplyAddr.mSockAddrLen); if (size > 0 && mRunning.load(boost::memory_order_consume)) { //dumpOSC(3, size, buf); //fflush(stdout); char *data = (char*)malloc(size); packet->mReplyAddr.mReplyFunc = udp_reply_func; packet->mSize = size; packet->mData = data; packet->mReplyAddr.mSocket = mSocket; memcpy(data, buf, size); ProcessOSCPacket(packet, mPortNum); packet = 0; } } } FreeOSCPacket(packet); // just in case return 0; }
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; }
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; }
CFDataRef SC_MachMessagePort::messagePortCallBack(CFMessagePortRef local, SInt32 msgid, CFDataRef data, void *info) { // CFShow(data); SC_MachMessagePort *port = (SC_MachMessagePort *) info; OSC_Packet* packet = (OSC_Packet *) malloc(sizeof(OSC_Packet)); bzero(&packet->mReplyAddr, sizeof(ReplyAddress)); packet->mReplyAddr.mReplyFunc = port->mReplyPort ? mach_reply_func : null_reply_func; packet->mReplyAddr.mReplyData = 0; packet->mReplyAddr.mSocket = (int)(int64)port->mReplyPort; packet->mSize = CFDataGetLength(data); packet->mData = (char*) memcpy(malloc(packet->mSize), CFDataGetBytePtr(data), packet->mSize); World *world = port->mWorld; if (world->mDumpOSC) dumpOSC(world->mDumpOSC, packet->mSize, packet->mData); if (!ProcessOSCPacket(world, packet)) { scprintf("command FIFO full\n"); free(packet->mData); free(packet); } // how can we send a reply? we could block here until the message is processed... return NULL; }
void* SC_TcpClientPort::Run() { OSC_Packet *packet = 0; int32 size; int32 msglen; int cmdfd = mCmdFifo[0]; int sockfd = mSocket; int nfds = sc_max(cmdfd, sockfd) + 1; bool cmdClose = false; pthread_detach(mThread); while (true) { fd_set rfds; FD_ZERO(&rfds); FD_SET(cmdfd, &rfds); FD_SET(sockfd, &rfds); if ((select(nfds, &rfds, 0, 0, 0) == -1) || (cmdClose = FD_ISSET(cmdfd, &rfds))) goto leave; if (!FD_ISSET(sockfd, &rfds)) continue; packet = (OSC_Packet*)malloc(sizeof(OSC_Packet)); if (!packet) goto leave; packet->mData = 0; size = recvall(sockfd, &msglen, sizeof(int32)); if (size < (int32)sizeof(int32)) goto leave; // msglen is in network byte order msglen = ntohl(msglen); packet->mData = (char*)malloc(msglen); if (!packet->mData) goto leave; size = recvall(sockfd, packet->mData, msglen); if (size < msglen) goto leave; memcpy(&packet->mReplyAddr.mSockAddr, &mReplySockAddr, sizeof(mReplySockAddr)); packet->mReplyAddr.mSockAddrLen = sizeof(mReplySockAddr); packet->mReplyAddr.mSocket = sockfd; packet->mReplyAddr.mReplyFunc = tcp_reply_func; packet->mSize = msglen; ProcessOSCPacket(packet); packet = 0; } leave: if (packet) { free(packet->mData); free(packet); } // Only call notify function when not closed explicitly if (!cmdClose && mClientNotifyFunc) { (*mClientNotifyFunc)(mClientData); } delete this; return 0; }
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; }