PeerConnectionClient(二)之Win32Socket

Win32Socket类主要成员为:

PeerConnectionClient(二)之Win32Socket_第1张图片
Win32Socket00.PNG

EventSink是用来干嘛的呢,其实他主要是为了接收socket事件用的,从图中可看出,EventSink继承自Win32Window类,Win32Window类对应着一个MFC窗口,通过Windwos API:

WSAAPI
WSAAsyncSelect(
    _In_ SOCKET s,
    _In_ HWND hWnd,
    _In_ u_int wMsg,
    _In_ long lEvent
    );

将Win32Socket的socket事件注册,当消息到来时,hWnd句柄对应的窗口便能接收到该消息。
注册的代码在:

bool Win32Socket::SetAsync(int events) {
  if (NULL == sink_) {
    CreateSink();
    ASSERT(NULL != sink_);
  }

  // start the async select
  if (WSAAsyncSelect(socket_, sink_->handle(), WM_SOCKETNOTIFY, events)
      == SOCKET_ERROR) {  //将socket_注册到sink_对应的窗口
    UpdateLastError();
    Close();
    return false;
  }

  return true;
}

消息处理函数在:

LRESULT Win32Window::WndProc(HWND hwnd, UINT uMsg,
                             WPARAM wParam, LPARAM lParam) {
  Win32Window* that = reinterpret_cast(
      ::GetWindowLongPtr(hwnd, GWLP_USERDATA));
  if (!that && (WM_CREATE == uMsg)) {
    that->wnd_ = hwnd;
  }
  if (that) {
    LRESULT result;
    bool handled = that->OnMessage(uMsg, wParam, lParam, result);  //调用自己或派生类的OnMessage()
  }
  return ::DefWindowProc(hwnd, uMsg, wParam, lParam);
}

你可能感兴趣的:(PeerConnectionClient(二)之Win32Socket)