Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

The network Event Sender/Receiver in C#

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Post Reply
miljbee
Experienced User
Posts: 146
Joined: Fri Mar 27, 2009 1:29 pm
Location: Orl├®ans, France

The network Event Sender/Receiver in C#

Post by miljbee »

Hello,

I am currently rewritting some part of my Home Automation System with C#.

I needed bidirectionnal dialog with EG.

So I wrote the network event receiver and sender in C#.
They will enable to exchange events in both directions between EG and your own C# App.


It might not be the cleannest code you would expect since I haven't a lot of experience with C#. I learn it by myself, and it isn't always easy. Also, you won't find much comments in the code, but I think it's simple enough for you to understand !

Regards
Attachments
EG_NetSendrRcvr.zip
(43.82 KiB) Downloaded 528 times
miljbee
TCP Events : A Better Network Event Sender/Receiver Plugin.
The Network Event Sender/Receiver in C#
Get events in EG from Google Calendar.
jwka
Posts: 24
Joined: Sun Nov 28, 2010 8:45 pm

Re: The network Event Sender/Receiver in C#

Post by jwka »

Hey miljbee,

could you provide a little bit of documentation aside? Being not a c# programmer, I have no idea how I could utilize your stuff in eg.

Thanks
jwka
miljbee
Experienced User
Posts: 146
Joined: Fri Mar 27, 2009 1:29 pm
Location: Orl├®ans, France

Re: The network Event Sender/Receiver in C#

Post by miljbee »

Hello,

You might find this code useful if you need to develop some functionality outside EG with C#, but still need to have you C# prog communicate with EG.

The provided code implements a network sender and a network receiver in C#. It is ready to be included in any C# Code.
It speaks threw the network with the network event receiver plugin provided with eg, and also with the network event sender plugin also provided with eg (see the plugins list)

Here is how to use it :
You will find an EG_Network_Event_Receiver_Sender class. This class implements both the server and the client. It means that you can send event to eg with it and also that eg can send event to your C# App.

When you create a new EG_Network_Event_Receiver_Sender object, the server starts automatically :

Code: Select all

EG_Network_Event_Receiver_Sender eg = new EG_Network_Event_Receiver_Sender("*:25632", "password", false);
The parameters are :
  • "localIp:Port" : localIp is the Ip Adress on which the server will listen. if you put *, it will listen on any interface of your computer.
    "password" : password is the password needed to connect to your server
    false : specify if your server supports enduring events or not (the network event sender/receiver of eg always generate enduring events, but in a manner that is useless. It's up to you to decide if you need it or not).
Once the server is started, if a client sends him an eg event, it will fire an event (I mean a C# event). To have your code react to this event, you need to subscribe to it.

This is done by writting this :

Code: Select all

eg.Event_FromNetworkEventSender += new EG_Network_Event_Receiver_Sender.Event_FromNetworkEventSender_Handler(eg_Event_FromNetworkEventSender);
When you write this, you tell C# that when the server receives an event from an eg client, it shoud run eg_Event_FromNetworkEventSender
So you need to write an eg_Event_FromNetworkEventSender method :

Code: Select all

        static void eg_Event_FromNetworkEventSender(object sender, NetWorkEventReceiver_EventArgs e)
        {
            Console.WriteLine("Event : " + e.name);
            foreach (string k in e.payload)
            {
                Console.WriteLine("  payload = " + k);
            }
            Console.WriteLine("");
        }
In this example, the event is just printed to the console, but you might code whatever you need here. It's up to you to decide. You get the name of the event in e.name and the payload in e.payload.

The EG_Network_Event_Receiver_Sender also implements a client.
Once you have created you EG_Network_Event_Receiver_Sender object, you can just call :

Code: Select all

eg.SendEventToNetworkEventReceiver("127.0.0.1:25632", "password", "testEvent", new string[3] { "1", "2", "12" })
This will actually send testEvent to the server that listen on 127.0.0.1:25632. The last param is the payload which will be send with the event. It must be a string array.
If all went fine, this method will return "success". Otherwise, it will return a string that indicates where things gets wrong :
"Error while trying to receive the \"accept\"" which might reveal a password problem.


If you directly run my code without modifying it, it will :
  • Create the server :

    Code: Select all

                EG_Network_Event_Receiver_Sender eg = new EG_Network_Event_Receiver_Sender("*:25632", "password", false);
    
    Then subscribe to the events that the server might fire :

    Code: Select all

                eg.EnduringEvent_FromNetworkEventSender_Start += new EG_Network_Event_Receiver_Sender.EnduringEvent_FromNetworkEventSender_Start_Handler(eg_EnduringEvent_FromNetworkEventSender_Start);
                eg.EnduringEvent_FromNetworkEventSender_End += new EG_Network_Event_Receiver_Sender.EnduringEvent_FromNetworkEventSender_End_Handler(eg_EnduringEvent_FromNetworkEventSender_End);
                eg.Event_FromNetworkEventSender += new EG_Network_Event_Receiver_Sender.Event_FromNetworkEventSender_Handler(eg_Event_FromNetworkEventSender);
    
    Then wait a few miliseconds to let the server start

    Code: Select all

                System.Threading.Thread.Sleep(500);
    
    Then send and event to the server which has just started and report any error :

    Code: Select all

                if ("success" != (status = eg.SendEventToNetworkEventReceiver("127.0.0.1:25632", "password", "testEvent", new string[3] { "1", "2", "12" })))
                {
                    Console.WriteLine(status);
                }
    
    Since we created a server that doesn't support enduring events (false parameter) the server will only fire Event_FromNetworkEventSender. When this event is fired, we told the server to run

    Code: Select all

            static void eg_Event_FromNetworkEventSender(object sender, NetWorkEventReceiver_EventArgs e)
            {
                Console.WriteLine("Event : " + e.name);
                foreach (string k in e.payload)
                {
                    Console.WriteLine("  payload = " + k);
                }
                Console.WriteLine("");
            }
    
    So the event that we have just sent is simply printed to the console.

    The demo is over, so the server is stopped properly before the program terminates :

    Code: Select all

                eg.StopLocalNetworkEventReceiver();
    
I hope these short explanations will help you. Let me know if you need more informations.

Regards,
miljbee
TCP Events : A Better Network Event Sender/Receiver Plugin.
The Network Event Sender/Receiver in C#
Get events in EG from Google Calendar.
lspatricio
Posts: 5
Joined: Sat Jul 04, 2015 3:42 am

Re: The network Event Sender/Receiver in C#

Post by lspatricio »

Hi there,

are you familiar with Java programming as well? Can you please implement the Network Event Sender in Java? I posted a query here: viewtopic.php?f=10&t=7274 regarding what I wanted to accomplish.

Thanks.
Post Reply