home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD2.bin / bbs / comm / amitcp-3.0ß2.lha / AmiTCP / src / util / letnet / sender.c < prev    next >
C/C++ Source or Header  |  1994-05-02  |  3KB  |  151 lines

  1. /* $Id: sender.c,v 1.5 1994/05/02 19:48:30 jraja Exp $
  2.  *
  3.  * sender.c --- letnet sender code
  4.  *
  5.  * Author: Pekka Pessi <Pekka.Pessi@hut.fi>
  6.  *
  7.  * Copyright (c) 1993 Pekka Pessi
  8.  *                    All rights reserved
  9.  *
  10.  * Created      : Tue Mar 23 20:10:33 1993 ppessi
  11.  * Last modified: Mon May 24 05:54:55 1993 ppessi
  12.  *
  13.  * $Log: sender.c,v $
  14.  * Revision 1.5  1994/05/02  19:48:30  jraja
  15.  * Added return 0; to the exitcode().
  16.  *
  17.  * Revision 1.4  1993/08/06  08:59:59  jraja
  18.  * Changed required bsdsocket.library version to 2.
  19.  *
  20.  * Revision 1.3  1993/05/26  23:45:32  ppessi
  21.  * Experiment with pr_ExitCode; some resource allocation bugs fixed.
  22.  *
  23.  * Revision 1.2  1993/05/23  17:57:57  ppessi
  24.  * Changed ID handling.
  25.  *
  26.  * Revision 1.1  93/05/18  00:59:07  ppessi
  27.  * Initial revision
  28.  * 
  29.  */
  30.  
  31. #define SocketBase childSocketBase
  32.  
  33. struct Library *childSocketBase;
  34.  
  35. #ifdef AMIGA
  36. #if __SASC
  37. #include <proto/dos.h>
  38. #include <clib/exec_protos.h>
  39. #include <pragmas/exec_pragmas.h>
  40. #include <proto/socket.h>
  41. #elif __GNUC__
  42. #include <inline/socket.h>
  43. #include <inline/exec.h>
  44. #else
  45. #include <clib/socket_protos.h>
  46. #endif
  47. #endif /* AMIGA */
  48.  
  49. #include <errno.h>
  50. #include <netdb.h>
  51.  
  52. #include <sys/param.h>
  53. #include <sys/socket.h>
  54. #include <sys/ioctl.h>
  55. #include <netinet/in.h>
  56.  
  57. #include <signal.h>
  58.  
  59. #include <dos/dos.h>
  60. #include <exec/execbase.h>
  61. #include <exec/memory.h>
  62.  
  63. #include "letnet.h"
  64.  
  65. SAVEDS ASM LONG exitcode(REG(d0) LONG status, REG(d1) LONG exitmessage)
  66. {
  67.   ((struct SocketMessage *)exitmessage)->sm_retval = status;
  68.   ReplyMsg((struct Message *)exitmessage);
  69.   return 0;
  70. }
  71.  
  72. int
  73. sender(int s)
  74. {
  75.   int m = 0, n = 0, istty;
  76.   int retval = 0;
  77.   char *cb = NULL;
  78.  
  79.   BPTR InFh = Input();
  80.  
  81.   if (InFh) {
  82.     cb = AllocMem(SENDBUFLEN, MEMF_PUBLIC);
  83.     if (!cb) {
  84.       PrintNetFault(Errno(), "Sender");
  85.       retval = 2;
  86.       goto Return;
  87.     }
  88.  
  89.     istty = IsInteractive(InFh);
  90.  
  91.     do {
  92.       if (SetSignal(0L, 0L) & SIGBREAKF_CTRL_C) goto Return;
  93.       
  94.       if (istty) {
  95.     /* Wait a char for a second */
  96.     if (!WaitForChar(InFh, 1000000)) continue;
  97.     n = 1;
  98.       } else { 
  99.     n = SENDBUFLEN;
  100.       }
  101.  
  102.       m = Read(InFh, cb, n);
  103.  
  104.       if (m == -1) {
  105.     PrintFault(IoErr(), "Read");
  106.     retval = 2;
  107.     goto Return;
  108.       }
  109.  
  110.       if (send(s, cb, m, 0) != m) {
  111.     PrintNetFault(Errno(), "send");
  112.     retval = 2;
  113.     goto Return;
  114.       }
  115.     } while(m == n);        /* DOS EOF from Read() */
  116.   }
  117.  
  118.  Return:
  119.   shutdown(s, 1);        /* no more sends */
  120.   if (cb) FreeMem(cb, SENDBUFLEN);
  121.  
  122.   return retval;
  123. }
  124.  
  125. /* 
  126.  * This is generic daemon startup code
  127.  */
  128. SAVEDS LONG
  129. do_sender(void) 
  130. {
  131.   int s;
  132.   int retval = 1;
  133.  
  134.   struct SocketMessage *exitm = (struct SocketMessage*)
  135.     ((struct Process*)FindTask(NULL))->pr_ExitData; 
  136.  
  137.   if (SocketBase = OpenLibrary("bsdsocket.library", 2L)) {
  138.     s = ObtainSocket(exitm->sm_id, PF_INET, SOCK_STREAM, NULL);
  139.     if (s != -1) {
  140.       retval = sender(s);
  141.     } else {
  142.       PrintNetFault(Errno(), "ObtainSocket");
  143.     }
  144.     CloseLibrary(SocketBase);
  145.   }
  146.  
  147.   Flush(Output());
  148.   return retval;
  149. }
  150.  
  151.