1#pragma once 2 3 4#ifdef __cplusplus 5extern "C" { 6#endif 7 8enum { 9 kMacSocket_TimeoutErr = -2 10}; 11 12// Since MacSocket does busy waiting, I do a callback while waiting 13 14typedef OSErr(*MacSocket_IdleWaitCallback) (void *); 15 16// Call this before anything else! 17 18OSErr MacSocket_Startup(void); 19 20// Call this to cleanup before quitting 21 22OSErr MacSocket_Shutdown(void); 23 24// Call this to allocate a "socket" (reference number is returned in 25// outSocketNum) 26// Note that inDoThreadSwitching is pretty much irrelevant right now, since I 27// ignore it 28// The inTimeoutTicks parameter is applied during reads/writes of data 29// The inIdleWaitCallback parameter specifies a callback which is called 30// during busy-waiting periods 31// The inUserRefPtr parameter is passed back to the idle-wait callback 32 33OSErr MacSocket_socket(int *outSocketNum, const Boolean inDoThreadSwitching, 34 const long inTimeoutTicks, 35 MacSocket_IdleWaitCallback inIdleWaitCallback, 36 void *inUserRefPtr); 37 38// Call this to connect to an IP/DNS address 39// Note that inTargetAddressAndPort is in "IP:port" format-- e.g. 40// 10.1.1.1:123 41 42OSErr MacSocket_connect(const int inSocketNum, char *inTargetAddressAndPort); 43 44// Call this to listen on a port 45// Since this a low-performance implementation, I allow a maximum of 1 (one!) 46// incoming request when I listen 47 48OSErr MacSocket_listen(const int inSocketNum, const int inPortNum); 49 50// Call this to close a socket 51 52OSErr MacSocket_close(const int inSocketNum); 53 54// Call this to receive data on a socket 55// Most parameters' purpose are obvious-- except maybe "inBlock" which 56// controls whether I wait for data or return immediately 57 58int MacSocket_recv(const int inSocketNum, void *outBuff, int outBuffLength, 59 const Boolean inBlock); 60 61// Call this to send data on a socket 62 63int MacSocket_send(const int inSocketNum, const void *inBuff, 64 int inBuffLength); 65 66// If zero bytes were read in a call to MacSocket_recv(), it may be that the 67// remote end has done a half-close 68// This function will let you check whether that's true or not 69 70Boolean MacSocket_RemoteEndIsClosing(const int inSocketNum); 71 72// Call this to see if the listen has completed after a call to 73// MacSocket_listen() 74 75Boolean MacSocket_ListenCompleted(const int inSocketNum); 76 77// These really aren't very useful anymore 78 79Boolean MacSocket_LocalEndIsOpen(const int inSocketNum); 80Boolean MacSocket_RemoteEndIsOpen(const int inSocketNum); 81 82// You may wish to change the userRefPtr for a socket callback-- use this to 83// do it 84 85void MacSocket_SetUserRefPtr(const int inSocketNum, void *inNewRefPtr); 86 87// Call these to get the socket's IP:port descriptor 88 89void MacSocket_GetLocalIPAndPort(const int inSocketNum, char *outIPAndPort, 90 const int inIPAndPortLength); 91void MacSocket_GetRemoteIPAndPort(const int inSocketNum, char *outIPAndPort, 92 const int inIPAndPortLength); 93 94// Call this to get error info from a socket 95 96void MacSocket_GetSocketErrorInfo(const int inSocketNum, 97 int *outSocketErrCode, 98 char *outSocketErrString, 99 const int inSocketErrStringMaxLength); 100 101 102#ifdef __cplusplus 103} 104#endif 105