Example #1
0
void WinEDA_DrawFrame::OnSockRequest(wxSocketEvent& evt)
/********************************************************/
/* Fonction appelee a chaque demande d'un client
*/
{
    size_t len;
    wxSocketBase *sock = evt.GetSocket();

    switch (evt.GetSocketEvent())
    {
    case wxSOCKET_INPUT:
        sock->Read(server_ipc_buffer,1);
        len = sock->Read(server_ipc_buffer+1,IPC_BUF_SIZE-2).LastCount();
        server_ipc_buffer[len+1] = 0;
        if(RemoteFct ) RemoteFct(server_ipc_buffer);
        break;

    case wxSOCKET_LOST:
        return;
        break;

    default:
        wxPrintf( wxT("WinEDA_DrawFrame::OnSockRequest() error: Invalid event !"));
        break;
    }
}
Example #2
0
/* Function called on every client request.
 */
void EDA_DRAW_FRAME::OnSockRequest( wxSocketEvent& evt )
{
    size_t        len;
    wxSocketBase* sock = evt.GetSocket();

    switch( evt.GetSocketEvent() )
    {
    case wxSOCKET_INPUT:
        sock->Read( client_ipc_buffer, 1 );

        if( sock->LastCount() == 0 )
            break;                    // No data, occurs on opening connection

        sock->Read( client_ipc_buffer + 1, IPC_BUF_SIZE - 2 );
        len = 1 + sock->LastCount();
        client_ipc_buffer[len] = 0;
        ExecuteRemoteCommand( client_ipc_buffer );
        break;

    case wxSOCKET_LOST:
        return;
        break;

    default:
        wxPrintf( wxT( "EDA_DRAW_FRAME::OnSockRequest() error: Invalid event !" ) );
        break;
    }
}
Example #3
0
void FaceView::OnSocketEvent(wxSocketEvent& event)
{
	wxSocketBase *sock = event.GetSocket();
	static int test = 0;
	// Now we process the event
	switch(event.GetSocketEvent())
	{
		case wxSOCKET_INPUT:
		{
			wxString msg;
			msg.sprintf(_T("input id: %d"), ++test);
			sock->SaveState();
//			sock->SetNotify(wxSOCKET_LOST_FLAG);
			wxLogStatus(msg, 0);
			ServerThread* pThread = new ServerThread(/*this, */m_pApp, sock);
			pThread->Create();
			pThread->Run();
			sock->RestoreState();

			//sock->SetNotify(wxSOCKET_LOST_FLAG | wxSOCKET_INPUT_FLAG);
			break;
		}
		case wxSOCKET_LOST:
		{
			if(!m_SocketIdHash.erase(sock))
			{
				//sth weird going on, LOG IT!
			}
			sock->Destroy();
			wxLogStatus(wxT("Remote client connection closed."));
			break;
		}
		default: ;
	}
}
Example #4
0
// Handle Commands from debugger (and lost connections)
void wxLuaDebuggerwxSocketServer::OnSocketEvent(wxSocketEvent& event)
{
    wxSocketBase *sock = event.GetSocket();

    // Now we process the event
    switch(event.GetSocketEvent())
    {
        case wxSOCKET_INPUT:
        {
            // We disable input events, so that the test doesn't trigger
            // wxSocketEvent again.
            sock->SetNotify(wxSOCKET_LOST_FLAG);

            unsigned char debugEvent = 0; // wxLuaDebuggeeEvents_Type
            if (m_acceptedSocket->ReadCmd(debugEvent))
                HandleDebuggeeEvent(debugEvent);

            // Enable input events again.
            sock->SetNotify(wxSOCKET_LOST_FLAG | wxSOCKET_INPUT_FLAG);
            break;
        }
        case wxSOCKET_LOST:
        {
            m_acceptedSocket->Destroy();
            delete m_acceptedSocket;
            m_acceptedSocket = NULL;
            break;
        }
        default:
            // Error
            break;
    }
}
Example #5
0
void CProxyEventHandler::ProxySocketHandler(wxSocketEvent& event)
{
	CProxySocket *sock = dynamic_cast<CProxySocket *>(event.GetSocket());
	if (sock) {
		sock->m_proxyStateMachine->Schedule(event.GetSocketEvent());
	} else {
		// we're doomed :)
	}
	sock->m_proxyStateMachine->Clock();
}
Example #6
0
void CServerConsole::OnSocketEvent(wxSocketEvent& event){
	wxSocketBase *sock = event.GetSocket();
	switch(event.GetSocketEvent()){
		case wxSOCKET_LOST:{
			Log(wxString::FromAscii("Client disconnected"));
			sock->Destroy();
			break;
		}
		case wxSOCKET_INPUT:{
			// Disable input events, not to trigger wxSocketEvent again
			sock->SetNotify(wxSOCKET_LOST_FLAG);

			ClientHandshake	handshake;
			sock->ReadMsg(&handshake, sizeof(handshake));
			if (sock->Error()){
				Log(wxString::FromAscii("Error receiving client message"));
				sock->Close();
				return;
			}

                        Log(wxString::FromAscii("Got a handshake"));

			int iClientID = handshake.id;
			if ((iClientID == -1) || ((iClientID > 0) && !IsClientIDValid(iClientID))){
				iClientID = CreateNewClient(iClientID);
			}

			ServerReply	reply;
			reply.id = iClientID;
			reply.status = 1;

			if (!IsClientIDValid(iClientID))
				reply.status = -1;

			sock->WriteMsg(&reply, sizeof(reply));
			if (sock->Error()){
				Log(wxString::FromAscii("Error sending a reply"));
				return;
			}
			Log(wxString::FromAscii("Sent a reply"));

			if (!IsClientIDValid(iClientID))
				return;

			ReceiveClientCommand(iClientID, sock);

			// Enable input events again.
			sock->SetNotify(wxSOCKET_LOST_FLAG | wxSOCKET_INPUT_FLAG);

			break;
		}
		default:
			wxASSERT(0);
	}
}
void wxScopeServerDialog::OnSocketEvent(wxSocketEvent& event)
{
  wxString s = _("OnSocketEvent: ");
  wxSocketBase *sock = event.GetSocket();
/*
  // First, print a message
  switch(event.GetSocketEvent())
  {
    case wxSOCKET_INPUT : s.Append(_("wxSOCKET_INPUT\n")); break;
    case wxSOCKET_LOST  : s.Append(_("wxSOCKET_LOST\n")); break;
    default             : s.Append(_("Unexpected event !\n")); break;
  }
  Tx_ServerLog->AppendText(s);
*/

  // Now we process the event
  switch(event.GetSocketEvent())
  {
    case wxSOCKET_INPUT:
    {
      // We disable input events, so that the test doesn't trigger
      // wxSocketEvent again.
      sock->SetNotify(wxSOCKET_LOST_FLAG);

      if (!m_server->HandleINPUT(sock)) // delegate input handling to class
         Tx_ServerLog->AppendText(_("HandleINPUT failed\n"));

      // Enable input events again.
      sock->SetNotify(wxSOCKET_LOST_FLAG | wxSOCKET_INPUT_FLAG);
      break;
    }
    case wxSOCKET_LOST:
    {
      m_numClients--;

      // Destroy() should be used instead of delete wherever possible,
      // due to the fact that wxSocket uses 'delayed events' (see the
      // documentation for wxPostEvent) and we don't want an event to
      // arrive to the event handler (the frame, here) after the socket
      // has been deleted. Also, we might be doing some other thing with
      // the socket at the same time; for example, we might be in the
      // middle of a test or something. Destroy() takes care of all
      // this for us.

      Tx_ServerLog->AppendText(_("Closing socket.\n\n"));
      sock->Notify(false);
      sock->Destroy();
      break;
    }
    default: ;
  }

}
Example #8
0
void tracelogApp::OnSocketEvent(wxSocketEvent& event)
{
    wxSocketBase *sock = event.GetSocket();
    if (wxSOCKET_LOST == event.GetSocketEvent()) {
        sock->Notify(false);
        std::map<wxSocketBase *, wxSocketOutputStream *>::iterator it = m_mClients.find(sock);
        if (m_mClients.end() != it) {
            delete it->second;
            m_mClients.erase(it);
        }
        sock->Destroy();
    }
}
Example #9
0
void creeperFrame::SocketEvn(wxSocketEvent& event)/*{{{*/
{
	wxSocketBase *client = event.GetSocket();
	wxSocketInputStream in(*client);
	wxStringOutputStream out(&creeperSocketData);

	if(in.IsOk())
	{
		in.Read(out);
		wxMessageBox(creeperSocketData);
		client->Close();
		SetStatusText(_("Connect Closed!"));
	}
}/*}}}*/
Example #10
0
/* Function called when a connection is requested by a client.
 */
void EDA_DRAW_FRAME::OnSockRequestServer( wxSocketEvent& evt )
{
    wxSocketBase*   sock2;
    wxSocketServer* server = (wxSocketServer*) evt.GetSocket();

    sock2 = server->Accept();

    if( sock2 == NULL )
        return;

    sock2->Notify( true );
    sock2->SetEventHandler( *this, ID_EDA_SOCKET_EVENT );
    sock2->SetNotify( wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG );
}
Example #11
0
void WinEDA_DrawFrame::OnSockRequestServer(wxSocketEvent& evt)
/**************************************************************/
/* fonction appelée lors d'une demande de connexion d'un client
*/
{
    wxSocketBase *sock2;
    wxSocketServer *server = (wxSocketServer *) evt.GetSocket();

    sock2 = server->Accept();
    if (sock2 == NULL) return;

    sock2->Notify(TRUE);
    sock2->SetEventHandler(*this, ID_EDA_SOCKET_EVENT);
    sock2->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
}
Example #12
0
void kitchenFrame::OnSocketEvent(wxSocketEvent& event)
{
    wxSocketBase *sock = event.GetSocket();
    switch(event.GetSocketEvent())
    {
    case wxSOCKET_CONNECTION:
    {
//    wxMessageBox(_("Connected!"));
        break;
    }

    case wxSOCKET_INPUT:
    {

        sock->SetNotify(wxSOCKET_LOST_FLAG);

        unsigned char c;
        sock->Read(&c, 1);

        switch (c)
        {
        case 0xBE:
            readFromDining(sock);
            break; //this means we are receiving something from dining room f.e. new order
            //  case 0xCE: Test2(sock); break;
            //  case 0xDE: Test3(sock); break;
        }

        sock->SetNotify(wxSOCKET_LOST_FLAG | wxSOCKET_INPUT_FLAG);
        break;
    }
    case wxSOCKET_LOST:
    {

//      wxMessageBox(_("DisConnected!"));
        StatusBar1->SetStatusText(_("Lost connection."),1);
//      sock->Destroy();
        sock->Close();
        break;
    }
    default:
        ;
    }
}
Example #13
0
void PSocketServer::OnSocketEvent(wxSocketEvent &event)
{
    wxCharBuffer buf(32*1024);
    wxSocketBase *sock = event.GetSocket();
    switch (event.GetSocketEvent()) {
        case wxSOCKET_INPUT:
            sock->Read(buf.data(), 32*1024);
            sock->Write(buf, 32*1024);
            text->AppendText(buf);
            text->AppendText("\n");
//            sock->Destroy();
            break;
        case wxSOCKET_LOST:
            this->checkSocketStatus(sock);
            wxLogMessage("socket lost!");
            sock->Destroy();
            break;
            
        default:
            break;
    }
}
Example #14
0
void wxTCPEventHandler::Server_OnRequest(wxSocketEvent &event)
{
  wxSocketServer *server = (wxSocketServer *) event.GetSocket();
  if (!server) {		/* No server, Then exit */
	  return ;
  }
  wxTCPServer *ipcserv = (wxTCPServer *) server->GetClientData();

  // This socket is being deleted; skip this event
  if (!ipcserv)
    return;

  if (event.GetSocketEvent() != wxSOCKET_CONNECTION)
    return;

  // Accept the connection, getting a new socket
  wxSocketBase *sock = server->Accept();
  if (!sock) {		/* No socket, no glory */
	  return ;
  }
  if (!sock->Ok())
  {
    sock->Destroy();
    return;
  }

  wxSocketStream *stream     = new wxSocketStream(*sock);
  wxDataInputStream *codeci  = new wxDataInputStream(*stream);
  wxDataOutputStream *codeco = new wxDataOutputStream(*stream);

  int msg;
  msg = codeci->Read8();

  if (msg == IPC_CONNECT)
  {
    wxString topic_name;
    topic_name = codeci->ReadString();

    wxTCPConnection *new_connection =
         (wxTCPConnection *)ipcserv->OnAcceptConnection (topic_name);

    if (new_connection)
    {
      if (new_connection->IsKindOf(CLASSINFO(wxTCPConnection)))
      {
        // Acknowledge success
        codeco->Write8(IPC_CONNECT);
        new_connection->m_topic = topic_name;
        new_connection->m_sock = sock;
        new_connection->m_sockstrm = stream;
        new_connection->m_codeci = codeci;
        new_connection->m_codeco = codeco;
        sock->SetEventHandler(*gs_handler, _CLIENT_ONREQUEST_ID);
        sock->SetClientData(new_connection);
        sock->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
        sock->Notify(true);
        return;
      }
      else
      {
        delete new_connection;
        // and fall through to delete everything else
      }
    }
  }

  // Something went wrong, send failure message and delete everything
  codeco->Write8(IPC_FAIL);

  delete codeco;
  delete codeci;
  delete stream;
  sock->Destroy();
}
Example #15
0
void wxTCPEventHandler::Client_OnRequest(wxSocketEvent &event)
{
  wxSocketBase *sock = event.GetSocket();
  if (!sock) {		/* No socket, no glory */
    return ;
  }
  wxSocketNotify evt = event.GetSocketEvent();
  wxTCPConnection *connection = (wxTCPConnection *)(sock->GetClientData());

  // This socket is being deleted; skip this event
  if (!connection)
    return;

  wxDataInputStream *codeci;
  wxDataOutputStream *codeco;
  wxSocketStream *sockstrm;
  wxString topic_name = connection->m_topic;
  wxString item;

  // We lost the connection: destroy everything
  if (evt == wxSOCKET_LOST)
  {
    sock->Notify(false);
    sock->Close();
    connection->OnDisconnect();
    return;
  }

  // Receive message number.
  codeci = connection->m_codeci;
  codeco = connection->m_codeco;
  sockstrm = connection->m_sockstrm;
  int msg = codeci->Read8();

  switch (msg)
  {
  case IPC_EXECUTE:
  {
    wxChar *data;
    size_t size;
    wxIPCFormat format;

    format = (wxIPCFormat)codeci->Read8();
    size = codeci->Read32();
    
    data = connection->GetBufferAtLeast( size );
    wxASSERT_MSG(data != NULL,
                 _T("Buffer too small in wxTCPEventHandler::Client_OnRequest") );
    sockstrm->Read(data, size);

    connection->OnExecute (topic_name, data, size, format);

    break;
  }
  case IPC_ADVISE:
  {
    wxChar *data;
    size_t size;
    wxIPCFormat format;

    item = codeci->ReadString();
    format = (wxIPCFormat)codeci->Read8();
    size = codeci->Read32();
    data = connection->GetBufferAtLeast( size );
    wxASSERT_MSG(data != NULL,
                 _T("Buffer too small in wxTCPEventHandler::Client_OnRequest") );
    sockstrm->Read(data, size);

    connection->OnAdvise (topic_name, item, data, size, format);

    break;
  }
  case IPC_ADVISE_START:
  {
    item = codeci->ReadString();

    bool ok = connection->OnStartAdvise (topic_name, item);
    if (ok)
      codeco->Write8(IPC_ADVISE_START);
    else
      codeco->Write8(IPC_FAIL);

    break;
  }
  case IPC_ADVISE_STOP:
  {
    item = codeci->ReadString();

    bool ok = connection->OnStopAdvise (topic_name, item);
    if (ok)
      codeco->Write8(IPC_ADVISE_STOP);
    else
      codeco->Write8(IPC_FAIL);

    break;
  }
  case IPC_POKE:
  {
    wxIPCFormat format;
    size_t size;
    wxChar *data;

    item = codeci->ReadString();
    format = (wxIPCFormat)codeci->Read8();
    size = codeci->Read32();
    data = connection->GetBufferAtLeast( size );
    wxASSERT_MSG(data != NULL,
                 _T("Buffer too small in wxTCPEventHandler::Client_OnRequest") );
    sockstrm->Read(data, size);

    connection->OnPoke (topic_name, item, data, size, format);

    break;
  }
  case IPC_REQUEST:
  {
    wxIPCFormat format;

    item = codeci->ReadString();
    format = (wxIPCFormat)codeci->Read8();

    int user_size = -1;
    wxChar *user_data = connection->OnRequest (topic_name, item, &user_size, format);

    if (user_data)
    {
      codeco->Write8(IPC_REQUEST_REPLY);

      if (user_size == -1)
        user_size = (wxStrlen(user_data) + 1) * sizeof(wxChar);    // includes final NUL

      codeco->Write32(user_size);
      sockstrm->Write(user_data, user_size);
    }
    else
      codeco->Write8(IPC_FAIL);

    break;
  }
  case IPC_DISCONNECT:
  {
    sock->Notify(false);
    sock->Close();
    connection->SetConnected(false);
    connection->OnDisconnect();
    break;
  }
  default:
    codeco->Write8(IPC_FAIL);
    break;
  }
}
/*!
 *  Handler for Socket events (after connection)
 *
 *  - Only expects:
 *    - wxSOCKET_INPUT - New data
 *    - wxSOCKET_LOST  - Connection dropped
 *
 *  @param event Event to handle
 */
void GdbServerWindow::OnSocketEvent(wxSocketEvent& event) {

   if (event.GetSocket() != clientSocket) {
      statusTextControl->AppendText(_("Event from unknown socket\n"));
      return;
   }
   // Now we process the event
   switch(event.GetSocketEvent()) {
      case wxSOCKET_INPUT: {
         if (clientSocket != NULL) {
            clientSocket->SetNotify(wxSOCKET_LOST_FLAG);
         }
         if (deferredOpen) {
            // Open on first access after socket creation
            deferredOpen = false;

            USBDM_ErrorCode rc = shared->initBdm();
            if (rc != BDM_RC_OK) {
               reportError("BDM Open failed, reason: ", M_FATAL, rc);
               statusTextControl->AppendText(_("BDM Open failed\n"));
               Logging::print("GdbServerWindow::OnSocketEvent() - BDM Open failed\n");
               dropConnection();
               return;
            }

            gdbInOut = new GdbInOutWx(clientSocket, statusTextControl);
            GdbCallback cb = GdbMessageWrapper::getCallBack(this);

            rc = gdbHandlerInit(gdbInOut, *shared->getCurrentDevice(), cb);
            if (rc != BDM_RC_OK) {
               reportError("GDB Handler initialisation failed, reason: ", M_FATAL, rc);
               Logging::print("GdbServerWindow::OnSocketEvent() - GDB Handler initialisation failed\n");
               dropConnection();
               return;
            }
            statusTextControl->AppendText(_("BDM Open OK\n"));
         }
         // Triggers read of socket
         const GdbPacket *packet;
         USBDM_ErrorCode rc = BDM_RC_OK;
         do {
            // Process packets from GDB until idle
            packet = gdbInOut->getGdbPacket();
            if (packet != NULL) {
               rc = doGdbCommand(packet);
               if (rc != BDM_RC_OK) {
                  statusTextControl->AppendText(wxString(USBDM_GetErrorString(rc),wxConvUTF8));
               }
            }
         } while ((packet != NULL) && (rc == BDM_RC_OK) && !deferredFail);

         if (deferredFail) {
            // A fatal error was reported - drop connection
            Logging::print("GdbServerWindow::OnSocketEvent() - deferredFail\n");
            dropConnection();
         }
         else {
            // Poll target immediately (also adjusts polling rate)
            pollTarget();
         }
         if (clientSocket != NULL) {
            clientSocket->SetNotify(wxSOCKET_LOST_FLAG | wxSOCKET_INPUT_FLAG);
         }
         break;
      }
      case wxSOCKET_LOST: {
         serverState = listening;
         Logging::print("GdbServerWindow::OnSocketEvent() - wxSOCKET_LOST\n");
         dropConnection();
         break;
      }
      default:
         statusTextControl->AppendText(_("Unexpected event on socket!\n"));
         break;
   }
   UpdateStatusBar();
}
Example #17
0
void MyFrame::OnSocketEvent(wxSocketEvent& event)
{
	txtRx->AppendText(wxT("OnSocketEvent: "));
	wxSocketBase *sockBase = event.GetSocket();

	// First, print a message
	switch (event.GetSocketEvent())
	{
	case wxSOCKET_INPUT: txtRx->AppendText(wxT("wxSOCKET_INPUT\n")); break;
	case wxSOCKET_LOST: txtRx->AppendText(wxT("wxSOCKET_LOST\n")); break;
	default: txtRx->AppendText(wxT("Unexpected event !\n")); break;
	}

	// Now we process the event
	switch (event.GetSocketEvent())
	{
	case wxSOCKET_INPUT:
	{
		// We disable input events, so that the test doesn't trigger
		// wxSocketEvent again.
		sockBase->SetNotify(wxSOCKET_LOST_FLAG);

		// Receive data from socket and send it back. We will first
		// get a byte with the buffer size, so we can specify the
		// exact size and use the wxSOCKET_WAITALL flag. Also, we
		// disabled input events so we won't have unwanted reentrance.
		// This way we can avoid the infamous wxSOCKET_BLOCK flag.

		sockBase->SetFlags(wxSOCKET_WAITALL);		

		// Read the size @ first byte
		unsigned char len;
		sockBase->Read(&len, 1);
		char buf[256];
		// Read the message
		wxUint32 lenRd = sockBase->Read(buf, len).LastCount();
		if (!lenRd)		{
			txtRx->AppendText(wxT("Failed to read message.\n"));
			return;
		}
		else {
			txtRx->AppendText(wxString::Format(wxT("Read %d bytes.\n"), lenRd));
		}

		txtRx->AppendText(wxString::Format(wxT("Rx: %s \n"),wxString::FromUTF8(buf, len)));

		// Write it back
		len = 2;
		buf[0] = 'O';
		buf[1] = 'K';
		sockBase->Write(&len,1);
		sockBase->Write(buf, len);
		txtRx->AppendText("Tx: " + wxString::From8BitData(buf, len) + "\n");
		// Enable input events again.
		sockBase->SetNotify(wxSOCKET_LOST_FLAG | wxSOCKET_INPUT_FLAG);
		break;
	}
	case wxSOCKET_LOST:
	{
		numClients--;

		// Destroy() should be used instead of delete wherever possible,
		// due to the fact that wxSocket uses 'delayed events' (see the
		// documentation for wxPostEvent) and we don't want an event to
		// arrive to the event handler (the frame, here) after the socket
		// has been deleted. Also, we might be doing some other thing with
		// the socket at the same time; for example, we might be in the
		// middle of a test or something. Destroy() takes care of all
		// this for us.

		txtRx->AppendText(wxT("Deleting socket.\n"));
		sockBase->Destroy();
		break;
	}
	default:;
	}

	SetStatusText(wxString::Format(wxT("%d  clients connected"), numClients), 1);
}