private AppTimeMetric ProcessJob(object job) { Socket fastAGISocket = null; try { fastAGISocket = (Socket)job; fastAGISocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true); logger.Debug("fastagi connection from " + IPSocket.GetSocketString((IPEndPoint)fastAGISocket.RemoteEndPoint) + "(" + Thread.CurrentThread.Name + " " + DateTime.Now.ToString("dd MMM yyyy HH:mm:ss") + ")"); byte[] buffer = new byte[2048]; int bytesRead = 1; // Caution - it could take the Asterisk server more than one socket send to get the all the request parameters sent. StringBuilder request = new StringBuilder(); while (bytesRead > 0) { bytesRead = fastAGISocket.Receive(buffer, 0, 2048, SocketFlags.None); request.Append(Encoding.ASCII.GetString(buffer, 0, bytesRead)); logger.Debug(Encoding.ASCII.GetString(buffer, 0, bytesRead)); if (request.ToString() != null && (Regex.Match(request.ToString(), @"\n\n", RegexOptions.Singleline).Success || Regex.Match(request.ToString(), @"\r\n\r\n", RegexOptions.Singleline).Success)) { break; } } FastAGIRequest fastAGIRequest = new FastAGIRequest(); return(fastAGIRequest.Run(fastAGISocket, request.ToString())); } catch (Exception excp) { logger.Error("Exception FastAGIWorker ProcessJobEvent. " + excp.Message); ExceptionAlert(excp.Message); return(new AppTimeMetric()); } finally { if (fastAGISocket != null) { try { logger.Debug("connection closed."); fastAGISocket.Close(); } catch (Exception sockCkoseExcp) { logger.Error("Exception FastAGIQueueDaemon ProceesJob (closing AGI socket). " + sockCkoseExcp); } } } }
public STUNServer(IPEndPoint primaryEndPoint, STUNSendMessageDelegate primarySend, IPEndPoint secondaryEndPoint, STUNSendMessageDelegate secondarySend) { m_primaryEndPoint = primaryEndPoint; m_primarySend = primarySend; m_secondaryEndPoint = secondaryEndPoint; m_secondarySend = secondarySend; m_primaryDiffPortSocket = NetServices.CreateRandomUDPListener(m_primaryEndPoint.Address, out m_primaryDiffPortEndPoint); m_secondaryDiffPortSocket = NetServices.CreateRandomUDPListener(m_secondaryEndPoint.Address, out m_secondaryDiffPortEndPoint); logger.LogDebug("STUN Server additional sockets, primary=" + IPSocket.GetSocketString(m_primaryDiffPortEndPoint) + ", secondary=" + IPSocket.GetSocketString(m_secondaryDiffPortEndPoint) + "."); }
public void Start() { try { logger.Debug("FastAGIDaemon Starting"); if (m_agiServerIPAddress == null || m_agiServerIPAddress.Trim().Length == 0) { throw new ApplicationException("Could not start FastAGI service as no listening IP address was specified."); } else if (m_agiWorkerThreadsCount <= 0) { throw new ApplicationException("Could not start FastAGI service as the number of worker threads is zero."); } IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse(m_agiServerIPAddress), m_agiServerPort); logger.Debug("Starting FastAGI socket on " + IPSocket.GetSocketString(localEndPoint)); m_fastAGIServer = new TcpListener(localEndPoint); m_fastAGIServer.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true); m_fastAGIServer.Start(); m_fastAGIQueueDaemon = new FastAGIQueueDaemon(m_queueAlertThreshold, m_queueMetricsFilePath, m_appMetricsFilePath, m_agiErrorAddress); m_fastAGIQueueDaemon.Start(m_agiWorkerThreadsCount, THREAD_NAME); while (!m_stopDaemon) { Socket fastAGIClient = m_fastAGIServer.AcceptSocket(); IPEndPoint connEndPoint = (IPEndPoint)fastAGIClient.RemoteEndPoint; logger.Debug("new connection from " + IPSocket.GetSocketString(connEndPoint)); m_fastAGIQueueDaemon.AddNewConnection(fastAGIClient); } logger.Debug("FastAGI Deamon Stopping."); } catch (Exception excp) { logger.Error("Exception FastAGIDaemon Start. " + excp.Message); } }
public override void Send(IPEndPoint destinationEndPoint, byte[] buffer) { try { if (destinationEndPoint == null) { throw new ApplicationException("An empty destination was specified to Send in SIPUDPChannel."); } else { string str = Encoding.UTF8.GetString(buffer); m_sipConn.Send(buffer, buffer.Length, destinationEndPoint); } } catch (Exception excp) { Logger.Logger.Error("Exception (" + excp.GetType().ToString() + ") SIPUDPChannel Send (sendto=>" + IPSocket.GetSocketString(destinationEndPoint) + "). ->" + excp.Message); throw excp; } }
public RTCPReportSampler(Guid rtpStreamId, uint syncSource, IPEndPoint remoteEndPoint, UInt16 startSequenceNumber, DateTime startTime, long bytesReceived) { m_rtpStreamId = rtpStreamId; m_syncSource = syncSource; m_remoteEndPoint = remoteEndPoint; m_windowStartSeqNum = startSequenceNumber; m_windowLastSeqNum = startSequenceNumber; m_windowSecondLastSeqNum = startSequenceNumber; m_lastSampleTime = DateTime.Now; logger.Debug("New RTCP report created for " + syncSource + " for stream from " + IPSocket.GetSocketString(remoteEndPoint) + ", start seq num=" + startSequenceNumber + "."); //resultsLogger.Info("StartTime,StartTimestamp,EndTime,EndTimestamp,Duration(ms),StartSeqNum,EndSeqNum,TotalPackets,TotalBytes,TransmissionRate(bps),Drops,Duplicates"); RTPReceiveRecord measurement = new RTPReceiveRecord(startTime, startSequenceNumber, bytesReceived, 0, true, false); m_rcvdSeqNums.Add(startSequenceNumber, measurement); }
public void Send(IPEndPoint destinationEndPoint, byte[] buffer, int length) { try { if (destinationEndPoint == null) { throw new ApplicationException("An empty destination was specified to Send in SIPUDPChannel."); } else { if (m_udpClient != null && m_udpClient.Client != null) { m_udpClient.Send(buffer, length, destinationEndPoint); } } } catch (Exception excp) { logger.Error("Exception (" + excp.GetType().ToString() + ") UDPListener Send (sendto=>" + IPSocket.GetSocketString(destinationEndPoint) + "). " + excp.Message); throw excp; } }
public virtual void Send(IPEndPoint destinationEndPoint, byte[] buffer) { try { if (destinationEndPoint == null) { logger.LogError("An empty destination was specified to Send in STUNListener."); } m_stunConn.Send(buffer, buffer.Length, destinationEndPoint); } catch (ObjectDisposedException) { logger.LogWarning("The STUNListener was not accessible when attempting to send a message to, " + IPSocket.GetSocketString(destinationEndPoint) + "."); } catch (Exception excp) { logger.LogError("Exception (" + excp.GetType().ToString() + ") STUNListener Send (sendto=>" + IPSocket.GetSocketString(destinationEndPoint) + "). " + excp.Message); throw; } }
private void Send() { try { int payloadSize = RTPPacketSendSize; RTPPacket rtpPacket = new RTPPacket(RTPPacketSendSize); byte[] rtpBytes = rtpPacket.GetBytes(); RTPHeader rtpHeader = new RTPHeader(); rtpHeader.SequenceNumber = (UInt16)65000; //Convert.ToUInt16(Crypto.GetRandomInt(0, UInt16.MaxValue)); uint sendTimestamp = uint.MaxValue - 5000; uint lastSendTimestamp = sendTimestamp; UInt16 lastSeqNum = 0; logger.Debug("RTP send stream starting to " + IPSocket.GetSocketString(m_streamEndPoint) + " with payload size " + payloadSize + " bytes."); Sending = true; m_startRTPSendTime = DateTime.MinValue; m_lastRTPSentTime = DateTime.MinValue; m_sampleStartSeqNo = rtpHeader.SequenceNumber; DateTime lastRTPSendAttempt = DateTime.Now; while (m_udpListener != null && !StopListening) { // This may be changed by the listener so it needs to be set each iteration. IPEndPoint dstEndPoint = m_streamEndPoint; //logger.Info("Sending RTP packet to " + dstEndPoint.Address + ":" + dstEndPoint.Port); if (payloadSize != m_rtpPacketSendSize) { payloadSize = m_rtpPacketSendSize; logger.Info("Changing RTP payload to " + payloadSize); rtpPacket = new RTPPacket(RTP_HEADER_SIZE + m_rtpPacketSendSize); rtpBytes = rtpPacket.GetBytes(); } try { if (m_startRTPSendTime == DateTime.MinValue) { m_startRTPSendTime = DateTime.Now; rtpHeader.MarkerBit = 0; logger.Debug("RTPSink Send SyncSource=" + rtpPacket.Header.SyncSource + "."); } else { lastSendTimestamp = sendTimestamp; double milliSinceLastSend = DateTime.Now.Subtract(m_lastRTPSentTime).TotalMilliseconds; sendTimestamp = Convert.ToUInt32((lastSendTimestamp + (milliSinceLastSend * TIMESTAMP_FACTOR)) % uint.MaxValue); if (lastSendTimestamp > sendTimestamp) { logger.Error("RTP Sender previous timestamp (" + lastSendTimestamp + ") > timestamp (" + sendTimestamp + ") ms since last send=" + milliSinceLastSend + ", lastseqnum=" + lastSeqNum + ", seqnum=" + rtpHeader.SequenceNumber + "."); } if (DateTime.Now.Subtract(m_lastRTPSentTime).TotalMilliseconds > 75) { logger.Debug("delayed send: " + rtpHeader.SequenceNumber + ", time since last send " + DateTime.Now.Subtract(m_lastRTPSentTime).TotalMilliseconds + "ms."); } } rtpHeader.Timestamp = sendTimestamp; byte[] rtpHeaderBytes = rtpHeader.GetBytes(); Array.Copy(rtpHeaderBytes, 0, rtpBytes, 0, rtpHeaderBytes.Length); // Send RTP packets and any extra channels required to emulate mutliple calls. for (int channelCount = 0; channelCount < m_channels; channelCount++) { //logger.Debug("Send rtp getting wallclock timestamp for " + DateTime.Now.ToString("dd MMM yyyy HH:mm:ss:fff")); //DateTime sendTime = DateTime.Now; //rtpHeader.Timestamp = RTPHeader.GetWallclockUTCStamp(sendTime); //logger.Debug(rtpHeader.SequenceNumber + "," + rtpHeader.Timestamp); m_udpListener.Send(rtpBytes, rtpBytes.Length, dstEndPoint); m_lastRTPSentTime = DateTime.Now; m_packetsSent++; m_bytesSent += rtpBytes.Length; if (m_packetsSent % 500 == 0) { logger.Debug("Total packets sent to " + dstEndPoint.ToString() + " " + m_packetsSent + ", bytes " + NumberFormatter.ToSIByteFormat(m_bytesSent, 2) + "."); } //sendLogger.Info(m_lastRTPSentTime.ToString("dd MMM yyyy HH:mm:ss:fff") + "," + m_lastRTPSentTime.Subtract(m_startRTPSendTime).TotalMilliseconds.ToString("0") + "," + rtpHeader.SequenceNumber + "," + rtpBytes.Length); //sendLogger.Info(rtpHeader.SequenceNumber + "," + DateTime.Now.ToString("dd MMM yyyy HH:mm:ss:fff")); if (DataSent != null) { try { DataSent(m_streamId, rtpBytes, dstEndPoint); } catch (Exception excp) { logger.Error("Exception RTPSink DataSent. " + excp.Message); } } lastSeqNum = rtpHeader.SequenceNumber; if (rtpHeader.SequenceNumber == UInt16.MaxValue) { //logger.Debug("RTPSink looping the sequence number in sample."); rtpHeader.SequenceNumber = 0; } else { rtpHeader.SequenceNumber++; } } } catch (Exception excp) { logger.Error("Exception RTP Send. " + excp.GetType() + ". " + excp.Message); if (excp.GetType() == typeof(SocketException)) { logger.Error("socket exception errorcode=" + ((SocketException)excp).ErrorCode + "."); } logger.Warn("Remote socket closed on send. Last RTP recevied " + m_lastRTPReceivedTime.ToString("dd MMM yyyy HH:mm:ss") + ", last RTP successfull send " + m_lastRTPSentTime.ToString("dd MMM yyyy HH:mm:ss") + "."); } Thread.Sleep(RTPFrameSize); #region Check for whether the stream has timed out on a send or receive and if so shut down the stream. double noRTPRcvdDuration = (m_lastRTPReceivedTime != DateTime.MinValue) ? DateTime.Now.Subtract(m_lastRTPReceivedTime).TotalSeconds : 0; double noRTPSentDuration = (m_lastRTPSentTime != DateTime.MinValue) ? DateTime.Now.Subtract(m_lastRTPSentTime).TotalSeconds : 0; double testDuration = DateTime.Now.Subtract(m_startRTPSendTime).TotalSeconds; if (( noRTPRcvdDuration > NO_RTP_TIMEOUT || noRTPSentDuration > NO_RTP_TIMEOUT || (m_lastRTPReceivedTime == DateTime.MinValue && testDuration > NO_RTP_TIMEOUT)) && // If the test request comes from a private or unreachable IP address then no RTP will ever be received. StopIfNoData) { logger.Warn("Disconnecting RTP stream on " + m_localEndPoint.Address.ToString() + ":" + m_localEndPoint.Port + " due to not being able to send any RTP for " + NO_RTP_TIMEOUT + "s."); StopListening = true; } // Shutdown the socket even if there is still RTP but the stay alive limit has been exceeded. if (RTPMaxStayAlive > 0 && DateTime.Now.Subtract(m_startRTPSendTime).TotalSeconds > RTPMaxStayAlive) { logger.Warn("Shutting down RTPSink due to passing RTPMaxStayAlive time."); Shutdown(); StopListening = true; } #endregion } } catch (Exception excp) { logger.Error("Exception Send RTPSink: " + excp.Message); } finally { #region Shut down socket. Shutdown(); if (SenderClosed != null) { try { SenderClosed(m_streamId, m_callDescriptorId); } catch (Exception excp) { logger.Error("Exception RTPSink SenderClosed. " + excp.Message); } } #endregion } }
private void Listen() { try { UdpClient udpSvr = m_udpListener; if (udpSvr == null) { logger.Error("The UDP server was not correctly initialised in the RTP sink when attempting to start the listener, the RTP stream has not been intialised."); return; } else { logger.Debug("RTP Listener now listening on " + m_localEndPoint.Address + ":" + m_localEndPoint.Port + "."); } IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); byte[] rcvdBytes = null; m_startRTPReceiveTime = DateTime.MinValue; m_lastRTPReceivedTime = DateTime.MinValue; DateTime previousRTPReceiveTime = DateTime.MinValue; uint previousTimestamp = 0; UInt16 sequenceNumber = 0; UInt16 previousSeqNum = 0; uint senderSendSpacing = 0; uint lastSenderSendSpacing = 0; while (!StopListening) { rcvdBytes = null; try { rcvdBytes = udpSvr.Receive(ref remoteEndPoint); } catch { //logger.Warn("Remote socket closed on receive. Last RTP received " + m_lastRTPReceivedTime.ToString("dd MMM yyyy HH:mm:ss") + ", last RTP successfull send " + m_lastRTPSentTime.ToString("dd MMM yyyy HH:mm:ss") + "."); } if (rcvdBytes != null && rcvdBytes.Length > 0) { // Check whether this is an RTCP report. UInt16 firstWord = BitConverter.ToUInt16(rcvdBytes, 0); if (BitConverter.IsLittleEndian) { firstWord = NetConvert.DoReverseEndian(firstWord); } ushort packetType = 0; if (BitConverter.IsLittleEndian) { packetType = Convert.ToUInt16(firstWord & 0x00ff); } if (packetType == RTCPHeader.RTCP_PACKET_TYPE) { logger.Debug("RTP Listener received remote RTCP report from " + remoteEndPoint + "."); try { RTCPPacket rtcpPacket = new RTCPPacket(rcvdBytes); RTCPReportPacket rtcpReportPacket = new RTCPReportPacket(rtcpPacket.Reports); if (RTCPReportReceived != null) { RTCPReportReceived(this, rtcpReportPacket); } } catch (Exception rtcpExcp) { logger.Error("Exception processing remote RTCP report. " + rtcpExcp.Message); } continue; } // Channel statistics. DateTime rtpReceiveTime = DateTime.Now; if (m_startRTPReceiveTime == DateTime.MinValue) { m_startRTPReceiveTime = rtpReceiveTime; //m_sampleStartTime = rtpReceiveTime; } previousRTPReceiveTime = new DateTime(m_lastRTPReceivedTime.Ticks); m_lastRTPReceivedTime = rtpReceiveTime; m_packetsReceived++; m_bytesReceived += rcvdBytes.Length; previousSeqNum = sequenceNumber; // This stops the thread running the ListenerTimeout method from deciding the strema has recieved no RTP and therefore should be shutdown. m_lastPacketReceived.Set(); // Let whoever has subscribed that an RTP packet has been received. if (DataReceived != null) { try { DataReceived(m_streamId, rcvdBytes, remoteEndPoint); } catch (Exception excp) { logger.Error("Exception RTPSink DataReceived. " + excp.Message); } } if (m_packetsReceived % 500 == 0) { logger.Debug("Total packets received from " + remoteEndPoint.ToString() + " " + m_packetsReceived + ", bytes " + NumberFormatter.ToSIByteFormat(m_bytesReceived, 2) + "."); } try { RTPPacket rtpPacket = new RTPPacket(rcvdBytes); uint syncSource = rtpPacket.Header.SyncSource; uint timestamp = rtpPacket.Header.Timestamp; sequenceNumber = rtpPacket.Header.SequenceNumber; //logger.Debug("seqno=" + rtpPacket.Header.SequenceNumber + ", timestamp=" + timestamp); if (previousRTPReceiveTime != DateTime.MinValue) { //uint senderSendSpacing = rtpPacket.Header.Timestamp - previousTimestamp; // Need to cope with cases where the timestamp has looped, if this timestamp is < last timesatmp and there is a large difference in them then it's because the timestamp counter has looped. lastSenderSendSpacing = senderSendSpacing; senderSendSpacing = (Math.Abs(timestamp - previousTimestamp) > (uint.MaxValue / 2)) ? timestamp + uint.MaxValue - previousTimestamp : timestamp - previousTimestamp; if (previousTimestamp > timestamp) { logger.Error("BUG: Listener previous timestamp (" + previousTimestamp + ") > timestamp (" + timestamp + "), last seq num=" + previousSeqNum + ", seqnum=" + sequenceNumber + "."); // Cover for this bug until it's nailed down. senderSendSpacing = lastSenderSendSpacing; } double senderSpacingMilliseconds = (double)senderSendSpacing / (double)TIMESTAMP_FACTOR; double interarrivalReceiveTime = m_lastRTPReceivedTime.Subtract(previousRTPReceiveTime).TotalMilliseconds; #region RTCP reporting. if (m_rtcpSampler == null) { //resultsLogger.Info("First Packet: " + rtpPacket.Header.SequenceNumber + "," + m_arrivalTime.ToString("HH:mm:fff")); m_rtcpSampler = new RTCPReportSampler(m_streamId, syncSource, remoteEndPoint, rtpPacket.Header.SequenceNumber, m_lastRTPReceivedTime, rcvdBytes.Length); m_rtcpSampler.RTCPReportReady += new RTCPSampleReadyDelegate(m_rtcpSampler_RTCPReportReady); m_rtcpSampler.StartSampling(); } else { //m_receiverReports[syncSource].RecordRTPReceive(rtpPacket.Header.SequenceNumber, sendTime, rtpReceiveTime, rcvdBytes.Length); // Transit time is calculated by knowing that the sender sent a packet at a certain time after the last send and the receiver received a pakcet a certain time after the last receive. // The difference in these two times is the jitter present. The transit time can change with each transimission and as this methid relies on two sends two packet // arrivals to calculate the transit time it's not going to be perfect (you'd need synchronised NTP clocks at each end to be able to be accurate). // However if used tor an average calculation it should be pretty close. //double transitTime = Math.Abs(interarrivalReceiveTime - senderSpacingMilliseconds); uint jitter = (interarrivalReceiveTime - senderSpacingMilliseconds > 0) ? Convert.ToUInt32(interarrivalReceiveTime - senderSpacingMilliseconds) : 0; if (jitter > 75) { logger.Debug("seqno=" + rtpPacket.Header.SequenceNumber + ", timestmap=" + timestamp + ", ts-prev=" + previousTimestamp + ", receive spacing=" + interarrivalReceiveTime + ", send spacing=" + senderSpacingMilliseconds + ", jitter=" + jitter); } else { //logger.Debug("seqno=" + rtpPacket.Header.SequenceNumber + ", receive spacing=" + interarrivalReceiveTime + ", timestamp=" + timestamp + ", transit time=" + transitTime); } m_rtcpSampler.RecordRTPReceive(m_lastRTPReceivedTime, rtpPacket.Header.SequenceNumber, rcvdBytes.Length, jitter); } #endregion } else { logger.Debug("RTPSink Listen SyncSource=" + rtpPacket.Header.SyncSource + "."); } previousTimestamp = timestamp; } catch (Exception excp) { logger.Error("Received data was not a valid RTP packet. " + excp.Message); } #region Switching endpoint if required to cope with NAT. // If a packet is recieved from an endpoint that wasn't expected treat the stream as being NATted and switch the endpoint to the socket on the NAT server. try { if (m_streamEndPoint != null && m_streamEndPoint.Address != null && remoteEndPoint != null && remoteEndPoint.Address != null && (m_streamEndPoint.Address.ToString() != remoteEndPoint.Address.ToString() || m_streamEndPoint.Port != remoteEndPoint.Port)) { logger.Debug("Expecting RTP on " + IPSocket.GetSocketString(m_streamEndPoint) + " but received on " + IPSocket.GetSocketString(remoteEndPoint) + ", now sending to " + IPSocket.GetSocketString(remoteEndPoint) + "."); m_streamEndPoint = remoteEndPoint; if (RemoteEndPointChanged != null) { try { RemoteEndPointChanged(m_streamId, remoteEndPoint); } catch (Exception changeExcp) { logger.Error("Exception RTPListener Changing Remote EndPoint. " + changeExcp.Message); } } } } catch (Exception setSendExcp) { logger.Error("Exception RTPListener setting SendTo Socket. " + setSendExcp.Message); } #endregion } else if (!StopListening) // Empty packet was received possibly indicating connection closure so check for timeout. { double noRTPRcvdDuration = (m_lastRTPReceivedTime != DateTime.MinValue) ? DateTime.Now.Subtract(m_lastRTPReceivedTime).TotalSeconds : 0; double noRTPSentDuration = (m_lastRTPSentTime != DateTime.MinValue) ? DateTime.Now.Subtract(m_lastRTPSentTime).TotalSeconds : 0; //logger.Warn("Remote socket closed on receive on " + m_localEndPoint.Address.ToString() + ":" + + m_localEndPoint.Port + ", reinitialising. No rtp for " + noRTPRcvdDuration + "s. last rtp " + m_lastRTPReceivedTime.ToString("dd MMM yyyy HH:mm:ss") + "."); // If this check is not done then the stream will never time out if it doesn't receive the first packet. if (m_lastRTPReceivedTime == DateTime.MinValue) { m_lastRTPReceivedTime = DateTime.Now; } remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); if ((noRTPRcvdDuration > NO_RTP_TIMEOUT || noRTPSentDuration > NO_RTP_TIMEOUT) && StopIfNoData) { logger.Warn("Disconnecting RTP listener on " + m_localEndPoint.ToString() + " due to not being able to send or receive any RTP for " + NO_RTP_TIMEOUT + "s."); Shutdown(); } } } } catch (Exception excp) { logger.Error("Exception Listen RTPSink: " + excp.Message); } finally { #region Shut down socket. Shutdown(); if (ListenerClosed != null) { try { ListenerClosed(m_streamId, m_callDescriptorId); } catch (Exception excp) { logger.Error("Exception RTPSink ListenerClosed. " + excp.Message); } } #endregion } }