source: TI05-delivery/trunk/src/bbftp-client-3.2.0/bbftpc/writemessage.c @ 1426

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/trunk/src/bbftp-client-3.2.0/bbftpc/writemessage.c@1426
Revision 1426, 4.0 KB checked in by spascoe, 13 years ago (diff)

Added temporary debug code.

Line 
1/*
2 * bbftpc/writemessage.c
3 * Copyright (C) 1999, 2000, 2001, 2002 IN2P3, CNRS
4 * bbftp@in2p3.fr
5 * http://doc.in2p3.fr/bbftp
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 */ 
21
22/****************************************************************************
23
24 
25 
26 writemessage.c v 1.6.0  2000/03/24     - Creation of the routine
27                v 1.7.1  2000/04/04     - Portage to IRIX (IRIX64)
28                v 1.8.7  2000/05/24     - Set the socket in non block mode
29                v 1.8.9  2000/08/08     - Add timestamp
30                v 1.8.10 2000/08/10     - Portage to Linux
31                v 1.9.0  2000/08/24     - Use configure to help port
32                v 2.0.0  2001/03/08     - Change send for write and add debug
33                v 2.0.1  2001/04/17     - Correct indentation
34
35*****************************************************************************/
36#include <bbftp.h>
37
38#include <errno.h>
39#include <stdio.h>
40#if TIME_WITH_SYS_TIME
41# include <sys/time.h>
42# include <time.h>
43#else
44# if HAVE_SYS_TIME_H
45#  include <sys/time.h>
46# else
47#  include <time.h>
48# endif
49#endif
50#include <sys/types.h>
51#include <unistd.h>
52#if HAVE_STRING_H
53# include <string.h>
54#endif
55
56#include <client.h>
57#include <client_proto.h>
58
59extern int  timestamp ;
60extern int  debug ;
61
62int writemessage(int sock,char *buffer,int msglen,int to,int fromchild) 
63{
64    int     retcode ;
65    char    ent[40] ;
66    int     nfds ;
67    fd_set  selectmask ; /* Select mask */
68    struct timeval    wait_timer;
69
70    int     msgsize ;
71    int     msgsend ;
72    int     nbsent ;
73
74    // DEBUG
75    // Do a checksum of the message and log it.
76    int i, c;
77    c = 0;
78    for (i = 0; i < msglen; i++) {
79      c += (int)buffer[i];
80    }
81    PRINTMESSAGE(stdout, CASE_NORMAL, 0, timestamp, "writemessage: %d, chksum: %d\n", msglen, c);
82
83
84    if ( fromchild == 1 ) {
85        sprintf(ent,"Child %06d : ",getpid()) ;
86    } else {
87        strcpy(ent,"") ;
88    }
89    nbsent = 0 ;
90    msgsize = msglen ;
91/*
92** start the writing loop
93*/
94    while ( nbsent < msgsize ) {
95        nfds = sysconf(_SC_OPEN_MAX) ;
96        FD_ZERO(&selectmask) ;
97        FD_SET(sock,&selectmask) ;
98        wait_timer.tv_sec  = to ;
99        wait_timer.tv_usec = 0 ;
100        if ( (retcode = select(FD_SETSIZE,0,&selectmask,0,&wait_timer) ) == -1 ) {
101            /*
102            ** Select error
103            */
104            if (debug ) 
105                PRINTMESSAGE(stderr,CASE_NORMAL,0,timestamp,"%sWrite message : Select error : MSG (%d,%d)\n",ent,msglen,nbsent) ;
106            return -1 ;
107        } else if ( retcode == 0 ) {
108            if (debug ) 
109                PRINTMESSAGE(stderr,CASE_NORMAL,0,timestamp,"%sWrite message : Time Out : MSG (%d,%d)\n",ent,msglen,nbsent) ;
110            return -1 ;
111        } else {
112            msgsend = write(sock,&buffer[nbsent],msgsize-nbsent) ;
113            if ( msgsend < 0 ) {
114                if (debug ) 
115                    PRINTMESSAGE(stderr,CASE_NORMAL,0,timestamp,"%sWrite message : Send error %d(%s) : MSG (%d,%d)\n",ent,errno,strerror(errno),msglen,nbsent) ;
116                return -1 ;
117            } else if (msgsend  == 0 ) {
118                if (debug ) 
119                    PRINTMESSAGE(stderr,CASE_NORMAL,0,timestamp,"%sWrite message : Connection breaks : MSG (%d,%d)\n",ent,msglen,nbsent) ;
120                return -1 ;
121            } else {
122                nbsent = nbsent + msgsend ;
123            }
124        }
125    }
126    return(0) ;
127}
Note: See TracBrowser for help on using the repository browser.