| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 virtual void OnNcDestroy(); | 158 virtual void OnNcDestroy(); |
| 159 | 159 |
| 160 private: | 160 private: |
| 161 bool OnSocketNotify(UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT& result); | 161 bool OnSocketNotify(UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT& result); |
| 162 bool OnDnsNotify(WPARAM wParam, LPARAM lParam, LRESULT& result); | 162 bool OnDnsNotify(WPARAM wParam, LPARAM lParam, LRESULT& result); |
| 163 | 163 |
| 164 Win32Socket * parent_; | 164 Win32Socket * parent_; |
| 165 }; | 165 }; |
| 166 | 166 |
| 167 void Win32Socket::EventSink::Dispose() { | 167 void Win32Socket::EventSink::Dispose() { |
| 168 parent_ = NULL; | 168 parent_ = nullptr; |
| 169 if (::IsWindow(handle())) { | 169 if (::IsWindow(handle())) { |
| 170 ::DestroyWindow(handle()); | 170 ::DestroyWindow(handle()); |
| 171 } else { | 171 } else { |
| 172 delete this; | 172 delete this; |
| 173 } | 173 } |
| 174 } | 174 } |
| 175 | 175 |
| 176 bool Win32Socket::EventSink::OnMessage(UINT uMsg, WPARAM wParam, | 176 bool Win32Socket::EventSink::OnMessage(UINT uMsg, WPARAM wParam, |
| 177 LPARAM lParam, LRESULT& result) { | 177 LPARAM lParam, LRESULT& result) { |
| 178 switch (uMsg) { | 178 switch (uMsg) { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 } else { | 220 } else { |
| 221 delete this; | 221 delete this; |
| 222 } | 222 } |
| 223 } | 223 } |
| 224 | 224 |
| 225 ///////////////////////////////////////////////////////////////////////////// | 225 ///////////////////////////////////////////////////////////////////////////// |
| 226 // Win32Socket | 226 // Win32Socket |
| 227 ///////////////////////////////////////////////////////////////////////////// | 227 ///////////////////////////////////////////////////////////////////////////// |
| 228 | 228 |
| 229 Win32Socket::Win32Socket() | 229 Win32Socket::Win32Socket() |
| 230 : socket_(INVALID_SOCKET), error_(0), state_(CS_CLOSED), connect_time_(0), | 230 : socket_(INVALID_SOCKET), |
| 231 closing_(false), close_error_(0), sink_(NULL), dns_(NULL) { | 231 error_(0), |
| 232 } | 232 state_(CS_CLOSED), |
| 233 connect_time_(0), |
| 234 closing_(false), |
| 235 close_error_(0), |
| 236 sink_(nullptr), |
| 237 dns_(nullptr) {} |
| 233 | 238 |
| 234 Win32Socket::~Win32Socket() { | 239 Win32Socket::~Win32Socket() { |
| 235 Close(); | 240 Close(); |
| 236 } | 241 } |
| 237 | 242 |
| 238 bool Win32Socket::CreateT(int family, int type) { | 243 bool Win32Socket::CreateT(int family, int type) { |
| 239 Close(); | 244 Close(); |
| 240 int proto = (SOCK_DGRAM == type) ? IPPROTO_UDP : IPPROTO_TCP; | 245 int proto = (SOCK_DGRAM == type) ? IPPROTO_UDP : IPPROTO_TCP; |
| 241 socket_ = ::WSASocket(family, type, proto, NULL, NULL, 0); | 246 socket_ = ::WSASocket(family, type, proto, nullptr, 0, 0); |
| 242 if (socket_ == INVALID_SOCKET) { | 247 if (socket_ == INVALID_SOCKET) { |
| 243 UpdateLastError(); | 248 UpdateLastError(); |
| 244 return false; | 249 return false; |
| 245 } | 250 } |
| 246 if ((SOCK_DGRAM == type) && !SetAsync(FD_READ | FD_WRITE)) { | 251 if ((SOCK_DGRAM == type) && !SetAsync(FD_READ | FD_WRITE)) { |
| 247 return false; | 252 return false; |
| 248 } | 253 } |
| 249 return true; | 254 return true; |
| 250 } | 255 } |
| 251 | 256 |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 480 state_ = CS_CONNECTING; | 485 state_ = CS_CONNECTING; |
| 481 return err; | 486 return err; |
| 482 } | 487 } |
| 483 | 488 |
| 484 Win32Socket* Win32Socket::Accept(SocketAddress* out_addr) { | 489 Win32Socket* Win32Socket::Accept(SocketAddress* out_addr) { |
| 485 sockaddr_storage saddr; | 490 sockaddr_storage saddr; |
| 486 socklen_t addr_len = sizeof(saddr); | 491 socklen_t addr_len = sizeof(saddr); |
| 487 SOCKET s = ::accept(socket_, reinterpret_cast<sockaddr*>(&saddr), &addr_len); | 492 SOCKET s = ::accept(socket_, reinterpret_cast<sockaddr*>(&saddr), &addr_len); |
| 488 UpdateLastError(); | 493 UpdateLastError(); |
| 489 if (s == INVALID_SOCKET) | 494 if (s == INVALID_SOCKET) |
| 490 return NULL; | 495 return nullptr; |
| 491 if (out_addr) | 496 if (out_addr) |
| 492 SocketAddressFromSockAddrStorage(saddr, out_addr); | 497 SocketAddressFromSockAddrStorage(saddr, out_addr); |
| 493 Win32Socket* socket = new Win32Socket; | 498 Win32Socket* socket = new Win32Socket; |
| 494 if (0 == socket->Attach(s)) | 499 if (0 == socket->Attach(s)) |
| 495 return socket; | 500 return socket; |
| 496 delete socket; | 501 delete socket; |
| 497 return NULL; | 502 return nullptr; |
| 498 } | 503 } |
| 499 | 504 |
| 500 int Win32Socket::Close() { | 505 int Win32Socket::Close() { |
| 501 int err = 0; | 506 int err = 0; |
| 502 if (socket_ != INVALID_SOCKET) { | 507 if (socket_ != INVALID_SOCKET) { |
| 503 err = ::closesocket(socket_); | 508 err = ::closesocket(socket_); |
| 504 socket_ = INVALID_SOCKET; | 509 socket_ = INVALID_SOCKET; |
| 505 closing_ = false; | 510 closing_ = false; |
| 506 close_error_ = 0; | 511 close_error_ = 0; |
| 507 UpdateLastError(); | 512 UpdateLastError(); |
| 508 } | 513 } |
| 509 if (dns_) { | 514 if (dns_) { |
| 510 WSACancelAsyncRequest(dns_->handle); | 515 WSACancelAsyncRequest(dns_->handle); |
| 511 delete dns_; | 516 delete dns_; |
| 512 dns_ = NULL; | 517 dns_ = nullptr; |
| 513 } | 518 } |
| 514 if (sink_) { | 519 if (sink_) { |
| 515 sink_->Dispose(); | 520 sink_->Dispose(); |
| 516 sink_ = NULL; | 521 sink_ = nullptr; |
| 517 } | 522 } |
| 518 addr_.Clear(); | 523 addr_.Clear(); |
| 519 state_ = CS_CLOSED; | 524 state_ = CS_CLOSED; |
| 520 return err; | 525 return err; |
| 521 } | 526 } |
| 522 | 527 |
| 523 int Win32Socket::EstimateMTU(uint16_t* mtu) { | 528 int Win32Socket::EstimateMTU(uint16_t* mtu) { |
| 524 SocketAddress addr = GetRemoteAddress(); | 529 SocketAddress addr = GetRemoteAddress(); |
| 525 if (addr.IsAnyIP()) { | 530 if (addr.IsAnyIP()) { |
| 526 error_ = ENOTCONN; | 531 error_ = ENOTCONN; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 545 *mtu = PACKET_MAXIMUMS[level]; | 550 *mtu = PACKET_MAXIMUMS[level]; |
| 546 return 0; | 551 return 0; |
| 547 } | 552 } |
| 548 } | 553 } |
| 549 | 554 |
| 550 RTC_NOTREACHED(); | 555 RTC_NOTREACHED(); |
| 551 return 0; | 556 return 0; |
| 552 } | 557 } |
| 553 | 558 |
| 554 void Win32Socket::CreateSink() { | 559 void Win32Socket::CreateSink() { |
| 555 RTC_DCHECK(NULL == sink_); | 560 RTC_DCHECK(nullptr == sink_); |
| 556 | 561 |
| 557 // Create window | 562 // Create window |
| 558 sink_ = new EventSink(this); | 563 sink_ = new EventSink(this); |
| 559 sink_->Create(NULL, L"EventSink", 0, 0, 0, 0, 10, 10); | 564 sink_->Create(nullptr, L"EventSink", 0, 0, 0, 0, 10, 10); |
| 560 } | 565 } |
| 561 | 566 |
| 562 bool Win32Socket::SetAsync(int events) { | 567 bool Win32Socket::SetAsync(int events) { |
| 563 if (NULL == sink_) { | 568 if (nullptr == sink_) { |
| 564 CreateSink(); | 569 CreateSink(); |
| 565 RTC_DCHECK(NULL != sink_); | 570 RTC_DCHECK(nullptr != sink_); |
| 566 } | 571 } |
| 567 | 572 |
| 568 // start the async select | 573 // start the async select |
| 569 if (WSAAsyncSelect(socket_, sink_->handle(), WM_SOCKETNOTIFY, events) | 574 if (WSAAsyncSelect(socket_, sink_->handle(), WM_SOCKETNOTIFY, events) |
| 570 == SOCKET_ERROR) { | 575 == SOCKET_ERROR) { |
| 571 UpdateLastError(); | 576 UpdateLastError(); |
| 572 Close(); | 577 Close(); |
| 573 return false; | 578 return false; |
| 574 } | 579 } |
| 575 | 580 |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 702 error = DoConnect(address); | 707 error = DoConnect(address); |
| 703 } else { | 708 } else { |
| 704 Close(); | 709 Close(); |
| 705 } | 710 } |
| 706 | 711 |
| 707 if (error) { | 712 if (error) { |
| 708 error_ = error; | 713 error_ = error; |
| 709 SignalCloseEvent(this, error_); | 714 SignalCloseEvent(this, error_); |
| 710 } else { | 715 } else { |
| 711 delete dns_; | 716 delete dns_; |
| 712 dns_ = NULL; | 717 dns_ = nullptr; |
| 713 } | 718 } |
| 714 } | 719 } |
| 715 | 720 |
| 716 /////////////////////////////////////////////////////////////////////////////// | 721 /////////////////////////////////////////////////////////////////////////////// |
| 717 // Win32SocketServer | 722 // Win32SocketServer |
| 718 // Provides cricket base services on top of a win32 gui thread | 723 // Provides cricket base services on top of a win32 gui thread |
| 719 /////////////////////////////////////////////////////////////////////////////// | 724 /////////////////////////////////////////////////////////////////////////////// |
| 720 | 725 |
| 721 static UINT s_wm_wakeup_id = 0; | 726 static UINT s_wm_wakeup_id = 0; |
| 722 const TCHAR Win32SocketServer::kWindowName[] = L"libjingle Message Window"; | 727 const TCHAR Win32SocketServer::kWindowName[] = L"libjingle Message Window"; |
| 723 | 728 |
| 724 Win32SocketServer::Win32SocketServer(MessageQueue* message_queue) | 729 Win32SocketServer::Win32SocketServer(MessageQueue* message_queue) |
| 725 : message_queue_(message_queue), | 730 : message_queue_(message_queue), |
| 726 wnd_(this), | 731 wnd_(this), |
| 727 posted_(false), | 732 posted_(false), |
| 728 hdlg_(NULL) { | 733 hdlg_(nullptr) { |
| 729 if (s_wm_wakeup_id == 0) | 734 if (s_wm_wakeup_id == 0) |
| 730 s_wm_wakeup_id = RegisterWindowMessage(L"WM_WAKEUP"); | 735 s_wm_wakeup_id = RegisterWindowMessage(L"WM_WAKEUP"); |
| 731 if (!wnd_.Create(NULL, kWindowName, 0, 0, 0, 0, 0, 0)) { | 736 if (!wnd_.Create(nullptr, kWindowName, 0, 0, 0, 0, 0, 0)) { |
| 732 LOG_GLE(LS_ERROR) << "Failed to create message window."; | 737 LOG_GLE(LS_ERROR) << "Failed to create message window."; |
| 733 } | 738 } |
| 734 } | 739 } |
| 735 | 740 |
| 736 Win32SocketServer::~Win32SocketServer() { | 741 Win32SocketServer::~Win32SocketServer() { |
| 737 if (wnd_.handle() != NULL) { | 742 if (wnd_.handle() != nullptr) { |
| 738 KillTimer(wnd_.handle(), 1); | 743 KillTimer(wnd_.handle(), 1); |
| 739 wnd_.Destroy(); | 744 wnd_.Destroy(); |
| 740 } | 745 } |
| 741 } | 746 } |
| 742 | 747 |
| 743 Socket* Win32SocketServer::CreateSocket(int type) { | 748 Socket* Win32SocketServer::CreateSocket(int type) { |
| 744 return CreateSocket(AF_INET, type); | 749 return CreateSocket(AF_INET, type); |
| 745 } | 750 } |
| 746 | 751 |
| 747 Socket* Win32SocketServer::CreateSocket(int family, int type) { | 752 Socket* Win32SocketServer::CreateSocket(int family, int type) { |
| 748 return CreateAsyncSocket(family, type); | 753 return CreateAsyncSocket(family, type); |
| 749 } | 754 } |
| 750 | 755 |
| 751 AsyncSocket* Win32SocketServer::CreateAsyncSocket(int type) { | 756 AsyncSocket* Win32SocketServer::CreateAsyncSocket(int type) { |
| 752 return CreateAsyncSocket(AF_INET, type); | 757 return CreateAsyncSocket(AF_INET, type); |
| 753 } | 758 } |
| 754 | 759 |
| 755 AsyncSocket* Win32SocketServer::CreateAsyncSocket(int family, int type) { | 760 AsyncSocket* Win32SocketServer::CreateAsyncSocket(int family, int type) { |
| 756 Win32Socket* socket = new Win32Socket; | 761 Win32Socket* socket = new Win32Socket; |
| 757 if (socket->CreateT(family, type)) { | 762 if (socket->CreateT(family, type)) { |
| 758 return socket; | 763 return socket; |
| 759 } | 764 } |
| 760 delete socket; | 765 delete socket; |
| 761 return NULL; | 766 return nullptr; |
| 762 } | 767 } |
| 763 | 768 |
| 764 void Win32SocketServer::SetMessageQueue(MessageQueue* queue) { | 769 void Win32SocketServer::SetMessageQueue(MessageQueue* queue) { |
| 765 message_queue_ = queue; | 770 message_queue_ = queue; |
| 766 } | 771 } |
| 767 | 772 |
| 768 bool Win32SocketServer::Wait(int cms, bool process_io) { | 773 bool Win32SocketServer::Wait(int cms, bool process_io) { |
| 769 BOOL b; | 774 BOOL b; |
| 770 if (process_io) { | 775 if (process_io) { |
| 771 // Spin the Win32 message pump at least once, and as long as requested. | 776 // Spin the Win32 message pump at least once, and as long as requested. |
| 772 // This is the Thread::ProcessMessages case. | 777 // This is the Thread::ProcessMessages case. |
| 773 uint32_t start = Time(); | 778 uint32_t start = Time(); |
| 774 do { | 779 do { |
| 775 MSG msg; | 780 MSG msg; |
| 776 SetTimer(wnd_.handle(), 0, cms, NULL); | 781 SetTimer(wnd_.handle(), 0, cms, nullptr); |
| 777 // Get the next available message. If we have a modeless dialog, give | 782 // Get the next available message. If we have a modeless dialog, give |
| 778 // give the message to IsDialogMessage, which will return true if it | 783 // give the message to IsDialogMessage, which will return true if it |
| 779 // was a message for the dialog that it handled internally. | 784 // was a message for the dialog that it handled internally. |
| 780 // Otherwise, dispatch as usual via Translate/DispatchMessage. | 785 // Otherwise, dispatch as usual via Translate/DispatchMessage. |
| 781 b = GetMessage(&msg, NULL, 0, 0); | 786 b = GetMessage(&msg, nullptr, 0, 0); |
| 782 if (b == -1) { | 787 if (b == -1) { |
| 783 LOG_GLE(LS_ERROR) << "GetMessage failed."; | 788 LOG_GLE(LS_ERROR) << "GetMessage failed."; |
| 784 return false; | 789 return false; |
| 785 } else if(b) { | 790 } else if(b) { |
| 786 if (!hdlg_ || !IsDialogMessage(hdlg_, &msg)) { | 791 if (!hdlg_ || !IsDialogMessage(hdlg_, &msg)) { |
| 787 TranslateMessage(&msg); | 792 TranslateMessage(&msg); |
| 788 DispatchMessage(&msg); | 793 DispatchMessage(&msg); |
| 789 } | 794 } |
| 790 } | 795 } |
| 791 KillTimer(wnd_.handle(), 0); | 796 KillTimer(wnd_.handle(), 0); |
| 792 } while (b && TimeSince(start) < cms); | 797 } while (b && TimeSince(start) < cms); |
| 793 } else if (cms != 0) { | 798 } else if (cms != 0) { |
| 794 // Sit and wait forever for a WakeUp. This is the Thread::Send case. | 799 // Sit and wait forever for a WakeUp. This is the Thread::Send case. |
| 795 RTC_DCHECK(cms == -1); | 800 RTC_DCHECK(cms == -1); |
| 796 MSG msg; | 801 MSG msg; |
| 797 b = GetMessage(&msg, NULL, s_wm_wakeup_id, s_wm_wakeup_id); | 802 b = GetMessage(&msg, nullptr, s_wm_wakeup_id, s_wm_wakeup_id); |
| 798 { | 803 { |
| 799 CritScope scope(&cs_); | 804 CritScope scope(&cs_); |
| 800 posted_ = false; | 805 posted_ = false; |
| 801 } | 806 } |
| 802 } else { | 807 } else { |
| 803 // No-op (cms == 0 && !process_io). This is the Pump case. | 808 // No-op (cms == 0 && !process_io). This is the Pump case. |
| 804 b = TRUE; | 809 b = TRUE; |
| 805 } | 810 } |
| 806 return (b != FALSE); | 811 return (b != FALSE); |
| 807 } | 812 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 837 max_messages_to_process > 0 && message_queue_->Get(&msg, 0, false); | 842 max_messages_to_process > 0 && message_queue_->Get(&msg, 0, false); |
| 838 --max_messages_to_process) { | 843 --max_messages_to_process) { |
| 839 message_queue_->Dispatch(&msg); | 844 message_queue_->Dispatch(&msg); |
| 840 } | 845 } |
| 841 | 846 |
| 842 // Anything remaining? | 847 // Anything remaining? |
| 843 int delay = message_queue_->GetDelay(); | 848 int delay = message_queue_->GetDelay(); |
| 844 if (delay == -1) { | 849 if (delay == -1) { |
| 845 KillTimer(wnd_.handle(), 1); | 850 KillTimer(wnd_.handle(), 1); |
| 846 } else { | 851 } else { |
| 847 SetTimer(wnd_.handle(), 1, delay, NULL); | 852 SetTimer(wnd_.handle(), 1, delay, nullptr); |
| 848 } | 853 } |
| 849 } | 854 } |
| 850 | 855 |
| 851 bool Win32SocketServer::MessageWindow::OnMessage(UINT wm, WPARAM wp, | 856 bool Win32SocketServer::MessageWindow::OnMessage(UINT wm, WPARAM wp, |
| 852 LPARAM lp, LRESULT& lr) { | 857 LPARAM lp, LRESULT& lr) { |
| 853 bool handled = false; | 858 bool handled = false; |
| 854 if (wm == s_wm_wakeup_id || (wm == WM_TIMER && wp == 1)) { | 859 if (wm == s_wm_wakeup_id || (wm == WM_TIMER && wp == 1)) { |
| 855 ss_->Pump(); | 860 ss_->Pump(); |
| 856 lr = 0; | 861 lr = 0; |
| 857 handled = true; | 862 handled = true; |
| 858 } | 863 } |
| 859 return handled; | 864 return handled; |
| 860 } | 865 } |
| 861 | 866 |
| 862 } // namespace rtc | 867 } // namespace rtc |
| OLD | NEW |