home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / MiscKit1.2.6 / Source / MiscSocket.m < prev    next >
Encoding:
Text File  |  1994-05-15  |  1.7 KB  |  112 lines

  1. /*  Abstract class to act as a cover for sockets.
  2.  *
  3.  *  Copyright (c) 1994 Christopher J. Kane.
  4.  *
  5.  *  This software is subject to the terms of the MiscKit license
  6.  *  agreement. Refer to the license document included with the
  7.  *  MiscKit distribution for these terms.
  8.  *
  9.  *  Version 1.0 BETA (20 April 1994)
  10.  */
  11.  
  12. #import <misckit/MiscSocket.h>
  13. #import <sys/errno.h>
  14. extern int close(int);
  15. extern int dup(int);
  16. extern int errno;
  17.  
  18. @implementation MiscSocket
  19.  
  20. - init
  21. {
  22.     if (0 <= sock) {
  23.         int old_errno = errno;
  24.         close(sock);
  25.         errno = old_errno;
  26.     }
  27.     sock = domain = type = -1;
  28.     return self;
  29. }
  30.  
  31. - initDomain:(int)aDomain type:(int)aType
  32. {
  33.     [self init];
  34.     switch (aType) {
  35.     case MiscSOCK_RAW:
  36.         sock = socket(aDomain, SOCK_RAW, 0);
  37.         break;
  38.     case MiscSOCK_DGRAM:
  39.         sock = socket(aDomain, SOCK_DGRAM, 0);
  40.         break;
  41.     case MiscSOCK_STREAM:
  42.         sock = socket(aDomain, SOCK_STREAM, 0);
  43.         break;
  44.     default :
  45.         sock = -1;
  46.         errno = ESOCKTNOSUPPORT;
  47.     }
  48.     if (sock < 0)
  49.         return [super free];
  50.     domain = aDomain;
  51.     type = aType;
  52.     return self;
  53. }
  54.  
  55. - close
  56. {
  57.     return [self init];
  58. }
  59.  
  60. - copyFromZone:(NXZone *)zone
  61. {
  62.     MiscSocket *copy = [super copyFromZone:zone];
  63.     if (sock != -1) {
  64.         copy->sock = dup(sock);
  65.         if (copy->sock == -1)
  66.             return [copy free];
  67.     }
  68.     return copy;
  69. }
  70.  
  71. - free
  72. {
  73.     [self close];
  74.     return [super free];
  75. }
  76.  
  77. - read:(NXTypedStream *)stream
  78. {
  79.     [super read:stream];
  80.     NXReadTypes(stream, "iii", &sock, &domain, &type);
  81.     return self;
  82. }
  83.  
  84. - write:(NXTypedStream *)stream
  85. {
  86.     [super write:stream];
  87.     NXWriteTypes(stream, "iii", &sock, &domain, &type);
  88.     return self;
  89. }
  90.  
  91. - (BOOL)isClosed
  92. {
  93.     return (sock == -1 && domain == -1 && type == -1);
  94. }
  95.  
  96. - (int)domain
  97. {
  98.     return domain;
  99. }
  100.  
  101. - (int)socket
  102. {
  103.     return sock;
  104. }
  105.  
  106. - (int)type
  107. {
  108.     return type;
  109. }
  110.  
  111. @end
  112.