void IO_ProfileSimResults::WriteOneRun(const ProfileSimRunResults& runRes, int& nextRec) { // run header WriteString(runRes.runID, ProfileSimRunResults::runIDLen); WriteStringArray(runRes.caseValDesc); WriteStringArray(runRes.caseValShortDesc); WriteStringArray(runRes.caseSeqData.sequenceIDs); WriteDoubleArray(runRes.caseSeqData.sequenceStartTimes); FlushBuffer(nextRec); // next record contains case start records BufferReset(nextRec); SC_IntArray caseStartRecs(runRes.Size(), -1); int runStartRec = nextRec; WriteIntArray(caseStartRecs, nextRec); // individual case results for (int i = 0; i < runRes.Size(); i++) { caseStartRecs[i] = nextRec; WriteOneCase(runRes[i], nextRec); } // update case start BufferReset(runStartRec); WriteIntArray(caseStartRecs); FlushBuffer(); }
static void _WorkerReadHandler(Event event, Connection* source, Worker* worker) { Buffer* bufferCopy = NULL; Event eventToProcess = AIO4C_OUTBOUND_DATA_EVENT; ProbeTimeStart(AIO4C_TIME_PROBE_DATA_PROCESS); if (event != AIO4C_INBOUND_DATA_EVENT || source->state == AIO4C_CONNECTION_STATE_CLOSED) { return; } bufferCopy = AllocateBuffer(worker->pool); if (event == AIO4C_INBOUND_DATA_EVENT) { BufferFlip(source->readBuffer); BufferCopy(bufferCopy, source->readBuffer); BufferReset(source->readBuffer); eventToProcess = AIO4C_READ_EVENT; } if (!EnqueueTaskItem(worker->queue, eventToProcess, source, bufferCopy)) { ReleaseBuffer(&bufferCopy); return; } ProbeTimeEnd(AIO4C_TIME_PROBE_DATA_PROCESS); }
void IO_ProfileSimResults::WriteOneCase(const ProfileSimCaseResults& caseRes, int& nextRec) { BufferReset(nextRec); WriteString(caseRes.caseID, ProfileSimCaseResults::caseIDLen); WriteDouble(caseRes.staticPressure); WriteBool(caseRes.caseDataIsExtended); WriteInt(caseRes.caseValues.Size()); for (int i = 0; i < caseRes.caseValues.Size(); i++) WriteDouble(caseRes.caseValues[i]); if (!caseRes.caseDataIsExtended) { const DC_GridData& currGrid = caseRes.caseData; WriteString(currGrid.gridID, DC_GridData::gridIDLen); WriteString(currGrid.xData.dataID, DC_DataCoord::dataIDLen); WriteString(currGrid.yData.dataID, DC_DataCoord::dataIDLen); WriteBool(currGrid.xData.dataIsLog); WriteBool(currGrid.yData.dataIsLog); WriteInt(currGrid.xData.Size()); WriteInt(currGrid.yData.Size()); WriteDoubleArray(currGrid.xData); WriteDoubleArray(currGrid.yData); for (int i = 0; i < currGrid.Size(); i++) WriteDouble(currGrid[i]); } else { const ExtendedProfile& currPro = caseRes.extendedCaseData; WriteInt(currPro.ntotalVert); WriteInt(currPro.nradial); WriteInt(currPro.nconstantVert); WriteInt(currPro.nvariableVert); // WriteDouble(currPro.constantVertThick); // v1 WriteDoubleArray(currPro.constantVertZPos); WriteDoubleArray(currPro.nodeRadii); WriteInt(currPro.Size()); for (int i = 0; i < currPro.Size(); i++) { const ExtendedProfileTimeData& currSing = currPro[i]; WriteDouble(currSing.profileTime); if (currPro.nvariableVert > 0) WriteDoubleArray(currSing.waterTablePos); WriteDoubleMatrix(currSing.nodalPressures); } } FlushBuffer(nextRec); }
bool ConnectionWrite(Connection* connection) { ssize_t nbWrite = 0; Buffer* buffer = connection->writeBuffer; ErrorCode code = AIO4C_ERROR_CODE_INITIALIZER; bool pendingCloseMemorized = false; aio4c_byte_t* data = NULL; if (!connection->canWrite) { code.expected = AIO4C_CONNECTION_STATE_CONNECTED; _ConnectionHandleError(connection, AIO4C_LOG_LEVEL_DEBUG, AIO4C_CONNECTION_STATE_ERROR, &code); return false; } if (connection->state == AIO4C_CONNECTION_STATE_PENDING_CLOSE) { pendingCloseMemorized = true; } if (!BufferHasRemaining(buffer)) { BufferReset(buffer); _ConnectionEventHandle(connection, AIO4C_WRITE_EVENT); BufferFlip(buffer); } if (BufferGetLimit(buffer) - BufferGetPosition(buffer) < 0) { code.buffer = buffer; _ConnectionHandleError(connection, AIO4C_LOG_LEVEL_ERROR, AIO4C_BUFFER_UNDERFLOW_ERROR, &code); return false; } data = BufferGetBytes(buffer); if ((nbWrite = send(connection->socket, (void*)&data[BufferGetPosition(buffer)], BufferRemaining(buffer), MSG_NOSIGNAL)) < 0) { #ifndef AIO4C_WIN32 code.error = errno; #else /* AIO4C_WIN32 */ code.source = AIO4C_ERRNO_SOURCE_WSA; #endif /* AIO4C_WIN32 */ _ConnectionHandleError(connection, AIO4C_LOG_LEVEL_ERROR, AIO4C_WRITE_ERROR, &code); return false; } ProbeSize(AIO4C_PROBE_NETWORK_WRITE_SIZE, nbWrite); BufferPosition(buffer, BufferGetPosition(buffer) + nbWrite); if (BufferHasRemaining(connection->writeBuffer)) { return true; } else if (pendingCloseMemorized) { ConnectionShutdown(connection); } return false; }
void OnException(int signo) { if (signo == SIGCHLD) return; #define WRITE_STRING_LEN(STR, LEN) \ do { \ write(STDERR_FILENO, (STR), (LEN)); \ if (fd) \ write(fd, (STR), (LEN)); \ } while(0) #define WRITE_STRING(STR) \ WRITE_STRING_LEN(STR, (sizeof(STR) - sizeof(STR[0]))) #define WRITE_BUFFER(BUFFER) \ WRITE_STRING_LEN((BUFFER).buffer, (BUFFER).offset) MinimalBuffer buffer; int fd = 0; if (crashlog && (signo == SIGSEGV || signo == SIGABRT || signo == SIGKILL)) fd = open(crashlog, O_WRONLY | O_CREAT | O_TRUNC, 0644); /* print signal info */ BufferReset(&buffer); BufferAppendUInt64(&buffer, signo, 10); WRITE_STRING("=========================\n"); WRITE_STRING("FCITX " VERSION " -- Get Signal No.: "); WRITE_BUFFER(buffer); WRITE_STRING("\n"); /* print time info */ time_t t = time(NULL); BufferReset(&buffer); BufferAppendUInt64(&buffer, t, 10); WRITE_STRING("Date: try \"date -d @"); WRITE_BUFFER(buffer); WRITE_STRING("\" if you are using GNU date ***\n"); /* print process info */ BufferReset(&buffer); BufferAppendUInt64(&buffer, getpid(), 10); WRITE_STRING("ProcessID: "); WRITE_BUFFER(buffer); WRITE_STRING("\n"); #if defined(ENABLE_BACKTRACE) #define BACKTRACE_SIZE 32 void *array[BACKTRACE_SIZE] = {NULL, }; int size = backtrace(array, BACKTRACE_SIZE); backtrace_symbols_fd(array, size, STDERR_FILENO); if (fd) backtrace_symbols_fd(array, size, fd); #endif if (fd) close(fd); switch (signo) { case SIGKILL: break; case SIGABRT: case SIGSEGV: case SIGBUS: case SIGILL: case SIGFPE: exit(1); break; default: { if (!instance || !instance->initialized) { exit(1); break; } uint8_t sig = 0; if (signo < 0xff) sig = (uint8_t)(signo & 0xff); write(selfpipe[1], &sig, 1); signal(signo, OnException); } break; } }
JNIEXPORT void JNICALL Java_com_aio4c_buffer_Buffer_reset(JNIEnv* jvm, jobject buffer) { Buffer* _buffer = _GetBuffer(jvm, buffer); BufferReset(_buffer); }
bool IO_UncertSimResults::AddUncertRun(const DC_UncertSimResults& resultHeader, const UncertRunResults& results, bool addExisting) { GenAppClearMsgs(); bool openExisting = addExisting && ConvFileC::CFfileExists(fileName); DC_UncertSimResults currHeader; if (openExisting) { if (!ReadFileHeader(currHeader, false)) { GenAppErrorMsg("UncertSimResults", "File contents incompatible"); return false; } if ((currHeader.IsMultiple() && (!resultHeader.IsMultiple())) || ((!currHeader.IsMultiple()) && resultHeader.IsMultiple())) { GenAppErrorMsg("UncertSimResults", "Cannot combine multiple & not multiple"); return false; } if (currHeader.IsMultiple()) { if (currHeader.multipleRunVariableIDs.Size() != resultHeader.multipleRunVariableIDs.Size()) GenAppErrorMsg("UncertSimResults", "Multiple run results different size??"); } } try { SC_IntArray runStartRecs; runStartRecs.SetResizable(); int nextRec, runIndexRecord; if (openExisting) { ReadFileHeader(currHeader, true); ReadNextRecord(); runIndexRecord = GetCurrentRec(); nextRec = ReadInt(); ReadIntArray(runStartRecs); } else { OpenFile(fileName, false); // set up initial header info nRuns = 0; WriteFileHeader(resultHeader); FlushBuffer(nextRec); runIndexRecord = nextRec; // initial header BufferReset(nextRec); WriteInt(0); FlushBuffer(nextRec); // backup and set nextRec BufferReset(runIndexRecord); WriteInt(nextRec); // reflush FlushBuffer(); } int runStart = nextRec; // set to next avail BufferReset(nextRec); // add the run to the end WriteOneRun(results, nextRec); nRuns++; // update header BufferReset(0); WriteFileHeader(resultHeader); FlushBuffer(); // update next avail record BufferReset(runIndexRecord); WriteInt(nextRec); // update run start records runStartRecs += runStart; WriteIntArray(runStartRecs); FlushBuffer(); CloseFile(); return true; } catch (BufFileC::BufferFileError& err) { GenAppErrorMsg("WriteUncertResults", err.errMsg); } CloseFile(); return false; }
bool IO_ProfileSimResults::AddSimRun(const ProfileSimRunResults& results, bool addExisting) { GenAppClearMsgs(); bool fileOK = addExisting && ReadFileHeader(false); try { SC_IntArray runStartRecs; runStartRecs.SetResizable(); int nextRec, runIndexRecord; if (fileOK) { ReadFileHeader(true); ReadNextRecord(); runIndexRecord = GetCurrentRec(); nextRec = ReadInt(); ReadIntArray(runStartRecs); } else { OpenFile(fileName, false); WriteStdFileHeader(fileHeader, StringLength(fileHeader) + 1, writeVersion, 0); WriteInt(0); // 0 runs FlushBuffer(nextRec); runIndexRecord = nextRec; // initial header BufferReset(nextRec); WriteInt(0); WriteInt(0); WriteInt(0); FlushBuffer(nextRec); // backup and set nextRec BufferReset(runIndexRecord); WriteInt(nextRec); WriteInt(0); WriteInt(0); // reflush FlushBuffer(); } int runStart = nextRec; // set to next avail BufferReset(nextRec); // add the run to the end WriteOneRun(results, nextRec); nRuns++; // update header BufferReset(0); WriteStdFileHeader(fileHeader, StringLength(fileHeader) + 1, writeVersion, 0); WriteInt(nRuns); FlushBuffer(); // update next avail record BufferReset(runIndexRecord); WriteInt(nextRec); // update run start records runStartRecs += runStart; WriteIntArray(runStartRecs); FlushBuffer(); CloseFile(); return true; } catch (BufFileC::BufferFileError& err) { GenAppErrorMsg("WriteSimResults", err.errMsg); } CloseFile(); return false; }
int main(int argc, char* argv[]) { Buffer* a = NULL; aio4c_byte_t* data = NULL; aio4c_byte_t b = 0; char* s = NULL; int i = 0; int d = 12345; size_t len = 0; Aio4cInit(argc, argv, NULL, NULL); s = aio4c_malloc(strlen(TEST_STRING) + 1); assert(s != NULL); memcpy(s, TEST_STRING, strlen(TEST_STRING) + 1); a = NewBuffer(BUFFER_SIZE); assert(a != NULL); assert(BufferGetCapacity(a) == BUFFER_SIZE); assert(BufferGetLimit(a) == BUFFER_SIZE); assert(BufferGetPosition(a) == 0); assert(BufferHasRemaining(a) == true); assert(BufferRemaining(a) == BUFFER_SIZE); assert((data = BufferGetBytes(a)) != NULL); for(i = 0; i < BUFFER_SIZE; i++) { assert(data[i] == 0); } assert(BufferPutInt(a, &d) == true); assert(BufferGetPosition(a) == sizeof(int)); assert(BufferFlip(a) == a); assert(BufferGetPosition(a) == 0); assert(BufferGetLimit(a) == sizeof(int)); d = 0; assert(BufferGetInt(a, &d) == true); assert(d == 12345); d = 45678; assert(BufferGetInt(a, &d) == false); assert(d == 45678); assert(BufferPutInt(a, &d) == false); assert(BufferReset(a) == a); assert(BufferPutString(a, s) == true); assert(BufferGetPosition(a) == (int)(strlen(s) + 1)); assert(BufferFlip(a) == a); assert(BufferGetPosition(a) == 0); assert(BufferGetLimit(a) == (int)(strlen(s) + 1)); for(i = 0; i < (int)(strlen(s) + 1); i++) { assert(BufferGetByte(a, &b) == true); assert(b == s[i]); assert(BufferGetPosition(a) == (i + 1)); } assert(BufferGetByte(a, &b) == false); assert(BufferHasRemaining(a) == false); assert(BufferReset(a) == a); assert(BufferGetCapacity(a) == BUFFER_SIZE); assert(BufferGetLimit(a) == BUFFER_SIZE); assert(BufferGetPosition(a) == 0); assert(BufferHasRemaining(a) == true); assert((data = BufferGetBytes(a)) != NULL); assert(BufferLimit(a, strlen(s) + 1) == a); assert(BufferGetLimit(a) == (int)(strlen(s) + 1)); len = strlen(s); s[len] = '0'; for(i = 0; i < BUFFER_SIZE; i++) { assert(data[i] == 0); } assert(BufferPutString(a, s) == false); for(i = 0; i < BUFFER_SIZE; i++) { assert(data[i] == 0); } s[len] = '\0'; assert(BufferPutString(a, s) == true); assert(BufferFlip(a) == a); for(i = 0; i < (int)(strlen(s) + 1); i++) { assert(BufferGetByte(a, &b) == true); assert(b == s[i]); assert(BufferGetPosition(a) == (i + 1)); } assert(BufferHasRemaining(a) == false); Aio4cEnd(); return 0; }
Connection* ConnectionInit(Connection* connection) { ErrorCode code = AIO4C_ERROR_CODE_INITIALIZER; #ifndef AIO4C_WIN32 if ((connection->socket = socket(PF_INET, SOCK_STREAM, 0)) == -1) { code.error = errno; #else /* AIO4C_WIN32 */ if ((connection->socket = socket(PF_INET, SOCK_STREAM, 0)) == SOCKET_ERROR) { code.source = AIO4C_ERRNO_SOURCE_WSA; #endif /* AIO4C_WIN32 */ return _ConnectionHandleError(connection, AIO4C_LOG_LEVEL_ERROR, AIO4C_SOCKET_ERROR, &code); } #ifndef AIO4C_WIN32 if (fcntl(connection->socket, F_SETFL, O_NONBLOCK) == -1) { code.error = errno; #else /* AIO4C_WIN32 */ unsigned long ioctl = 1; if (ioctlsocket(connection->socket, FIONBIO, &ioctl) == SOCKET_ERROR) { code.source = AIO4C_ERRNO_SOURCE_WSA; #endif /* AIO4C_WIN32 */ return _ConnectionHandleError(connection, AIO4C_LOG_LEVEL_ERROR, AIO4C_FCNTL_ERROR, &code); } ConnectionState(connection, AIO4C_CONNECTION_STATE_INITIALIZED); return connection; } Connection* ConnectionConnect(Connection* connection) { ErrorCode code = AIO4C_ERROR_CODE_INITIALIZER; ConnectionState newState = AIO4C_CONNECTION_STATE_CONNECTING; if (connection->state != AIO4C_CONNECTION_STATE_INITIALIZED) { code.expected = AIO4C_CONNECTION_STATE_INITIALIZED; return _ConnectionHandleError(connection, AIO4C_LOG_LEVEL_DEBUG, AIO4C_CONNECTION_STATE_ERROR, &code); } if (connect(connection->socket, AddressGetAddr(connection->address), AddressGetAddrSize(connection->address)) == -1) { #ifndef AIO4C_WIN32 code.error = errno; if (errno == EINPROGRESS) { #else /* AIO4C_WIN32 */ int error = WSAGetLastError(); code.source = AIO4C_ERRNO_SOURCE_WSA; if (error == WSAEINPROGRESS || error == WSAEALREADY || error == WSAEWOULDBLOCK) { #endif /* AIO4C_WIN32 */ newState = AIO4C_CONNECTION_STATE_CONNECTING; } else { return _ConnectionHandleError(connection, AIO4C_LOG_LEVEL_ERROR, AIO4C_CONNECT_ERROR, &code); } } else { newState = AIO4C_CONNECTION_STATE_CONNECTED; } ConnectionState(connection, newState); return connection; } Connection* ConnectionFinishConnect(Connection* connection) { ErrorCode code = AIO4C_ERROR_CODE_INITIALIZER; int soError = 0; socklen_t soSize = sizeof(int); #ifdef AIO4C_HAVE_POLL aio4c_poll_t polls[1] = { { .fd = connection->socket, .events = POLLOUT, .revents = 0 } }; #ifndef AIO4C_WIN32 if (poll(polls, 1, -1) == -1) { code.error = errno; #else /* AIO4C_WIN32 */ if (WSAPoll(polls, 1, -1) == SOCKET_ERROR) { code.source = AIO4C_ERRNO_SOURCE_WSA; #endif /* AIO4C_WIN32 */ return _ConnectionHandleError(connection, AIO4C_LOG_LEVEL_ERROR, AIO4C_POLL_ERROR, &code); } if (polls[0].revents > 0) { #else /* AIO4C_HAVE_POLL */ fd_set writeSet; fd_set errorSet; FD_ZERO(&writeSet); FD_ZERO(&errorSet); FD_SET(connection->socket, &writeSet); FD_SET(connection->socket, &errorSet); #ifndef AIO4C_WIN32 if (select(connection->socket + 1, NULL, &writeSet, &errorSet, NULL) == -1) { code.error = errno; #else /* AIO4C_WIN32 */ if (select(connection->socket + 1, NULL, &writeSet, &errorSet, NULL) == SOCKET_ERROR) { code.source = AIO4C_ERRNO_SOURCE_WSA; #endif /* AIO4C_WIN32 */ return _ConnectionHandleError(connection, AIO4C_LOG_LEVEL_ERROR, AIO4C_SELECT_ERROR, &code); } if (FD_ISSET(connection->socket, &writeSet) || FD_ISSET(connection->socket, &errorSet)) { #endif /* AIO4C_HAVE_POLL */ #ifndef AIO4C_WIN32 if (getsockopt(connection->socket, SOL_SOCKET, SO_ERROR, &soError, &soSize) != 0) { code.error = errno; #else /* AI4OC_WIN32 */ if (getsockopt(connection->socket, SOL_SOCKET, SO_ERROR, (char*)&soError, &soSize) == SOCKET_ERROR) { code.source = AIO4C_ERRNO_SOURCE_WSA; #endif /* AIO4C_WIN32 */ return _ConnectionHandleError(connection, AIO4C_LOG_LEVEL_ERROR, AIO4C_GETSOCKOPT_ERROR, &code); } if (soError != 0) { #ifndef AIO4C_WIN32 code.error = soError; #else /* AIO4C_WIN32 */ code.source = AIO4C_ERRNO_SOURCE_SOE; code.soError = soError; #endif /* AIO4C_WIN32 */ return _ConnectionHandleError(connection, AIO4C_LOG_LEVEL_ERROR, AIO4C_FINISH_CONNECT_ERROR, &code); } } return connection; } Connection* ConnectionRead(Connection* connection) { Buffer* buffer = NULL; ssize_t nbRead = 0; ErrorCode code = AIO4C_ERROR_CODE_INITIALIZER; aio4c_byte_t* data = NULL; buffer = connection->readBuffer; if (!BufferHasRemaining(buffer)) { code.buffer = buffer; return _ConnectionHandleError(connection, AIO4C_LOG_LEVEL_ERROR, AIO4C_BUFFER_OVERFLOW_ERROR, &code); } data = BufferGetBytes(buffer); if ((nbRead = recv(connection->socket, (void*)&data[BufferGetPosition(buffer)], BufferRemaining(buffer), 0)) < 0) { #ifndef AIO4C_WIN32 code.error = errno; #else /* AIO4C_WIN32 */ code.source = AIO4C_ERRNO_SOURCE_WSA; #endif /* AIO4C_WIN32 */ return _ConnectionHandleError(connection, AIO4C_LOG_LEVEL_ERROR, AIO4C_READ_ERROR, &code); } ProbeSize(AIO4C_PROBE_NETWORK_READ_SIZE, nbRead); if (nbRead == 0) { if (connection->state == AIO4C_CONNECTION_STATE_PENDING_CLOSE) { ConnectionState(connection, AIO4C_CONNECTION_STATE_CLOSED); return connection; } else { return _ConnectionHandleError(connection, AIO4C_LOG_LEVEL_INFO, AIO4C_CONNECTION_DISCONNECTED, &code); } } if (!connection->canRead) { Log(AIO4C_LOG_LEVEL_WARN, "received data on connection %s when reading is not allowed", connection->string); BufferReset(buffer); return connection; } BufferPosition(buffer, BufferGetPosition(buffer) + nbRead); _ConnectionEventHandle(connection, AIO4C_INBOUND_DATA_EVENT); return connection; }
void OnException(int signo) { if (signo == SIGCHLD) return; MinimalBuffer buffer; int fd = -1; if (crashlog && (signo == SIGSEGV || signo == SIGABRT)) fd = open(crashlog, O_WRONLY | O_CREAT | O_TRUNC, 0644); /* print signal info */ BufferReset(&buffer); BufferAppendUInt64(&buffer, signo, 10); _write_string(fd, "=========================\n"); _write_string(fd, "FCITX " VERSION " -- Get Signal No.: "); _write_buffer(fd, &buffer); _write_string(fd, "\n"); /* print time info */ time_t t = time(NULL); BufferReset(&buffer); BufferAppendUInt64(&buffer, t, 10); _write_string(fd, "Date: try \"date -d @"); _write_buffer(fd, &buffer); _write_string(fd, "\" if you are using GNU date ***\n"); /* print process info */ BufferReset(&buffer); BufferAppendUInt64(&buffer, getpid(), 10); _write_string(fd, "ProcessID: "); _write_buffer(fd, &buffer); _write_string(fd, "\n"); #if defined(ENABLE_BACKTRACE) #define BACKTRACE_SIZE 32 void *array[BACKTRACE_SIZE] = { NULL, }; int size = backtrace(array, BACKTRACE_SIZE); backtrace_symbols_fd(array, size, STDERR_FILENO); if (fd >= 0) backtrace_symbols_fd(array, size, fd); #endif if (fd >= 0) close(fd); switch (signo) { case SIGABRT: case SIGSEGV: case SIGBUS: case SIGILL: case SIGFPE: exit(1); break; default: { if (!instance || !instance->initialized) { exit(1); break; } uint8_t sig = 0; if (signo < 0xff) sig = (uint8_t)(signo & 0xff); write(selfpipe[1], &sig, 1); signal(signo, OnException); } break; } }