bool SchedulerBase::CreateSignalChannel(int *outSigId, SignalCallback callback, void *userdata) { LogAssert(IsMainThread()); if (!LogVerify(outSigId) || !LogVerify(callback)) return false; *outSigId = -1; // Create a set of pipes int fdPipe[2]; int flags; int ret = pipe(fdPipe); if (ret != 0) { gLog.ErrnoError(errno, "Unable to create pipe for signaling"); return false; } FileDescriptor pipeRead(fdPipe[0]); FileDescriptor pipeWrite(fdPipe[1]); flags = fcntl(pipeRead, F_GETFL); flags = flags | O_NONBLOCK; if (-1 == fcntl(pipeRead, F_SETFL, flags)) { gLog.LogError("Failed to set read pipe to non-blocking."); return false; } flags = fcntl(pipeWrite, F_GETFL); flags = flags | O_NONBLOCK; if (-1 == fcntl(pipeWrite, F_SETFL, flags)) { gLog.LogError("Failed to set write pipe to non-blocking."); return false; } if (!watchSocket(pipeRead)) return false; schedulerSignalItem item; item.callback = callback; item.userdata = userdata; item.fdWrite = pipeWrite; item.fdRead = pipeRead; m_signals[item.fdRead] = item; pipeWrite.Detach(); pipeRead.Detach(); *outSigId = item.fdWrite; gLog.Optional(Log::TimerDetail, "Created signal channel from %d to %d .", item.fdWrite, item.fdRead); return true; }
int write(int fd, const void* buffer, unsigned int count) { _FD_STRUCT* fds; DWORD numWritten; fds = fds_from_index(fd); if (fds == NULL) { errno = EBADF; return -1; } initStdHandlesInline(); if (fds->pipe != NULL) { if (fds->pipeChannel != -1) { // write header (for distinguishing stdout from stderr) unsigned long length = count; unsigned char header[5]; header[0] = fds->pipeChannel; memcpy(&header[1], &length, sizeof(length)); /*int x = */ pipeWrite(fds->pipe, header, sizeof(header)); } /*int x =*/ pipeWrite(fds->pipe, (unsigned char*)buffer, count); numWritten = count; } else if (fds->hFile != INVALID_HANDLE_VALUE) { if (!WriteFile(fds->hFile, buffer, count, &numWritten, NULL)) { // fds->error = TRUE; return 0; } } else return 0; return (int)numWritten; }
void evt_send(struct evt_t * evt) { int i; for(i=0;i<NB_EVT_PIPES;i++) { if(evt_pipe_tab[i].used && (evt_pipe_tab[i].mask&evt->evt_class)) { pipeWrite(&(evt_pipe_tab[i].evt_pipe), evt, sizeof(struct evt_t)); } } }
void basicOutput::writeUnicodeNullString(const cString& string, bool shouldPutZeroTerminator) { #ifdef XSTL_UNICODE // UNICODE version, just put it throw uint length = string.length(); if (shouldPutZeroTerminator) length++; pipeWrite(string.getBuffer(), length * sizeof(character)); #else unichar ch; for (uint i = 0; i < string.length(); i++) { ch = string[i]; pipeWrite(&ch, sizeof(ch)); } if (shouldPutZeroTerminator) { // Write the null terminated character ch = 0; pipeWrite(&ch, sizeof(ch)); } #endif }
void basicOutput::writeFixedSizeString(const cString& string, uint numberOfCharacters, int unicodeSize) { cBuffer buffer(numberOfCharacters * unicodeSize); uint8* buf = buffer.getBuffer(); memset(buf, 0, buffer.getSize()); uint count = t_min(string.length(), numberOfCharacters); for (uint i = 0; i < count; i++) { switch (unicodeSize) { case 1: buf[i] = cChar::covert2Ascii(string[i]); break; case 2: ((uint16*)buf)[i] = string[i]; break; case 4: ((uint32*)buf)[i] = string[i]; break; default: CHECK_FAIL(); } } pipeWrite(buffer, buffer.getSize()); }
void basicOutput::streamWriteUint8(const uint8 byte) { pipeWrite(&byte, sizeof(uint8)); }
void basicOutput::streamWriteUint16(const uint16 word) { uint8 buffer[sizeof(uint16)]; ((cEndian*)(this))->writeUint16(buffer, word); pipeWrite(buffer, sizeof(uint16)); }
void basicOutput::streamWriteUint32(const uint32 dword) { uint8 buffer[sizeof(uint32)]; ((cEndian*)(this))->writeUint32(buffer, dword); pipeWrite(buffer, sizeof(uint32)); }
void basicOutput::streamWriteUint64(const uint64 qword) { uint8 buffer[sizeof(uint64)]; ((cEndian*)(this))->writeUint64(buffer, qword); pipeWrite(buffer, sizeof(uint64)); }
void basicOutput::pipeWrite(const cBuffer& buffer, const uint length) { pipeWrite(buffer.getBuffer(), length); }