c# - Processing incoming messages from UdpClient.BeginReceive() -
okay, first stack overflow question please sense free suggest improve ways inquire or should include next time. of time can google answers 1 little trickier...
i writing windows application in c# listens on udp port , process incoming udp messages. more specifically, using udpclient
class , listening using beginreceive method. receive callback in turn firing it's own message received event , resetting udp client again. "messagereceived" event subsequently handled processor object.
i thought pretty smart until manager posed questions me such as:
how many messages can receive @ 1 time? happens if more that? are messages queued or lost if processing takes long? what happen if processing takes long?
we cannot losing messages because lastly 1 still processing, , can't building until scheme crashes because it's out of memory either. hear (and rightfully so) sort of verification there deterministic way deal "storm" of messages. unfortunately, have no thought go answer. have included think relevant code below.
so:
does know answers questions above? where research find these answers and/or patterns follow sort of thing? what kind of tools utilize watch processing debugging/performance profiling?
if have made huge error in design, should sort out (i.e. introduce queue, utilize thread pool etc)?
public void receivecallback(iasyncresult ar) { //cast item listener udplistener listener = (udplistener)ar.asyncstate; //if supposed listening, info socket //listen false during shutdown if (listen) { //the seek grab placed within hear loop if there error in processing //recover , hear again. may cause more exceptions can sure not // stop listening without knowing seek { //address , port external scheme ipendpoint ep = listener.endpoint; //get info async read byte[] receivebytes = listener.client.endreceive(ar, ref ep); //reset socket hear 1 time again listener.client.beginreceive(new asynccallback(receivecallback), listener); //execute event pass external components message heartbeateventargs hea = new heartbeateventargs(datetime.now, ep, receivebytes); onheartbeatreceived(hea); //ack external scheme heartbeatacknowledgement(new ipendpoint(ep.address, ep.port), receivebytes); } grab (exception e) { log.error(e.message); //reset socket hear 1 time again } } }
listner wrapper around udpclient
. follows:
#region udpclient wrapper (udplistener) /// <summary> /// udplistener used command asynchronous processing of udpclient object. /// </summary> public class udplistener { /// <summary> /// ipendpoint on take connection. set "any". /// </summary> public ipendpoint endpoint { get; set; } /// <summary> /// socket based client object used communication /// </summary> public udpclient client { get; set; } public udplistener(int port) { endpoint = new ipendpoint(ipaddress.any, port); client = new udpclient(endpoint); } } #endregion
thanks,
dinsdale
if losing messages concern, udp isn't you. udp guarantees if message arrives, complete. not guarantee message order or delivery. in other words, if client sends 2 messages, might receive them out of order, or first, or lastly (or none @ all). if need guarantee delivery , order, utilize tcp instead (tcp comes it's own set of guarantees , gotchas).
with regard how many messages can process, have upper limit. if aren't processing messages faster arriving, queued, either in application layer or in udp networking layer. 1 time network buffers full, network interface begin throwing away messages.
c# asynchronous .net-4.5 udpclient