MacSocket.h 2.9 KB

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