custom Socket wrapper help Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
mario
Earned a small fee
Earned a small fee
Posts: 11
Joined: Fri Jan 20, 2006 3:06 am

custom Socket wrapper help

Post by mario »

i am trying to make a bit of a wrapper class for the socket so that i can basically just define the socket class and have the events embeded in my class(mainly for portability between different apps i write)
this compiles fine and i can send fine but i cant get the OnSocketEvent
to fire.

any suggestions or reasons as to why it cant work would be helpful

ive tried it

Code: Select all

   
    wxEvtHandler* pHandler = wxTheApp;
    m_sc = new socks(pHandler);
and just

Code: Select all

    m_sc = new socks();

Code: Select all


#include <wx/socket.h>
#include <wx/wx.h>
enum{
    SOCKET_ID
};

class socks:public  wxSocketClient, wxEvtHandler{
    public:
        void OnSocketEvent(class wxSocketEvent& evt);
        socks();
        socks(wxEvtHandler *e);

    private:
        wxEvtHandler *m_evt;
        wxSocketBase *m_socket;
        void ReadLoop();
        wxString m_buffer, full_message;
        DECLARE_EVENT_TABLE()
};


BEGIN_EVENT_TABLE( socks, socks)
  EVT_SOCKET(SOCKET_ID, socks::OnSocketEvent)
END_EVENT_TABLE()

socks::socks():m_socket(){
    m_evt=new wxEvtHandler();

     m_evt->Connect(SOCKET_ID,wxEVT_SOCKET,wxSocketEventHandler( socks::OnSocketEvent),NULL,this);


    m_socket->SetEventHandler(*((wxEvtHandler*)this), SOCKET_ID);
    m_socket->Notify(true); // We're ready to recieve client socket events


}
socks::socks(wxEvtHandler *e):m_socket(){
    m_evt=e;
    m_socket= new wxSocketBase();

    m_evt->Connect(SOCKET_ID,wxEVT_SOCKET,wxSocketEventHandler( socks::OnSocketEvent),NULL,this);

    m_socket->SetEventHandler(*m_evt, SOCKET_ID);
    m_socket->Notify(true); // We're ready to recieve client socket events
}

void socks::OnSocketEvent(class wxSocketEvent& evt){
  wxString s = _("OnSocketEvent: ");
  wxSocketBase *sc=evt.GetSocket();
wxMessageBox(s,_("Test"));
exit(0);
  switch(evt.GetSocketEvent())
  {
    case wxSOCKET_INPUT      : s.Append(_("wxSOCKET_INPUT\n"));ReadLoop() ; break;
    case wxSOCKET_OUTPUT     : s.Append(_("wxSOCKET_OUTPUT\n")); break;
    case wxSOCKET_LOST       : s.Append(_("wxSOCKET_LOST\n")); break;
    case wxSOCKET_CONNECTION : s.Append(_("wxSOCKET_CONNECTION\n")); break;
    default                  : s.Append(_("Unexpected event !\n")); break;
  }
}
void socks::ReadLoop(){
//my way of getting the input 
//already have this working fine
}
upCASE
Moderator
Moderator
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Post by upCASE »

Hi!
Before calling Notify(), you should specify which socket events you want to be notified of by using wxSocketBase::SetNotify().
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda
mario
Earned a small fee
Earned a small fee
Posts: 11
Joined: Fri Jan 20, 2006 3:06 am

Post by mario »

i previously had that in there and just attempted to place it back and it still doesnt set off onSocketEvent

im really sorta lost as to what i did wrong
upCASE
Moderator
Moderator
Posts: 3176
Joined: Mon Aug 30, 2004 6:55 am
Location: Germany, Cologne

Post by upCASE »

Hi!
I just noticed another thing:

Code: Select all

BEGIN_EVENT_TABLE( socks, socks) 
The second parameter of that macro should hold the BASE class, not the class itself.
In your case this would have to be wxEvtHandler. If your compiler gives an error about wrong references, try changing

Code: Select all

class socks:public  wxSocketClient, wxEvtHandler
to

Code: Select all

class socks:public  wxEvtHandler, wxSocketClient
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4

"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda
mario
Earned a small fee
Earned a small fee
Posts: 11
Joined: Fri Jan 20, 2006 3:06 am

Post by mario »

Thanks for all the insite UpCase

Unfortuneatley still nothing as far as triggering when input arives

Code: Select all

BEGIN_EVENT_TABLE( socks, wxEvtHandler)
  EVT_SOCKET(SOCKET_ID, socks::OnSocketEvent)
END_EVENT_TABLE()

socks::socks():m_socket(){
    m_evt=new wxEvtHandler();
    m_evt->Connect(SOCKET_ID,wxEVT_SOCKET,wxSocketEventHandler( socks::OnSocketEvent),NULL,this);
    m_socket->SetEventHandler(*((wxEvtHandler*)this), SOCKET_ID);
    // Recieve input events, which means the socket is ready to read/write
    m_socket->SetNotify(wxSOCKET_INPUT_FLAG|wxSOCKET_OUTPUT_FLAG|wxSOCKET_CONNECTION_FLAG|wxSOCKET_LOST_FLAG);
    m_socket->Notify(true); // We're ready to recieve client socket events
}
is it possible its firing and the wxMessageBox(s,_("Test")); that i have as a debug is just not apearing for some reason(i would think it would crash the program if it couldnt pop up correctly)
mario
Earned a small fee
Earned a small fee
Posts: 11
Joined: Fri Jan 20, 2006 3:06 am

Post by mario »

how about has anybody else try something similar before and have some code i can use as refrence?
toxicBunny
Super wx Problem Solver
Super wx Problem Solver
Posts: 424
Joined: Tue Jul 12, 2005 8:44 pm
Location: Alabama, USA

Post by toxicBunny »

I do something similar where I have a class derived from wxEvtHandler and a wxSocketClient member variable. My event table looks about the same as yours, but this is all I do to initialize the socket.

Code: Select all

sock = new wxSocketClient();
// Setup the event handler and subscribe to most events
sock->SetEventHandler(*this, -1);
sock->SetNotify(wxSOCKET_CONNECTION_FLAG |
                        wxSOCKET_INPUT_FLAG |
                        wxSOCKET_LOST_FLAG);
sock->Notify(true);
In this code, sock is a member variable that is a pointer to a wxSocketClient object. This works for me without any problems.

If your class is derived from wxEvtHandler, there's no need for the m_evt object that you're using. Also, why do you have a wxSocketBase variable if your class is derived from wxSocketClient? You probably don't need to use both Connect() and SetEventHandler().

-Scott
wxMSW 2.6.2, VS 2002, 2003 and 2005, Code::Blocks and mingw, Windows XP Pro
mario
Earned a small fee
Earned a small fee
Posts: 11
Joined: Fri Jan 20, 2006 3:06 am

Post by mario »

Not Exactly the Method i had hoped to use as i now need to write functions to overload all the current network functions but in the long run it will probably give me the same result if not a actually making it cleaner code in hte long run

none the less thank you for your help upCASE and toxicBunny i will post the full end wrapper code later after i debug a bit
(if there is a better place to post snippets beside this forum please tell me)
leio
Can't get richer than this
Can't get richer than this
Posts: 802
Joined: Mon Dec 27, 2004 10:46 am
Location: Estonia, Tallinn
Contact:

Post by leio »

I am inheriting my connection class from both wxSocketClient and wxEvtHandler. So I have a is-a relationship to wxSocketClient, not a has-a one.
Just noting here, in case it might be helpful to someone, that we had to do the deriving as follows in wxMUD:

Code: Select all

class Connection : public wxEvtHandler, public wxSocketClient
If it was the other way around (wxSocketClient before wxEvtHandler), then VS2003 was complaining about possibly generating bad code, or whatever similarily crazy. Turning it around fixed that. Don't ask me why.
Hopefully noting that might be useful to someone.
Compilers: gcc-3.3.6, gcc-3.4.5, gcc-4.0.2, gcc-4.1.0 and MSVC6
OS's: Gentoo Linux, WinXP; WX: CVS HEAD

Project Manager of wxMUD - http://wxmud.sf.net/
Developer of wxGTK;
gtk+ port maintainer of OMGUI - http://www.omgui.org/
Post Reply