Click here to Skip to main content
16,009,318 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to work with Shortcut & Keyboard Pin
DaveyM699-Oct-08 8:53
professionalDaveyM699-Oct-08 8:53 
GeneralRe: How to work with Shortcut & Keyboard Pin
Pedram Behroozi9-Oct-08 22:38
Pedram Behroozi9-Oct-08 22:38 
QuestionCOM Interface Question Pin
MSBassSinger9-Oct-08 4:30
professionalMSBassSinger9-Oct-08 4:30 
AnswerRe: COM Interface Question Pin
led mike9-Oct-08 4:38
led mike9-Oct-08 4:38 
QuestionRe: VB6 COM Interface Question Pin
MSBassSinger9-Oct-08 6:17
professionalMSBassSinger9-Oct-08 6:17 
RantRe: VB6 COM Interface Question Pin
Paul Conrad9-Oct-08 7:08
professionalPaul Conrad9-Oct-08 7:08 
AnswerRe: VB6 COM Interface Question Pin
MSBassSinger9-Oct-08 7:54
professionalMSBassSinger9-Oct-08 7:54 
QuestionAll these event handlers Pin
Dewald9-Oct-08 3:56
Dewald9-Oct-08 3:56 
I think my code has just raised a OnGenerateHeadache() event Smile | :)

I'm implementing a TCP server that accepts client connections and process messages sent to it, then sends an appropriate response. Something gave me the idea that using events and delegates was a good idea but now I'm starting to wonder if it was. My code works and I will probably not change it (unless I can improve it) but I'd like to get the opinion of experts on how sound my approach is.

What got me thinking along the lines of events and delegates at first is this; data coming in over a TCP connection could come in various bursts. It's just a stream of data and the start and end of one message might not arrive in the same burst. So my TCPConnection class raises an event every time it receives data but it won't know whether that data constitutes a complete message or just the first half of a message. It simply raises an event (called DataReceived(byte[] stream)), passing the bytes received as a parameter. Whoever handles that event is responsible for checking when the message is complete.

Enters my next class, FramingEngine. My way of establishing when a message is complete is to 'frame' it with extra bytes before and after. My protocol is slightly more complex but for the sake of simplicity let's just say it works as follows. Every message will start with a 0x02 byte and end with a 0x03 byte (unless the byte is preceded by a 0x10 byte in which case it is just part of the message).

So FramingEngine has a method called DecodeIncomingData(byte[] buffer) which checks for these bytes and packs the message into an internal buffer until it receives the terminating bytes of a message. At this point the method knows that it has received a complete message and it raises an event (called MessageReceived(byte[] message)), passing the internal buffer which is stripped of all these control bytes.

Enters my next class, MessagingEngine. This is the class that understands the messages. In other words, this class is specific to my current project while TCPConnection and FramingEngine are common classes that could be used in any other project of mine that also utilizes TCP comms. So MessagingEngine has a method called HandleMessage(byte[] message) which processes a message, decides what to do with it and responds if necessary.

So basically, data coming in on the TCP connection gets passed all the way down to MessagingEngine through events. Something like this:
TCPConnection myConnection; //This is instantiated earlier, don't worry about how it's done.

FramingEngine frameEngine = new FramingEngine();
MessagingEngine messageEngine = new MessagingEngine();

myConnection.DataReceived += new TCPConnection.DataReceivedDelegate(frameEngine.DecodeIncomingData);
frameEngine.MessageReceived += new FramingEngine.FrameReceivedDelegate(messageEngine.HandleMessage);


So far so good, but now, to some of the messages (most of them actually) that MessagingEngine receives it has to respond, but is has to do so framing the responses through FramingEngine first and then ultimately sending it to TCPConnection.

The problem is that MessagingEngine has no knowledge of the TCP connection on which the data was received (and is to be sent again). It doesn't even have knowledge of FramingEngine. So, I decided to employ this mechanism of events being raised and handled to pass the message up the chain of abstraction again. As I said, it works, but I'm not sure this is good programming. What are your opinions?

So MessagingEngine when it's ready to respond, raises an event called SendMessage(byte[] message). FramingEngine has a method called EncodeOutgoingData(byte[] message) which is used to handle the event raised by MessagingEngine. In turn, EncodeOutgoingData raises an event called MessageFramed(byte[] buffer) which is handled by the TCP connection's SendData(byte[] buffer) method, like so:
messageEngine.SendMessage += new MessagingEngine.SendMessageDelegate(frameEngine.EncodeOutgoingData);
frameEngine.MessageFramed += new FramingEngine.MessageFramedDelegate(myConnection.SendData)


Would you have done it differently? Which is an elegant way of doing this?
AnswerRe: All these event handlers Pin
DaveyM699-Oct-08 4:43
professionalDaveyM699-Oct-08 4:43 
GeneralRe: All these event handlers Pin
Dewald9-Oct-08 5:11
Dewald9-Oct-08 5:11 
AnswerRe: All these event handlers Pin
led mike9-Oct-08 4:47
led mike9-Oct-08 4:47 
AnswerRe: All these event handlers Pin
S. Senthil Kumar9-Oct-08 5:00
S. Senthil Kumar9-Oct-08 5:00 
GeneralRe: All these event handlers Pin
Dewald9-Oct-08 5:20
Dewald9-Oct-08 5:20 
GeneralRe: All these event handlers Pin
S. Senthil Kumar9-Oct-08 5:34
S. Senthil Kumar9-Oct-08 5:34 
GeneralRe: All these event handlers Pin
Dewald9-Oct-08 23:02
Dewald9-Oct-08 23:02 
QuestionIncluding c# v2.0 library in a c# v1.1 library. [modified] Pin
Spoon659-Oct-08 2:40
Spoon659-Oct-08 2:40 
AnswerRe: Including c# v2.0 library in a c# v1.3 library. Pin
Pete O'Hanlon9-Oct-08 3:00
mvePete O'Hanlon9-Oct-08 3:00 
GeneralRe: Including c# v2.0 library in a c# v1.1 library. Pin
Spoon659-Oct-08 3:08
Spoon659-Oct-08 3:08 
AnswerRe: Including c# v2.0 library in a c# v1.1 library. Pin
Guffa9-Oct-08 3:12
Guffa9-Oct-08 3:12 
GeneralRe: Including c# v2.0 library in a c# v1.1 library. Pin
Spoon659-Oct-08 3:52
Spoon659-Oct-08 3:52 
GeneralRe: Including c# v2.0 library in a c# v1.1 library. Pin
Dave Kreskowiak9-Oct-08 5:24
mveDave Kreskowiak9-Oct-08 5:24 
GeneralRe: Including c# v2.0 library in a c# v1.1 library. Pin
Spoon659-Oct-08 22:11
Spoon659-Oct-08 22:11 
GeneralRe: Including c# v2.0 library in a c# v1.1 library. Pin
Spoon6513-Oct-08 22:37
Spoon6513-Oct-08 22:37 
GeneralRe: Including c# v2.0 library in a c# v1.1 library. Pin
Dave Kreskowiak14-Oct-08 1:52
mveDave Kreskowiak14-Oct-08 1:52 
AnswerRe: Including c# v2.0 library in a c# v1.1 library. Pin
Dan Neely9-Oct-08 4:06
Dan Neely9-Oct-08 4:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.