// private functions RyConnection * RyProxyServer::_getConnection(int handle){ //QMutexLocker locker(&connectionOpMutex); //Q_UNUSED(locker) //QDateTime time = QDateTime::currentDateTime(); //qDebug()<<"getConnection:" // <<time.toMSecsSinceEpoch(); //if(!_cacheConnections.contains(handle)){ //qDebug()<<"_lastConnectionId"<<_lastConnectionId; _lastConnectionId++; QMutexLocker locker(&connectionOpMutex); QThread *newThread = new QThread(); RyConnection *connection = new RyConnection(handle,_lastConnectionId); _connections.append(connection); _threads[connection] = newThread; locker.unlock(); connect(connection,SIGNAL(idleTimeout()),SLOT(onConnectionIdleTimeout())); connect(connection,SIGNAL(pipeBegin(RyPipeData_ptr)), SLOT(onPipeBegin(RyPipeData_ptr))); connect(connection,SIGNAL(pipeComplete(RyPipeData_ptr)), SLOT(onPipeComplete(RyPipeData_ptr))); connect(connection,SIGNAL(pipeError(RyPipeData_ptr)), SLOT(onPipeError(RyPipeData_ptr))); connect(connection,SIGNAL(connectionClose()), SLOT(onConnectionClosed())); connect(connection,SIGNAL(pipeBegin(RyPipeData_ptr)), SIGNAL(pipeBegin(RyPipeData_ptr))); connect(connection,SIGNAL(pipeComplete(RyPipeData_ptr)), SIGNAL(pipeComplete(RyPipeData_ptr))); connect(connection,SIGNAL(pipeError(RyPipeData_ptr)), SIGNAL(pipeError(RyPipeData_ptr))); connection->moveToThread(newThread); connect(newThread,SIGNAL(started()),connection,SLOT(run())); connect(newThread,SIGNAL(finished()),SLOT(onThreadTerminated())); newThread->start(); /* qDebug()<<"=== create connection cost:" <<time.msecsTo(QDateTime::currentDateTime()) <<time.toMSecsSinceEpoch(); */ return connection; }
int pipe_readInt(FILE *in) { int value = -1; if (fscanf(in, " %d;", &value) != 1) { pipeError(); } return value; }
char *pipe_readString(FILE *in) { int length = pipe_readInt(in); if (length <= 0) return strdup(""); char *data = malloc(length +1); if (!data) { pipeError(); return strdup(""); } data[length] = '\0'; if (fread(data, length, 1, in) == 1) { return data; } else { pipeError(); free(data); return strdup(""); } }
void pipe_readData(FILE *in, char **data, int *length) { *length = pipe_readInt(in); if (*length <= 0) { *length = 0; *data = NULL; return; } *data = malloc(*length); if ((*data == NULL) || (fread(*data, *length, 1, in) != 1)) { pipeError(); free(*data); *length = 0; } }
/* pipeObj * Reads an OpenMath object from <inDev> and copy it to <outFile>. * Syntax is checked to detect poorly structured objects. * It is able to dup an incomplete object. * (ie: that lacks the bounding <OMOBJ> </OMOBJ> * inDev: source device * outDev: destibation device * return: 0 or some error status */ static OMstatus pipeObj(OMdev inDev, FILE *outFile) { OMtokenType ttype; OMstatus status; char *buf; int i; double tmplog; /* check for end of parse */ if ((status = OMgetType(inDev, &ttype))) { if (status == OMnoMoreToken) return status; /* OK that may be a normal exit condition */ checkStatus(status); /* there is something realy wrong */ } /* pipe the token depending on its type */ switch (ttype) { case OMtokenInt32: checkStatus(OMgetInt32(inDev, &i)); /* There are at most log_10(i) + 1 characters in the string for abs(i), Add 1 inside the log10 in case abs(i) = 0 */ tmplog = (double) log10((double) abs(i)+1) + 2; /* Add 1 for the null, 1 for the '-' */ buf = (char *) malloc(sizeof(char)*( ceil(tmplog) + 2)); sprintf(buf, "%d",i); OMfwriteByte(gapOMtokenInteger, outFile); OMfwriteString(buf, strlen(buf), outFile); free(buf); break; case OMtokenBigInt: checkStatus(pipeBigInt(inDev, outFile)); break; case OMtokenSymbol: checkStatus(pipeSymb(inDev, outFile)); break; case OMtokenVar: checkStatus(pipeVar(inDev, outFile)); break; case OMtokenString: checkStatus(OMgetString(inDev, &buf)); OMfwriteByte(gapOMtokenString, outFile); OMfwriteString(buf, strlen(buf), outFile); free(buf); break; case OMtokenApp: checkStatus(pipeApp(inDev, outFile)); break; case OMtokenEndApp: fatalError(1, "syntax error: </OMA> found out of <OMA> scope."); break; case OMtokenAttr: checkStatus(pipeAttr(inDev, outFile)); break; case OMtokenEndAttr: fatalError(1, "syntax error: </OMATTR> found out of <OMATTR> scope."); break; case OMtokenAtp: fatalError(1, "syntax error: <OMATP> found out of <OMATTR> scope."); break; case OMtokenEndAtp: fatalError(1, "syntax error: </OMATP> found out of <OMATTR> scope."); break; case OMtokenBind: checkStatus(pipeBind(inDev, outFile)); break; case OMtokenEndBind: fatalError(1, "syntax error: </OMBIND> found out of <OMBIND> scope."); break; case OMtokenBVar: fatalError(1, "syntax error: <OMBVAR> found out of <OMBIND> scope."); break; case OMtokenEndBVar: fatalError(1, "syntax error: </OMBVAR> found out of <OMBIND> scope."); break; case OMtokenError: pipeError(inDev, outFile); break; case OMtokenEndError: fatalError(1, "syntax error: </OME> found out of <OME> scope."); break; case OMtokenObject: /* this object is explicitly enclosed in <OMOBJ> ... </OMOBJ> */ checkStatus(OMgetObject(inDev)); OMfwriteByte(gapOMtokenObject, outFile); /* parse embeded elements */ checkStatus(pipeObj(inDev, outFile)); /* end object tag */ checkStatus(OMgetEndObject(inDev)); OMfwriteByte(gapOMtokenEndObject, outFile); break; case OMtokenEndObject: fprintf(stderr, "warning: empty <OMOBJ></OMOBJ>!\n"); break; case OMtokenByteArray: case OMtokenWCString: case OMtokenFloat64: default: { char tmp[1024]; sprintf(tmp, "Not yet implemented or unknown token type (%d).\n", ttype); fatalError(1, tmp); } } return OMsuccess; }
pid_t forkLisp(any ex) { pid_t n; int i, hear[2], tell[2]; static int mic[2]; flushAll(); if (!Spkr) { if (pipe(mic) < 0) pipeError(ex, "open"); closeOnExec(ex, mic[0]), closeOnExec(ex, mic[1]); Spkr = mic[0]; } if (pipe(hear) < 0 || pipe(tell) < 0) pipeError(ex, "open"); closeOnExec(ex, hear[0]), closeOnExec(ex, hear[1]); closeOnExec(ex, tell[0]), closeOnExec(ex, tell[1]); for (i = 0; i < Children; ++i) if (!Child[i].pid) break; if ((n = fork()) < 0) err(ex, NULL, "fork"); if (n == 0) { void *p; Slot = i; Spkr = 0; Mic = mic[1]; close(hear[1]), close(tell[0]), close(mic[0]); if (Hear) close(Hear), closeInFile(Hear), closeOutFile(Hear); initInFile(Hear = hear[0], NULL); if (Tell) close(Tell); Tell = tell[1]; for (i = 0; i < Children; ++i) if (Child[i].pid) close(Child[i].hear), close(Child[i].tell), free(Child[i].buf); Children = 0, free(Child), Child = NULL; for (p = Env.inFrames; p; p = ((inFrame*)p)->link) ((inFrame*)p)->pid = 0; for (p = Env.outFrames; p; p = ((outFrame*)p)->link) ((outFrame*)p)->pid = 0; for (p = CatchPtr; p; p = ((catchFrame*)p)->link) ((catchFrame*)p)->fin = Zero; free(Termio), Termio = NULL; if (Repl) ++Repl; val(PPid) = val(Pid); val(Pid) = boxCnt(getpid()); run(val(Fork)); val(Fork) = Nil; return 0; } if (i == Children) allocChildren(); close(hear[0]), close(tell[1]); Child[i].pid = n; Child[i].hear = tell[0]; nonblocking(Child[i].tell = hear[1]); Child[i].ofs = Child[i].cnt = 0; Child[i].buf = NULL; return n; }