private void SendMessage(Socket socket, IPEndPoint remoteEP, Message message) { ParameterizedThreadStart udpListenerThreadStart = new ParameterizedThreadStart(UdpSenderWorkMethod); UdpSenderThread = new Thread(udpListenerThreadStart); UdpSenderThread.IsBackground = true; UdpSenderThread.Name = "UdpSenderThread"; UdpSenderThread.Priority = ThreadPriority.Normal; UdpSenderWorkMethodParams udpSenderWorkMethodParams = new UdpSenderWorkMethodParams(socket, remoteEP, message); UdpSenderThread.Start(udpSenderWorkMethodParams); }
private void NotifyFailedTransmission(UdpSenderWorkMethodParams udpSenderWorkMethodParams) { MsgHeaderData msgHeaderData = udpSenderWorkMethodParams.Message.HeaderData; Console.WriteLine("Transmission failed for {0} message - RefNo : {1}", msgHeaderData.MsgDataType, msgHeaderData.RefNo); }
private void UdpSenderWorkMethod(object obj) { if ((obj is UdpSenderWorkMethodParams) == false) { return; } UdpSenderWorkMethodParams udpSenderWorkMethodParams = (UdpSenderWorkMethodParams)obj; if (udpSenderWorkMethodParams.Socket == null || udpSenderWorkMethodParams.RemoteEP == null || udpSenderWorkMethodParams.Message == null) { return; } try { byte[] data = ConvertMessageToBytes(udpSenderWorkMethodParams.Message); data = SevenZipHelper.Compress(data); if (udpSenderWorkMethodParams.Message.HeaderData.MsgDataType != EMsgDataType.ReceiveConfirmation) { int refNo = udpSenderWorkMethodParams.Message.HeaderData.RefNo; bool receiveConfirmationReceived = false; //Send message max three times. Stop if confirmation is received. for (int i = 0; i < 3; i++) { udpSenderWorkMethodParams.Socket.SendTo(data, udpSenderWorkMethodParams.RemoteEP); //Check for confirmation for 5 sec int j = 0; while (true) { if (ReceivedConfirmationList != null && ReceivedConfirmationList.Contains(refNo)) { receiveConfirmationReceived = true; break; } //Wait for 10 micro sec Thread.Sleep(1000); j++; if (j == 5000) { receiveConfirmationReceived = false; break; } } if (receiveConfirmationReceived) { break; } } if (receiveConfirmationReceived == false) { NotifyFailedTransmission(udpSenderWorkMethodParams); } else { lock (ReceivedConfirmationList) { ReceivedConfirmationList.Remove(refNo); } } } else { udpSenderWorkMethodParams.Socket.SendTo(data, udpSenderWorkMethodParams.RemoteEP); } } catch (Exception ex) { Console.WriteLine(ex.Message); } udpSenderWorkMethodParams.Socket.Close(); }