X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/a324a7bccf4bda8f4f2bf09daee8104cae953cee..0045dee3f4992cfb5226e74338a9b354d32fffbe:/samples/wxsocket/client.cpp diff --git a/samples/wxsocket/client.cpp b/samples/wxsocket/client.cpp index feb050d03a..4d14e9cb94 100644 --- a/samples/wxsocket/client.cpp +++ b/samples/wxsocket/client.cpp @@ -48,6 +48,7 @@ class MyFrame: public wxFrame DECLARE_CLASS(MyFrame) public: MyClient *sock; + int m_good; MyFrame(void); virtual ~MyFrame(); @@ -57,6 +58,7 @@ public: void OnQuitApp(wxCommandEvent& evt); void OnExecOpenConnection(wxCommandEvent& evt); void OnExecCloseConnection(wxCommandEvent& evt); + void OnSocketEvent(wxSocketEvent& evt); void UpdateStatus(); void Download(wxInputStream *input); @@ -86,6 +88,7 @@ const int SKDEMO_TEST2 = 104; const int SKDEMO_CLOSE = 105; const int SKDEMO_TEST3 = 106; const int ID_TEST_CLOSE = 107; +const int SKDEMO_SCK = 108; IMPLEMENT_APP(MyApp) @@ -140,7 +143,7 @@ MyFrame::MyFrame(): sock = new MyClient(); sock->SetFlags((wxSocketBase::wxSockFlags) (wxSocketBase::WAITALL | wxSocketBase::SPEED)); sock->frame = this; - sock->SetNotify(GSOCK_LOST_FLAG); + sock->SetNotify(wxSOCKET_LOST_FLAG); CreateStatusBar(2); UpdateStatus(); } @@ -166,8 +169,8 @@ void MyFrame::OnExecOpenConnection(wxCommandEvent& WXUNUSED(evt)) "Connect ...", "localhost"); addr.Hostname(hname); addr.Service(3000); - sock->SetNotify(0); - sock->Connect(addr, TRUE); + sock->Connect(addr, FALSE); + sock->WaitOnConnect(10); sock->SetFlags(wxSocketBase::NONE); if (!sock->IsConnected()) wxMessageBox("Can't connect to the specified host", "Alert !"); @@ -188,8 +191,23 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(SKDEMO_QUIT, MyFrame::OnQuitApp) EVT_MENU(SKDEMO_CONNECT, MyFrame::OnExecOpenConnection) EVT_MENU(SKDEMO_CLOSE, MyFrame::OnExecCloseConnection) + EVT_SOCKET(SKDEMO_SCK, MyFrame::OnSocketEvent) END_EVENT_TABLE() +class MyFrameSocketTimer: public wxTimer { + public: + void Notify() { + *m_var = 0; + } + + int *m_var; +}; + +void MyFrame::OnSocketEvent(wxSocketEvent& evt) +{ + m_good = 1; +} + void MyFrame::OnCloseTest(wxCommandEvent& evt) { wxButton *button = (wxButton *)evt.GetEventObject(); @@ -220,7 +238,7 @@ void MyFrame::OnExecTest1(wxCommandEvent& WXUNUSED(evt)) if (!sock->IsConnected()) return; - wxDialog *dlgbox = new wxDialog(this, -1, "Test 1", wxDefaultPosition, wxSize(414, 250)); + wxDialog *dlgbox = new wxDialog(this, -1, "Test 1", wxDefaultPosition, wxSize(414, 280)); wxTextCtrl *text_win = new wxTextCtrl(dlgbox, -1, "", wxPoint(0, 0), wxSize(400, 200), wxTE_MULTILINE); @@ -259,7 +277,44 @@ void MyFrame::OnExecTest1(wxCommandEvent& WXUNUSED(evt)) sock->Close(); UpdateStatus(); } else - text_win->WriteText("done\nTest 1 passed !\n"); + text_win->WriteText("done\nTest 1A passed !\n"); + + /* No 2 */ + sock->SetEventHandler(*this, SKDEMO_SCK); + sock->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG); + sock->Notify(TRUE); + text_win->WriteText("Test 1B: sending bytes to the server\n"); + if (!sock->IsData()) + text_win->WriteText("No data to read yet (this is OK)\n"); + + wxYield(); + sock->Write((char *)buf, wxStrlen(buf)+1); + text_win->WriteText("Waiting for incoming bytes (timeout = 2 sec) ..."); + wxYield(); + + m_good = 2; + + MyFrameSocketTimer timer; + + timer.m_var = &m_good; + timer.Start(2000, TRUE); + + while (m_good == 2) + wxYield(); + + if (!m_good) { + text_win->WriteText("timeout ! Failed.\n"); + sock->Close(); + UpdateStatus(); + } else + text_win->WriteText("event ! (no timeout).\n"); + + if (sock->IsData()) + text_win->WriteText("Data is available, as expected...\n"); + + sock->Read((char *)buf2, wxStrlen(buf)+1); + + text_win->WriteText("Success!\n"); dlgbox->Layout(); dlgbox->ShowModal(); @@ -274,26 +329,31 @@ void MyFrame::OnExecTest1(wxCommandEvent& WXUNUSED(evt)) void MyFrame::Download(wxInputStream *input) { wxProgressDialog progress("Downloading ...", "0% downloaded"); - wxBufferedInputStream buf_in(*input); wxFileOutputStream f_out("test.url"); - - size_t file_size = input->StreamSize(); size_t downloaded; - int BUFSIZE = (file_size > 100) ? (file_size / 100) : file_size; - int bytes_read = BUFSIZE; + int BUFSIZE, bytes_read; + size_t file_size; wxString message; int percents; char *buf; -// TODO: Support for streams which don't support StreamSize - + if (input->GetSize() == (size_t)-1) { + file_size = (size_t)-1; + bytes_read = BUFSIZE = 10240; + } else { + file_size = input->GetSize(); + if (file_size > 10240) + bytes_read = BUFSIZE = file_size / 1024; + else + bytes_read = BUFSIZE = 1024; + } buf = new char[BUFSIZE]; downloaded = 0; bytes_read = BUFSIZE; while (downloaded < file_size && bytes_read != 0) { - bytes_read = buf_in.Read(buf, BUFSIZE).LastRead(); + bytes_read = input->Read(buf, BUFSIZE).LastRead(); f_out.Write(buf, bytes_read); downloaded += bytes_read;