void EventWorker::Run() { wxIPV4address ca; ca.Hostname(m_host); ca.Service(3000); m_clientSocket->SetNotify(wxSOCKET_CONNECTION_FLAG|wxSOCKET_LOST_FLAG|wxSOCKET_OUTPUT_FLAG|wxSOCKET_INPUT_FLAG); m_clientSocket->Notify(true); m_currentType = WorkerEvent::CONNECTING; m_doneSent = false; //wxLogMessage(wxT("EventWorker: Connecting.....")); m_clientSocket->Connect(ca,false); }
void socketStream::setUp() { // create the socket threads and wait until they are ready to accept // connections (if we called Connect() before this happens, it would fail) { wxMutexLocker lock(gs_mutex); m_writeThread = new SocketServerThread(TEST_PORT_READ, &socketStream::WriteSocket); CPPUNIT_ASSERT_EQUAL( wxCOND_NO_ERROR, gs_cond.Wait() ); m_readThread = new SocketServerThread(TEST_PORT_WRITE, &socketStream::ReadSocket); CPPUNIT_ASSERT_EQUAL( wxCOND_NO_ERROR, gs_cond.Wait() ); } m_readSocket = new wxSocketClient(ms_flags); CPPUNIT_ASSERT( m_readSocket->Connect(LocalAddress(TEST_PORT_READ)) ); m_writeSocket = new wxSocketClient(ms_flags); CPPUNIT_ASSERT( m_writeSocket->Connect(LocalAddress(TEST_PORT_WRITE)) ); }
void StreamerFrame::OnConnectDisconnect(wxCommandEvent& event){ string connect_label = "Connect"; string disconnect_label = "Disconnect"; //we connect to the client //we first check the label of the btn if(connect->GetLabel() == connect_label){ client = new wxSocketClient(wxSOCKET_WAITALL); client->Connect(ip,0); }else if(connect->GetLabel() == disconnect_label){ //we will disconnect connect->SetValue(false); connect->SetLabel("Connect"); SetStatusText("Disconnected"); } }
wxThread::ExitCode ThreadWorker::Entry() { wxIPV4address ca; ca.Hostname(m_host); ca.Service(5678); //wxLogDebug(wxT("ThreadWorker: Connecting.....")); m_clientSocket->SetTimeout(60); bool failed = false; WorkerEvent::evt_type etype = WorkerEvent::CONNECTING; if (!m_clientSocket->Connect(ca)) { wxLogError(wxT("Cannot connect to %s:%d"),ca.IPAddress().c_str(), ca.Service()); failed = true; } else { //wxLogMessage(wxT("ThreadWorker: Connected. Sending %d bytes of data"),m_outsize); etype = WorkerEvent::SENDING; WorkerEvent e(this,etype); wxGetApp().AddPendingEvent(e); int to_process = m_outsize; do { m_clientSocket->Write(m_outbuf,m_outsize); if (m_clientSocket->Error()) { wxLogError(wxT("ThreadWorker: Write error")); failed = true; } to_process -= m_clientSocket->LastCount(); //wxLogDebug(wxT("EventWorker: written %d bytes, %d bytes to do"),m_clientSocket->LastCount(),to_process); } while(!m_clientSocket->Error() && to_process != 0); if (!failed) { etype = WorkerEvent::RECEIVING; WorkerEvent e(this,etype); wxGetApp().AddPendingEvent(e); to_process = m_insize; do { m_clientSocket->Read(m_inbuf,m_insize); if (m_clientSocket->Error()) { wxLogError(wxT("ThreadWorker: Read error")); failed = true; break; } to_process -= m_clientSocket->LastCount(); //wxLogDebug(wxT("EventWorker: readed %d bytes, %d bytes to do"),m_clientSocket->LastCount(),to_process); } while(!m_clientSocket->Error() && to_process != 0); } char* outdat = (char*)m_outbuf+2; if (!failed && (memcmp(m_inbuf,outdat,m_insize) != 0)) { wxLogError(wxT("Data mismatch")); failed = true; } } //wxLogDebug(wxT("ThreadWorker: Finished")); if (!failed) { etype = WorkerEvent::DISCONNECTING; WorkerEvent e(this,etype); wxGetApp().AddPendingEvent(e); }; m_clientSocket->Close(); m_clientSocket->Destroy(); m_clientSocket = NULL; delete [] m_outbuf; delete [] m_inbuf; if (!failed) etype = WorkerEvent::DONE; WorkerEvent e(this,etype); if (failed) e.setFailed(); wxGetApp().AddPendingEvent(e); return 0; }
void EventWorker::OnSocketEvent(wxSocketEvent& pEvent) { switch(pEvent.GetSocketEvent()) { case wxSOCKET_INPUT: //wxLogDebug(wxT("EventWorker: INPUT")); do { if (m_readed == m_insize) return; //event already posted m_clientSocket->Read(m_inbuf + m_readed, m_insize - m_readed); if (m_clientSocket->Error()) { if (m_clientSocket->LastError() != wxSOCKET_WOULDBLOCK) { wxLogError(wxT("%s: read error"),CreateIdent(m_localaddr).c_str()); SendEvent(true); } } m_readed += m_clientSocket->LastCount(); //wxLogDebug(wxT("EventWorker: readed %d bytes, %d bytes to do"),m_clientSocket->LastCount(), m_insize - m_readed); if (m_readed == m_insize) { if (!memcmp(m_inbuf,m_outbuf,m_insize)) { wxLogError(wxT("%s: data mismatch"),CreateIdent(m_localaddr).c_str()); SendEvent(true); } m_currentType = WorkerEvent::DISCONNECTING; wxLogDebug(wxT("%s: DISCONNECTING"),CreateIdent(m_localaddr).c_str()); SendEvent(false); //wxLogDebug(wxT("EventWorker %p closing"),this); m_clientSocket->Close(); m_currentType = WorkerEvent::DONE; wxLogDebug(wxT("%s: DONE"),CreateIdent(m_localaddr).c_str()); SendEvent(false); } } while (!m_clientSocket->Error()); break; case wxSOCKET_OUTPUT: //wxLogDebug(wxT("EventWorker: OUTPUT")); do { if (m_written == m_outsize) return; if (m_written == 0) { m_currentType = WorkerEvent::SENDING; wxLogDebug(wxT("%s: SENDING"),CreateIdent(m_localaddr).c_str()); } m_clientSocket->Write(m_outbuf + m_written, m_outsize - m_written); if (m_clientSocket->Error()) { if (m_clientSocket->LastError() != wxSOCKET_WOULDBLOCK) { wxLogError(wxT("%s: Write error"),CreateIdent(m_localaddr).c_str()); SendEvent(true); } } m_written += m_clientSocket->LastCount(); if (m_written != m_outsize) { //wxLogDebug(wxT("EventWorker: written %d bytes, %d bytes to do"),m_clientSocket->LastCount(),m_outsize - m_written); } else { //wxLogDebug(wxT("EventWorker %p SENDING->RECEIVING"),this); m_currentType = WorkerEvent::RECEIVING; wxLogDebug(wxT("%s: RECEIVING"),CreateIdent(m_localaddr).c_str()); SendEvent(false); } } while(!m_clientSocket->Error()); break; case wxSOCKET_CONNECTION: { //wxLogMessage(wxT("EventWorker: got connection")); wxLogMessage(wxT("%s: starting writing message (2 bytes for signature and %d bytes of data to write)"),CreateIdent(m_localaddr).c_str(),m_outsize-2); if (!m_clientSocket->GetLocal(m_localaddr)) { wxLogError(_("Cannot get peer data for socket %p"),m_clientSocket); } m_currentType = WorkerEvent::SENDING; wxLogDebug(wxT("%s: CONNECTING"),CreateIdent(m_localaddr).c_str()); SendEvent(false); } break; case wxSOCKET_LOST: { wxLogError(_("%s: connection lost"),CreateIdent(m_localaddr).c_str()); SendEvent(true); } break; } }