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