66 #if defined ( PLD_tk ) || defined ( ENABLE_tk )
92 #if defined ( __sgi ) && !defined ( SVR3 )
93 #include <sys/select.h>
95 #ifdef PL_HAVE_UNISTD_H
104 #include <sys/stat.h>
105 #include <sys/types.h>
114 #define MIN( a, b ) ( ( ( a ) < ( b ) ) ? ( a ) : ( b ) )
121 #define PACKET_MAGIC 0x6feeddcc
130 typedef struct PartialRead
135 struct PartialRead *next;
138 #define MAX_OPEN_FILES 128
140 static PartialRead *partial[MAX_OPEN_FILES];
142 static void pl_FreeReadBuffer(
int fd );
143 static void pl_Unread(
int fd,
char *
buffer,
int numBytes,
int copy );
144 static int pl_Read(
int fd,
char *
buffer,
int numReq );
167 pl_FreeReadBuffer(
int fd )
169 PartialRead *readList;
171 while ( partial[fd] != NULL )
173 readList = partial[fd];
174 partial[fd] = readList->next;
175 free( readList->buffer );
198 pl_Unread(
int fd,
char *
buffer,
int numBytes,
int copy )
207 new = (PartialRead *) malloc(
sizeof ( PartialRead ) );
210 new->buffer = (
char *) malloc( (
size_t) numBytes );
211 memcpy( new->buffer, buffer, (
size_t) numBytes );
217 new->bufSize = numBytes;
219 new->next = partial[fd];
242 pl_Read(
int fd,
char *buffer,
int numReq )
247 PartialRead *readList;
252 readList = partial[fd];
258 if ( readList == NULL )
260 numRead = (int) read( fd, buffer, (
size_t) numReq );
264 fprintf( stderr,
"received %d bytes starting with:", numRead );
265 for ( j = 0; j <
MIN( 8, numRead ); j++ )
266 fprintf( stderr,
" %x", 0x000000FF & (
unsigned long) buffer[j] );
267 fprintf( stderr,
"\n" );
279 while ( ( readList != NULL ) && ( numRead < numReq ) )
281 numToCopy = readList->bufSize - readList->offset;
282 if ( numToCopy + numRead > numReq )
284 numToCopy = numReq - numRead;
286 memcpy( buffer + numRead, readList->buffer + readList->offset, (
size_t) numToCopy );
292 readList = readList->next;
293 tmp->offset += numToCopy;
294 if ( tmp->offset == tmp->bufSize )
298 partial[fd] = readList;
300 numRead += numToCopy;
306 if ( ( numRead < numReq ) )
308 numToCopy = numReq - numRead;
309 numRead += (int) read( fd, buffer + numRead, (
size_t) numToCopy );
323 #include <arpa/inet.h>
325 #include <netinet/in.h>
326 #include <sys/socket.h>
338 get_inet(
char ** listptr,
int length )
342 while ( ( ptr = (
struct in_addr *) *listptr++ ) == NULL )
345 return inet_ntoa( *ptr );
351 register struct hostent *hostptr;
354 if ( gethostname( hostname, 100 ) )
356 Tcl_AppendResult( interp,
"Error -- cannot get host name",
361 if ( ( hostptr = gethostbyname( hostname ) ) == NULL )
363 Tcl_AppendResult( interp,
"Error -- cannot get host info for node ",
364 hostname, (
char *) NULL );
368 Tcl_SetResult( interp,
369 get_inet( hostptr->h_addr_list, hostptr->h_length ),
404 unsigned int packetLen,
header[2];
406 unsigned char hbuf[8];
415 numRead = pl_Read( iodev->
fd, (
char *) hbuf, headerSize );
420 fprintf( stderr,
"Incorrect header read, numRead = %d\n", numRead );
428 if ( numRead < headerSize )
431 fprintf( stderr,
"Incomplete header read, numRead = %d\n", numRead );
433 pl_Unread( iodev->
fd, (
char *) hbuf, numRead, 1 );
434 Tcl_ResetResult( interp );
448 header[0] |= (
unsigned int) ( hbuf[j++] << 24 );
449 header[0] |= (
unsigned int) ( hbuf[j++] << 16 );
450 header[0] |= (
unsigned int) ( hbuf[j++] << 8 );
451 header[0] |= hbuf[j++];
454 header[1] |= (
unsigned int) ( hbuf[j++] << 24 );
455 header[1] |= (
unsigned int) ( hbuf[j++] << 16 );
456 header[1] |= (
unsigned int) ( hbuf[j++] << 8 );
457 header[1] |= hbuf[j++];
466 if ( header[0] != PACKET_MAGIC )
468 fprintf( stderr,
"Badly formatted packet, numRead = %d\n", numRead );
469 Tcl_AppendResult( interp,
"Error reading from ", iodev->
typeName,
470 ": badly formatted packet", (
char *) NULL );
473 packetLen = header[1] - (
unsigned int) headerSize;
479 if ( header[1] > (
unsigned) pdfs->
bufmax )
481 free( (
void *) pdfs->
buffer );
482 pdfs->
bufmax = header[1] + 32;
483 pdfs->
buffer = (
unsigned char *) malloc( pdfs->
bufmax );
494 if ( iodev->
type == 0 )
496 numRead = pl_Read( iodev->
fd, (
char *) pdfs->
buffer, (
int) packetLen );
501 if ( Tdp_FDIsReady( iodev->
fd ) & TCL_FILE_READABLE )
503 numRead = pl_Read( iodev->
fd, (
char *) pdfs->
buffer, packetLen );
508 fprintf( stderr,
"Packet not ready, putting back header\n" );
510 pl_Unread( iodev->
fd, (
char *) hbuf, headerSize, 1 );
511 Tcl_ResetResult( interp );
522 if ( (
unsigned) numRead != packetLen )
525 fprintf( stderr,
"Incomplete packet read, numRead = %d\n", numRead );
527 pl_Unread( iodev->
fd, (
char *) pdfs->
buffer, numRead, 1 );
528 pl_Unread( iodev->
fd, (
char *) hbuf, headerSize, 1 );
532 pdfs->
bp = (size_t) numRead;
534 fprintf( stderr,
"received %d byte packet starting with:", numRead );
535 for ( j = 0; j < 4; j++ )
537 fprintf( stderr,
" %x", 0x000000FF & (
unsigned long) pdfs->
buffer[j] );
539 fprintf( stderr,
"\n" );
555 if ( errno == EWOULDBLOCK || errno == EAGAIN )
557 Tcl_ResetResult( interp );
564 errMsg = Tcl_PosixError( interp );
574 if ( iodev->
type == 0 )
577 #if !defined ( MAC_TCL ) && !defined ( __WIN32__ ) && !defined ( __CYGWIN__ )
578 Tk_DeleteFileHandler( iodev->
fd );
582 pl_FreeReadBuffer( iodev->
fd );
584 Tcl_ResetResult( interp );
591 Tcl_AppendResult( interp,
"pl_PacketReceive -- error reading from ",
592 iodev->
typeName,
": ", errMsg, (
char *) NULL );
620 unsigned char hbuf[8];
621 unsigned int packetLen, header[2];
632 packetLen = (
unsigned int) pdfs->
bp + 8;
634 header[0] = PACKET_MAGIC;
635 header[1] = packetLen;
644 hbuf[j++] = (
unsigned char) ( ( header[0] & (
unsigned long) 0xFF000000 ) >> 24 );
645 hbuf[j++] = (
unsigned char) ( ( header[0] & (
unsigned long) 0x00FF0000 ) >> 16 );
646 hbuf[j++] = (
unsigned char) ( ( header[0] & (
unsigned long) 0x0000FF00 ) >> 8 );
647 hbuf[j++] = (
unsigned char) ( header[0] & (
unsigned long) 0x000000FF );
649 hbuf[j++] = (
unsigned char) ( ( header[1] & (
unsigned long) 0xFF000000 ) >> 24 );
650 hbuf[j++] = (
unsigned char) ( ( header[1] & (
unsigned long) 0x00FF0000 ) >> 16 );
651 hbuf[j++] = (
unsigned char) ( ( header[1] & (
unsigned long) 0x0000FF00 ) >> 8 );
652 hbuf[j++] = (
unsigned char) ( header[1] & (
unsigned long) 0x000000FF );
661 buffer = (
char *) malloc( len );
663 memcpy( buffer, (
char *) hbuf, 8 );
664 memcpy( buffer + 8, (
char *) pdfs->
buffer, pdfs->
bp );
667 fprintf( stderr,
"sending %z byte packet starting with:", len );
668 for ( j = 0; j < 12; j++ )
670 fprintf( stderr,
" %x", 0x000000FF & (
unsigned long) buffer[j] );
672 fprintf( stderr,
"\n" );
674 numSent = (int) write( iodev->
fd, buffer, len );
678 if ( (
unsigned) numSent != packetLen )
680 if ( ( errno == 0 ) || ( errno == EWOULDBLOCK || errno == EAGAIN ) )
685 Tcl_ResetResult( interp );
686 sprintf( tmp,
"%d", numSent - 8 );
687 Tcl_SetResult( interp, tmp, TCL_VOLATILE );
690 else if ( errno == EPIPE )
696 if ( iodev->
type == 0 )
701 Tcl_SetResult( interp, tmp, TCL_VOLATILE );
706 Tcl_AppendResult( interp,
"pl_PacketSend -- error writing to ",
708 Tcl_PosixError( interp ), (
char *) NULL );
717 sprintf( tmp,
"%d", numSent - 8 );
718 Tcl_SetResult( interp, tmp, TCL_VOLATILE );
729 #endif // defined(PLD_tk) || defined (ENABLE_tk)
PLDLLIMPEXP_TCLTK int pl_PacketReceive(Tcl_Interp *interp, PLiodev *iodev, PDFstrm *pdfs)
int plHost_ID(ClientData clientData, Tcl_Interp *interp, int argc, const char **argv)
static Tcl_Interp * interp
PLDLLIMPEXP_TCLTK int pl_PacketSend(Tcl_Interp *interp, PLiodev *iodev, PDFstrm *pdfs)