You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
3.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. // Copyright 2008 Google Inc. All Rights Reserved.
  2. //
  3. // MacAsyncSocket is a kind of AsyncSocket. It only creates sockets
  4. // of the TCP type, and does not (yet) support listen and accept. It works
  5. // asynchronously, which means that users of this socket should connect to
  6. // the various events declared in asyncsocket.h to receive notifications about
  7. // this socket.
  8. #ifndef TALK_BASE_MACASYNCSOCKET_H__
  9. #define TALK_BASE_MACASYNCSOCKET_H__
  10. #include <CoreFoundation/CoreFoundation.h>
  11. #include "talk/base/asyncsocket.h"
  12. #include "talk/base/nethelpers.h"
  13. namespace talk_base {
  14. class MacBaseSocketServer;
  15. class MacAsyncSocket : public AsyncSocket, public sigslot::has_slots<> {
  16. public:
  17. MacAsyncSocket(MacBaseSocketServer* ss, int family);
  18. virtual ~MacAsyncSocket();
  19. bool valid() const { return source_ != NULL; }
  20. // Socket interface
  21. virtual SocketAddress GetLocalAddress() const;
  22. virtual SocketAddress GetRemoteAddress() const;
  23. virtual int Bind(const SocketAddress& addr);
  24. virtual int Connect(const SocketAddress& addr);
  25. virtual int Send(const void* buffer, size_t length);
  26. virtual int SendTo(const void* buffer, size_t length,
  27. const SocketAddress& addr);
  28. virtual int Recv(void* buffer, size_t length);
  29. virtual int RecvFrom(void* buffer, size_t length, SocketAddress* out_addr);
  30. virtual int Listen(int backlog);
  31. virtual MacAsyncSocket* Accept(SocketAddress* out_addr);
  32. virtual int Close();
  33. virtual int GetError() const;
  34. virtual void SetError(int error);
  35. virtual ConnState GetState() const;
  36. virtual int EstimateMTU(uint16* mtu);
  37. virtual int GetOption(Option opt, int* value);
  38. virtual int SetOption(Option opt, int value);
  39. // For the MacBaseSocketServer to disable callbacks when process_io is false.
  40. void EnableCallbacks();
  41. void DisableCallbacks();
  42. protected:
  43. void OnResolveResult(SignalThread* thread);
  44. int DoConnect(const SocketAddress& addr);
  45. private:
  46. // Creates an async socket from an existing bsd socket
  47. MacAsyncSocket(MacBaseSocketServer* ss, int family, int native_socket);
  48. // Attaches the socket to the CFRunloop and sets the wrapped bsd socket
  49. // to async mode
  50. void Initialize(int family);
  51. // Translate the SocketAddress into a CFDataRef to pass to CF socket
  52. // functions. Caller must call CFRelease on the result when done.
  53. static CFDataRef CopyCFAddress(const SocketAddress& address);
  54. // Callback for the underlying CFSocketRef.
  55. static void MacAsyncSocketCallBack(CFSocketRef s,
  56. CFSocketCallBackType callbackType,
  57. CFDataRef address,
  58. const void* data,
  59. void* info);
  60. MacBaseSocketServer* ss_;
  61. CFSocketRef socket_;
  62. int native_socket_;
  63. CFRunLoopSourceRef source_;
  64. int current_callbacks_;
  65. bool disabled_;
  66. int error_;
  67. ConnState state_;
  68. AsyncResolver* resolver_;
  69. DISALLOW_EVIL_CONSTRUCTORS(MacAsyncSocket);
  70. };
  71. } // namespace talk_base
  72. #endif // TALK_BASE_MACASYNCSOCKET_H__