public void Start(IPEndPoint traceEP, IPAddress traceAdapter, NetTraceSinkDelegate onReceive) { lock (syncLock) { sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); sock.Bind(new IPEndPoint(traceAdapter, traceEP.Port)); if (traceAdapter.Equals(IPAddress.Any)) { sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(traceEP.Address)); } else { sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(traceEP.Address, traceAdapter)); } sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 5); sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastLoopback, 1); this.onReceive = onReceive; this.recvBuf = new byte[NetTracePacket.MaxPacket]; this.recvEP = new IPEndPoint(IPAddress.Any, 0); this.onSockRecv = new AsyncCallback(OnSockReceive); this.recvQueue = new Queue(10000); this.timer = new GatedTimer(new TimerCallback(OnTimer), null, TimeSpan.FromSeconds(0.5), TimeSpan.FromSeconds(0.5)); sock.BeginReceiveFrom(recvBuf, 0, recvBuf.Length, SocketFlags.None, ref recvEP, onSockRecv, null); } }
/// <summary> /// Initialization for Windows. /// </summary> private static void InitWindows() { timer = new GatedTimer(new TimerCallback(OnTimer), null, TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(5)); lastCount = (uint)Environment.TickCount; year = TimeSpan.FromDays(365); // Initialize this such that the first time returned by Now // will be quite a long time after DateTime.MinValue. msi = (ulong)(TimeSpan.TicksPerDay * 365 / TimeSpan.TicksPerMillisecond); msi &= 0xFFFFFFFF00000000; // Get the timer resolution for the current machine. #if MOBILE_DEVICE resolution = TimeSpan.FromMilliseconds(15); // Guessing this #else uint timeAdjustment; uint timeIncrement; bool timeAdjustmentDisabled; if (WinApi.GetSystemTimeAdjustment(out timeAdjustment, out timeIncrement, out timeAdjustmentDisabled) == 0) { resolution = TimeSpan.Zero; SysLog.LogError("Windows Error [{0}] obtaining system timer resolution. All subsequent calls to SysTime will fail.", WinApi.GetLastError()); } else { resolution = TimeSpan.FromTicks(timeIncrement); } #endif }
public void Stop(object o) { lock (syncLock) { if (sock != null) { timer.Dispose(); timer = null; recvQueue.Clear(); recvQueue = null; sock.Close(); sock = null; } } }
/// <summary> /// Static constructor. /// </summary> static AsyncTimer() { pending = new LinkedList <AsyncTimerResult>(); period = TimeSpan.FromMilliseconds(500); timer = new GatedTimer(new TimerCallback(OnTimer), null, period); }