home *** CD-ROM | disk | FTP | other *** search
/ Network CD 2 / Network CD - Volume 2.iso / programs / internet / tcp / amitcp / amitcp-src-22.lha / AmiTCP-2.2 / src / util / letnet / sender.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-12  |  2.8 KB  |  147 lines

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