There's two threads:
============================================================
Thread 1 : Main Thread:
This is the main GUI thread. The main GUI thread displays a dialog box. On this dialog box, there is a wxListCtrl which displays information sniffed from the network. For simplicity, I have a global variable which keeps track of the memory address of the main dialog box object:
Code: Select all
Dialog_Main *g_p_dlgmain;
This is the network packet sniffer thread. This thread runs in a loop sniffing packets from the network. Each time a packet that meets certain criteria is encountered, the contents of the wxListCtrl on the main dialog box need to be altered.
============================================================
Currently I've got this working fine using wxMutexGuiEnter/wxMutexGuiLeave, but I want to use AddPendingEvent instead... but I don't know what to do.
First of all, let me say that I'm using wxFormbuilder to design my GUI, and I have wxFormbuilder set to "Connect()" the events instead of using an event table. (I don't know if this prohibits me from using an even table for my custom events???)
There's three kinds of notification that my Sniffer thread has to make to the main GUI thread. The Sniffer makes these notifications by calling member functions which belong to the main dialog box object. These member functions deal with editing the contents of the wxListCtrl. Here they are:
Code: Select all
void Dialog_Main::Add_MAC_to_list(uintMAC);
void Dialog_Main::Update_IP_List(MAC_Data const *);
void Dialog_Main::Add_Port_to_list(MAC_Data const *,uintIP,uintPort);
1) The type "uintMAC" is just a typedef for uint_fast64_t
2) The type "MAC_Data" is a struct containing members which give all sorts of information about a MAC address
3) The types "uintIP" and "uintPort" are just typedef's (uint_fast32_t/uint_fast16_t)
From my Sniffer Thread, I invoke these member functions as follows:
Code: Select all
if ( we sniffed an interesting MAC address )
{
wxMutexGuiEnter();
g_p_dlgmain->Add_MAC_to_list(mac);
wxMutexGuiLeave();
}
if ( we sniffed an interesting IP address )
{
wxMutexGuiEnter();
g_p_dlgmain->Update_IP_List(p_mac_data);
wxMutexGuiLeave();
}
if ( we sniffed an interesting Port number )
{
wxMutexGuiEnter();
g_p_dlgmain->Add_Port_to_list(p_mac_data,ip,port);
wxMutexGuiLeave();
}
OK so now I want to move away from using wxMutexGuiEnter, and I want to do things properly by using AddPendingEvent.
From what I understand, I need to create three custom events (corresponding to my 3 different kinds of notification). I've searched the web about how to do this but I've found pages that show 5 different ways of doing it. To be honest it's a bit daunting at first... I don't know what to do.
Can someone please help me out by posting minimalistic code for getting this done? I figure I have to create 3 custom wxEvent objects, something like:
Code: Select all
class MyEvent_Add_MAC_to_list : public wxEvent {
public:
uintMAC mac;
};
class MyEvent_Update_IP_List : public wxEvent {
public:
MAC_Data const *p;
};
class MyEven_Port_to_list : public wxEvent {
public:
MAC_Data const *p;
uintIP ip;
uintPort port;
};
Code: Select all
if ( we sniffed an interesting MAC address )
{
MyEvent_Add_MAC_to_list x;
x.mac = mac;
g_p_dialogmain->GetEventHandler()->AddPendingEvent(x);
}
if ( we sniffed an interesting IP address )
{
MyEvent_Update_IP_List x;
x.p = p_mac_data;
g_p_dialogmain->GetEventHandler()->AddPendingEvent(x);
}
if ( we sniffed an interesting Port number )
{
MyEven_Port_to_list x;
x.p = p_mac_data;
x.ip = ip;
x.port = port;
g_p_dialogmain->GetEventHandler()->AddPendingEvent(x);
}
I've no idea how to "connect" the events in my main thread.
Can someone please help me out and post minimalistic code, both for my main thread and for the sniffer thread.
Thanks a lot for your time.