/*!
 *  Translate a IPaddress into a meaningful string
 *
 *  @param addr The address to translate
 *
 *  @return A string representing the addr e.g. localhost:1099
 */
wxString GdbServerWindow::getAddr(IPaddress addr) {
   wxString *serverAddr = new wxString();

   if (addr.IPAddress().compare(_("127.0.0.1")) == 0) {
      serverAddr->Printf(_("localhost:%u"), addr.Service());
   }
   else {
      serverAddr->Printf(_("%s:%u"), (const char *)addr.IPAddress().c_str(), addr.Service());
   }
   return *serverAddr;
}
Esempio n. 2
0
void MyFrame::OnServerEvent(wxSocketEvent& event)
{
	txtRx->AppendText(wxT("OnServerEvent: "));
	wxSocketBase *sockBase;

	switch (event.GetSocketEvent())
	{
	case wxSOCKET_CONNECTION: txtRx->AppendText(wxT("wxSOCKET_CONNECTION\n")); break;
	default: txtRx->AppendText(wxT("Unexpected event !\n")); break;
	}

	// Accept new connection if there is one in the pending
	// connections queue, else exit. We use Accept(false) for
	// non-blocking accept (although if we got here, there
	// should ALWAYS be a pending connection).

	sockBase = sock->Accept(false);

	if (sockBase)
	{
		IPaddress addr;
		if (!sockBase->GetPeer(addr))
		{
			txtRx->AppendText(wxT("New connection from unknown client accepted.\n"));
		}
		else
		{
			txtRx->AppendText(wxString::Format(wxT("New client connection from %s:%u accepted \n"),
				addr.IPAddress(), addr.Service()));
		}
	}
	else
	{
		txtRx->AppendText(wxT("Error: couldn't accept a new connection \n"));
		return;
	}

	sockBase->SetEventHandler( *this, SOCKET_ID);
	sockBase->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
	sockBase->Notify(true);

	numClients++;
	SetStatusText(wxString::Format(wxT("%d  clients connected"),numClients), 1);
}
Esempio n. 3
0
// frame constructor
MyFrame::MyFrame(const wxString& title)
: wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(390, 280), 
  wxDEFAULT_FRAME_STYLE ^ wxRESIZE_BORDER)
{
// set the frame icon
SetIcon(wxICON(sample));

#if wxUSE_MENUS
// create a menu bar
wxMenu *fileMenu = new wxMenu;

// the "About" item should be in the help menu
wxMenu *helpMenu = new wxMenu;
helpMenu->Append(Minimal_About, "&About\tF1", "Show about dialog");

fileMenu->Append(Minimal_Quit, "E&xit\tAlt-X", "Quit this program");

// now append the freshly created menu to the menu bar...
wxMenuBar *menuBar = new wxMenuBar();
menuBar->Append(fileMenu, "&File");
menuBar->Append(helpMenu, "&Help");

// ... and attach this menu bar to the frame
SetMenuBar(menuBar);
#endif // wxUSE_MENUS

#if wxUSE_STATUSBAR
// create a status bar just for fun (by default with 1 pane only)
CreateStatusBar(2);
SetStatusText("TCP server using wxWidgets");
#endif // wxUSE_STATUSBAR
txtRx = new wxTextCtrl(this, ID_TXTRX, wxT(""), wxPoint(5, 5), 
        wxSize(365, 125), wxTE_MULTILINE);

// Create the address - defaults to localhost:0 initially
IPaddress addr;
addr.AnyAddress();
addr.Service(3000);
txtRx->AppendText(wxString::Format(wxT("Creating server at %s:%u \n")
    ,addr.IPAddress(), addr.Service()));

// Create the socket
sock = new wxSocketServer(addr);

// We use IsOk() here to see if the server is really listening
if (!sock->IsOk()){
	txtRx->AppendText(wxString::Format(wxT("Could not listen at the specified port !\n")));
	return;
}

IPaddress addrReal;
if (!sock->GetLocal(addrReal)){
	txtRx->AppendText(wxString::Format(wxT("ERROR: couldn't get the address we bound to. \n")));
}
else{
	txtRx->AppendText(wxString::Format(wxT("Server listening at %s:%u \n"),
		addrReal.IPAddress(), addrReal.Service()));
}

// Setup the event handler and subscribe to connection events
sock->SetEventHandler( *this, SERVER_ID);
sock->SetNotify(wxSOCKET_CONNECTION_FLAG);
sock->Notify(true);
numClients = 0;
}
Esempio n. 4
0
void wxGISLocalServerConnection::OnSocketEvent(wxSocketEvent& event)
{
    switch(event.GetSocketEvent())
    {
        case wxSOCKET_INPUT:
            //send event to advisers
            wxLogDebug(wxT("wxGISNetServerConnection: INPUT"));
            break;
        case wxSOCKET_OUTPUT:
            wxLogDebug(wxT("wxGISNetServerConnection: OUTPUT"));
            //ProcessNetMessage();
            break;
        case wxSOCKET_CONNECTION:
            wxLogDebug(wxT("wxGISNetServerConnection: CONNECTION"));
            break;
        case wxSOCKET_LOST:
            wxLogDebug(wxT("wxGISNetServerConnection: LOST"));
            {
                IPaddress addr;
                if (!m_pSock->GetPeer(addr))
                {
                    wxLogMessage(_("User #%d is disconnected"), m_nUserId);
                }
                else
                {
                    wxLogMessage(_("User #%d from %s:%d is disconnected"), m_nUserId, addr.IPAddress().c_str(), addr.Service());
                }

                m_bIsConnected = false;
                //send bye to the app
                wxNetMessage msgin(enumGISNetCmdBye, enumGISNetCmdStUnk, enumGISPriorityHigh);
                PostEvent(new wxGISNetEvent(m_nUserId, wxGISNET_MSG, msgin));

                //as connection is lost we destroy itself
                Destroy();
            }
            break;
        default:
            break;
    }
}
Esempio n. 5
0
void wxGISLocalNetworkPlugin::OnTCPServerEvent(wxSocketEvent& event)
{
    switch(event.GetSocketEvent())
    {
        case wxSOCKET_INPUT:
            wxLogError(_("wxGISLocalNetworkPlugin: Unexpected wxSOCKET_INPUT in wxSocketServer"));
            break;
        case wxSOCKET_OUTPUT:
            wxLogError(_("wxGISLocalNetworkPlugin: Unexpected wxSOCKET_OUTPUT in wxSocketServer"));
        break;
        case wxSOCKET_CONNECTION:
        {
            // Accept new connection if there is one in the pending
            // connections queue, else exit. We use Accept(false) for
            // non-blocking accept (although if we got here, there
            // should ALWAYS be a pending connection).

            wxSocketBase* sock = m_listeningSocket->Accept(false);
            IPaddress addr;
            if (!sock->GetPeer(addr))
            {
                wxLogError(_("wxGISLocalNetworkPlugin: cannot get peer info"));
            }
            else
            {
                wxLogMessage(_("wxGISLocalNetworkPlugin: Got connection from %s:%d"), addr.IPAddress().c_str(), addr.Service());
            }

            wxGISLocalServerConnection* pSrvConn = new wxGISLocalServerConnection();
            pSrvConn->SetSocket(sock);
            m_pNetService->AddConnection(pSrvConn);
        }
        break;
        case wxSOCKET_LOST:
            wxLogError(_("wxGISLocalNetworkPlugin: Unexpected wxSOCKET_LOST in wxSocketServer"));
        break;
    }
}