static int TRANS(Os2BytesReadable)(XtransConnInfo ciptr, BytesReadable_t *pend ) { ULONG rc, state, nread; AVAILDATA avail; char buffer; PRMSG(2,"Os2BytesReadable(%x->%d,%x)\n", ciptr, ciptr->fd, pend); rc = DosPeekNPipe (ciptr->fd, &buffer, 0, &nread, &avail, &state); if (rc != 0) { errno = EPIPE; *pend = 0; return -1; } if (state == NP_STATE_CLOSING) { errno = EPIPE; *pend = 0; return -1; } errno = 0; *pend = avail.cbpipe; return 0; }
LONG XNamedPipeClient::GetState(void) { ULONG state, buffer, buffer2; AVAILDATA avail; DosPeekNPipe(handle, &buffer, 4, &buffer2, &avail, &state); return state; }
int main(VOID) { APIRET rc = NO_ERROR; /* Return code */ CHAR message[256] = ""; /* Message buffer */ HFILE PipeHandle = NULLHANDLE; /* Pipe handle */ PIPEINFO PipeBuffer[4] = {{0}}; struct _AVAILDATA BytesAvail = {0}; UCHAR Buffer[200] = {0}; ULONG bytes = 0; ULONG Action = 0; ULONG PipeState = 0; ULONG HandType = 0; ULONG FlagWord = 0; ULONG BytesRead = 0; rc = DosOpen("\\PIPE\\EXAMPLE", &PipeHandle, &Action, 0, 0, FILE_OPEN, OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYREADWRITE | OPEN_FLAGS_FAIL_ON_ERROR, NULL); if (rc != NO_ERROR) { printf("DosOpen error: error code = %u\n", rc); return 1; } else printf("Connected to pipe.\n"); rc = DosQueryHType(PipeHandle, &HandType, &FlagWord); if (rc != NO_ERROR) { printf("DosQueryHType error: error code = %u\n", rc); return 1; } else printf("Handle type value is %u\n", HandType); rc = DosPeekNPipe(PipeHandle, Buffer, sizeof(Buffer), &BytesRead, &BytesAvail, &PipeState); if (rc != NO_ERROR) { printf("DosPeekNPipe error: error code = %u\n", rc); return 1; } else printf("Pipe status value is %u\n\n", PipeState); printf("Enter message to send to PIPEHOST: "); fflush(NULL); /* Flush above printf out to display */ gets(message); rc = DosWrite(PipeHandle, message, strlen(message), &bytes); if (rc != NO_ERROR) { printf("DosWrite error: error code = %u\n", rc); return 1; } rc = DosRead(PipeHandle, message, sizeof(message), &bytes); if (rc != NO_ERROR) { printf("DosRead error: error code = %u\n", rc); return 1; } printf("\nMessage received from PIPEHOST: %s\n\n", message); rc = DosClose(PipeHandle); /* Should check if (rc != NO_ERROR) here... */ printf("...Disconnected\n"); return NO_ERROR; }
static void ProcessConnection( void ) { char buff[MAX_TRANS]; ULONG bytes_read; unsigned long max; struct _AVAILDATA BytesAvail; ULONG PipeState; APIRET rc; RESULTCODES res; PID dummy; char *dir; for( ;; ) { DosRead( LnkHdl, buff, sizeof( buff ), &bytes_read ); if( bytes_read == 0 ) break; buff[bytes_read] = '\0'; switch( buff[0] ) { case LNK_CWD: rc = 0; dir = &buff[1]; if( isalpha( dir[0] ) && dir[1] == ':' ) { rc = DosSetDefaultDisk( toupper( dir[0] ) - ('A' - 1) ); dir += 2; } if( rc == 0 && dir[0] != '\0' ) { rc = DosSetCurrentDir( dir ); } SendStatus( rc ); break; case LNK_RUN: DosSetNPHState( RedirHdl, NP_NOWAIT | NP_READMODE_BYTE ); DosConnectNPipe( RedirHdl ); DosSetNPHState( RedirHdl, NP_WAIT | NP_READMODE_BYTE ); RunCmd( &buff[1] ); break; case LNK_QUERY: max = *(unsigned long *)&buff[1]; if( max > sizeof( buff ) ) max = sizeof( buff ); --max; rc = DosPeekNPipe(RedirHdl, buff, 0, &bytes_read, &BytesAvail, &PipeState ); if( rc == 0 && BytesAvail.cbpipe != 0 ) { DosRead( RedirHdl, &buff[1], max, &bytes_read ); buff[0] = LNK_OUTPUT; DosWrite( LnkHdl, buff, bytes_read + 1, &bytes_read ); } else { rc = DosWaitChild( DCWA_PROCESS, DCWW_NOWAIT, &res, &dummy, ProcId ); if( rc != ERROR_CHILD_NOT_COMPLETE ) { DosDisConnectNPipe( RedirHdl ); SendStatus( res.codeResult ); ProcId = 0; } else { /* let someone else run */ DosSleep( 1 ); buff[0] = LNK_NOP; DosWrite( LnkHdl, buff, 1, &bytes_read ); } } break; case LNK_CANCEL: DosSendSignalException( ProcId, XCPT_SIGNAL_INTR ); break; case LNK_ABORT: DosKillProcess( DKP_PROCESSTREE, ProcId ); break; case LNK_DONE: return; case LNK_SHUTDOWN: exit( 0 ); break; } } }