MacSocket.h 3.2 KB

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