📄 pipe.c
字号:
/* TradeClient <http://tradeclient.sourceforge.net> * $Id: pipe.c,v 1.22 2001/03/20 22:19:33 ttabner Exp $ * * Copyright (C) 1999-2000 Bynari Inc. * Copyright (C) 2001 Project TradeClient * * LGPL * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU Library General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library * General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */#include "puma.h"#ifdef write#undef write#endif/* data protocallbytes | description-------------------------------------1 | type char-------------------------------------4 | data size-------------------------------------var | data (lenght of 'data size')-------------------------------------NOTE: data size should never be 0*/void pipe_data_put (int type, char *data, unsigned long size) { int fd=tm_globs->dpipe[1]; char tmps ; char *snum ;#if DEBUG > 6 printf( "pipe_data_put\n" ) ;#endif#if DEBUG > 6 printf ("data put type=%d\n", type); printf ("data put size=%d\n", (int)size);#endif#ifdef DMALLOC dmalloc_verify( 0 ) ;#endif tmps = (char)type; if( write (fd, &tmps, 1) == -1 ) {#if DEBUG > 6 perror( "pipe_date_put() write failed when writing type\n" ) ;#endif } snum = disassemble_long( size ) ; if( write (fd, snum, 4) == -1 ) {#if DEBUG > 6 perror( "pipe_date_put() write failed when writing length\n" ) ;#endif } free (snum); if( data ) { if( write (fd, data, size) == -1 ) {#if DEBUG > 6 perror( "pipe_date_put() write failed when writing data\n" ) ;#endif } }#ifdef DMALLOC dmalloc_verify( 0 ) ;#endif#if DEBUG > 6 printf( "pipe_data_put finished\n" ) ;#endif return ;}char *pipe_data_get (int *type, unsigned long *size, int *error) { int fd=tm_globs->dpipe[0]; int fdarg ; signed long bread, rdata, rbsize ; char *data=NULL; char snum[5] ;#if DEBUG > 6 printf( "pipe_data_get()\n" ) ;#endif#ifdef DMALLOC dmalloc_verify( 0 ) ;#endif *type=-1; *size=0; *error=0;#if DEBUG > 6 printf( "Reading type in pipe_data_get()\n" ) ;#endif snum[0] = -1 ; bread=read (fd, snum, 1); *type=((int)snum[0]);#if DEBUG > 6 printf ("pipe_data_get has read bread=%d *type=%d\n", (int)bread, *type);#endif if (bread==-1) goto readerror;#if DEBUG > 6 else { printf( "pipe_date_get actually has data!!!!!!\n" ) ; }#endif#ifdef DMALLOC dmalloc_verify( 0 ) ;#endif /* Now, go into blocking mode to make sure we can't drift out of sync. */ if (fcntl (fd, F_GETFL, &fdarg)==-1) { printf ("error while querying the file descriptor on pipe\n"); } fdarg&=~O_NONBLOCK; if (fcntl (fd, F_SETFL, fdarg)==-1) { printf ("error while setting the file descriptor on pipe\n"); } bread=read (fd, snum, 4); if (bread==-1) goto readerror; *size=assemble_long (snum);#if DEBUG > 6 printf ("pipe_data_get bread=%d *size=%d snum=%d,%d,%d,%d\n", (int)bread, (int)*size, snum[0], snum[1], snum[2], snum[3]);#endif bread = rdata = 0 ; rbsize = *size ; if( *size > 0 ) {#if DEBUG > 6 printf( "reading %d bytes from pipe.\n", *size ) ;#endif data = (char *)calloc( (*size) + 10, sizeof(char) ) ; /* Copy the data directly into the destination buffer */ while( rdata < (*size) ) { bread = read( fd, &data[rdata], rbsize ) ; if( bread >= 0 ) { rdata += bread ; rbsize = (*size) - bread ; } } } else data = NULL ;#if DEBUG > 6 printf ("pipe_data_get size=%d rdata=%d data=%s\n", (int)*size, (int)rdata, data);#endif fdarg=O_NONBLOCK; if (fcntl (fd, F_SETFL, fdarg)==-1) { insert_error (_("error while setting the file descriptor on pipe\n")); } goto done ; readerror:#if DEBUG > 6 printf( "We got a read error when reading type/length in pipe_date_get()!\n" ) ;#endif if( errno != EAGAIN ) { *error=NODATA ; fdarg=O_NONBLOCK; if (fcntl (fd, F_SETFL, fdarg)==-1) { insert_error (_("error while setting the file descriptor on pipe\n")); } } if( data ) { free( data ) ; data = NULL ; } done:#ifdef DMALLOC dmalloc_verify( 0 ) ;#endif#if DEBUG > 6 printf( "Finished with pipe_data_get()\n" ) ;#endif return data ;}/* status protocallbytes | description-------------------------------------2 | type char-------------------------------------2 | data size-------------------------------------var | data (lenght of 'data size')-------------------------------------NOTE: data size can be 0*/void pipe_parent_stat_put (int stat, char *data, int size) { int fd=tm_globs->pspipe[1]; unsigned long tmpl1=stat; unsigned long tmpl2=size; char *snum1=disassemble_long (tmpl1); char *snum2=disassemble_long (tmpl2);#ifdef DMALLOC dmalloc_verify( 0 ) ;#endif#if DEBUG > 6 printf( "pipe_parent_stat_put\n" ) ;#endif write (fd, snum1, 4);#if DEBUG > 6 printf ("stat parent put size=%d snum=%d,%d,%d,%d\n", size, snum2[0], snum2[1], snum2[2], snum2[3]);#endif write (fd, snum2, 4); if( size > 0 ) {#if DEBUG > 6 printf ("stat parent put data[0]=%d data[1]=%d data[2]=%d data[3]=%d\n", data[0], data[1], data[2], data[3]);#endif write (fd, data, size); }#ifdef DMALLOC dmalloc_verify( 0 ) ;#endif free (snum1); free (snum2); return ;}char *pipe_parent_stat_get (unsigned long *stat, unsigned long *size, int *error) { int fd=tm_globs->pspipe[0]; int rbsize ; int fdarg; unsigned long bread, rdata; char *data=NULL; char *snum=(char *)calloc(5, sizeof(char));#if DEBUG > 6 printf( "pipe_parent_stat_get\n" ) ;#endif *stat=-1; *size=0; *error=0; bread=read (fd, snum, 4); if (bread==-1) goto readerror; *stat=assemble_long (snum);#if DEBUG > 6 printf ("stat parent bread=%d *stat=%d snum=%d,%d,%d,%d\n", (int)bread, (int)*stat, snum[0], snum[1], snum[2], snum[3]);#endif /* Now, go into blocking mode to make sure we can't drift out of sync. */ if (fcntl (fd, F_GETFL, &fdarg)==-1) { insert_error ("error while querying the file descriptor on pipe\n"); } fdarg&=~O_NONBLOCK; if (fcntl (fd, F_SETFL, fdarg)==-1) { insert_error ("error while setting the file descriptor on pipe\n"); } bread=read (fd, snum, 4); if (bread==-1) goto readerror; *size=assemble_long (snum);#if DEBUG > 6 printf ("stat parent bread=%d *size=%d snum=%d,%d,%d,%d\n", (int)bread, (int)*size, snum[0], snum[1], snum[2], snum[3]);#endif if( *size > 0 ) { data = (char *)malloc( *size + 10 ) ; rbsize = *size ; bread = rdata = 0 ; /* Copy the data directly into the destination buffer */ while( rdata < (*size) ) { bread = read( fd, &data[rdata], rbsize ) ; if( bread >= 0 ) { rdata += bread ; rbsize = (*size) - bread ; } } }#ifdef DMALLOC dmalloc_verify( 0 ) ;#endif#if DEBUG > 6 printf ("stat parent rdata=%d *size=%d\n", (int)rdata, (int)*size);#endif free (snum); fdarg=O_NONBLOCK; if (fcntl (fd, F_SETFL, fdarg)==-1) { insert_error (_("error while setting the file descriptor on pipe\n")); } return data;readerror: if( errno != EAGAIN ) { *error=READERR; fdarg=O_NONBLOCK; if (fcntl (fd, F_SETFL, fdarg)==-1) { insert_error (_("error while setting the file descriptor on pipe\n")); } } if( data ) free (data); if( snum ) free (snum); return NULL;}void pipe_child_stat_put (int stat, char *data, int size) { int fd=tm_globs->cspipe[1]; unsigned long tmpl1=stat; unsigned long tmpl2=size; char *snum1=disassemble_long (tmpl1); char *snum2=disassemble_long (tmpl2);#if DEBUG > 6 printf( "--------===========**********>>>>>>>.pipe_child_stat_put\n" ) ;#ifdef DMALLOC dmalloc_verify( 0 ) ;#endif#endif#if DEBUG > 6 printf( "pipe_child_stat_put is writing data.\n" ) ;#endif write (fd, snum1, 4);#if DEBUG > 6 printf ("stat child put size=%d snum=%d,%d,%d,%d\n", (int)size, snum2[0], snum2[1], snum2[2], snum2[3]);#endif write (fd, snum2, 4); if (size>0) {#if DEBUG > 6 printf ("stat child put data[0]=%d data[1]=%d data[2]=%d data[3]=%d\n", data[0], data[1], data[2], data[3]);#endif write (fd, data, size); } free (snum1); free (snum2);#ifdef DMALLOC dmalloc_verify( 0 ) ;#endif return ;}char *pipe_child_stat_get (unsigned long *stat, unsigned long *size, int *error) { int fd=tm_globs->cspipe[0]; int rbsize ; int fdarg; unsigned long bread, rdata; char *data=NULL; char snum[5] ; *stat=-1; *size=0; *error=0;#ifdef DMALLOC dmalloc_verify( 0 ) ;#endif bread=read (fd, snum, 4);#if DEBUG > 6 printf( "pipe_child_stat_get: read() is done.\n" ) ;#endif if (bread==-1) goto readerror; *stat=assemble_long (snum);#if DEBUG > 6 printf ("stat child bread=%d *stat=%d snum=%d,%d,%d,%d\n", (int)bread, (int)*stat, snum[0], snum[1], snum[2], snum[3]);#endif bread=read (fd, snum, 4); if (bread==-1) goto readerror; *size=assemble_long (snum);#if DEBUG > 6 printf ("stat child bread=%d *size=%d snum=%d,%d,%d,%d\n", (int)bread, (int)*size, snum[0], snum[1], snum[2], snum[3]);#endif if (fcntl (fd, F_GETFL, &fdarg)==-1) { insert_error ("error while querying the file descriptor on pipe\n"); } fdarg&=~O_NONBLOCK; if (fcntl (fd, F_SETFL, fdarg)==-1) { insert_error ("error while setting the file descriptor on pipe\n"); } if( *size > 0 ) { data = (char *)malloc( *size + 10 ) ; rbsize = *size ; bread = rdata = 0 ; /* Copy the data directly into the destination buffer */ while( rdata < (*size) ) { bread = read( fd, &data[rdata], rbsize ) ; if( bread >= 0 ) { rdata += bread ; rbsize = (*size) - bread ; } } }#ifdef DMALLOC dmalloc_verify( 0 ) ;#endif#if DEBUG > 6 printf ("stat child rdata=%d *size=%d data=%d,%d,%d,%d\n", (int)rdata, (int)*size, data[0], data[1], data[2], data[3]);#endif fdarg=O_NONBLOCK; if (fcntl (fd, F_SETFL, fdarg)==-1) { insert_error (_("Error setting descriptor attributes in the pipe layer\n")); }#ifdef DMALLOC dmalloc_verify( 0 ) ;#endif return data;readerror: if( errno != EAGAIN ) { *error=READERR; fdarg=O_NONBLOCK; if (fcntl (fd, F_SETFL, fdarg)==-1) { insert_error (_("Error setting descriptor attributes in the pipe layer\n")); } }#ifdef DMALLOC dmalloc_verify( 0 ) ;#endif if( data ) free( data ) ; return NULL ;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -