/// <summary> /// Sends UDP packet. /// </summary> /// <param name="packet">Packet to send.</param> /// <param name="remoteEndPoint">Address of destination for packet. It can be broadcast address.</param> /// <exception cref="System.ArgumentNullException">Packet is null. -or- Remote IP end point is null.</exception> public static void Send(TinyPacket packet, IPEndPoint remoteEndPoint) { if (packet == null) { throw new ArgumentNullException("packet", "Packet is null."); } if (remoteEndPoint == null) { throw new ArgumentNullException("remoteEndPoint", "Remote IP end point is null."); } using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) { if (remoteEndPoint.Address == IPAddress.Broadcast) { socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true); } if (IsRunningOnMono == false) { socket.SetSocketOption(SocketOptionLevel.Udp, SocketOptionName.NoChecksum, false); } socket.SendTo(packet.GetBytes(), remoteEndPoint); Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "TinyMessage [{0} -> {1}]", packet, remoteEndPoint)); } }
/// <summary> /// Sends one UDP packet and expects another in return. /// Returns null if respose is not received within receiveTimeout. /// </summary> /// <param name="packet">Packet to send.</param> /// <param name="remoteEndPoint">Address of destination for packet. It can be broadcast address.</param> /// <param name="receiveTimeout">Number of milliseconds to wait for receive operation to be done. If number is zero, infinite timeout will be used.</param> /// <exception cref="System.ArgumentNullException">Packet is null. -or- Remote IP end point is null.</exception> public static TinyPacket SendAndReceive(TinyPacket packet, IPEndPoint remoteEndPoint, Int32 receiveTimeout) { if (packet == null) { throw new ArgumentNullException("packet", "Packet is null."); } if (remoteEndPoint == null) { throw new ArgumentNullException("remoteEndPoint", "Remote IP end point is null."); } using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) { socket.ReceiveTimeout = receiveTimeout; if (remoteEndPoint.Address == IPAddress.Broadcast) { socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true); } if (IsRunningOnMono == false) { socket.SetSocketOption(SocketOptionLevel.Udp, SocketOptionName.NoChecksum, false); } var bytesOut = packet.GetBytes(); socket.SendTo(bytesOut, bytesOut.Length, SocketFlags.None, remoteEndPoint); Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "TinyMessage [{0} -> {1}]", packet, remoteEndPoint)); EndPoint remoteEndPointIn = (EndPoint)(new IPEndPoint(IPAddress.Any, 0)); var bytesIn = new byte[65536]; try { int len = socket.ReceiveFrom(bytesIn, ref remoteEndPointIn); var packetIn = Medo.Net.TinyPacket.Parse(bytesIn, 0, len); Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "TinyMessage [{0} <- {1}]", packetIn, remoteEndPointIn)); return(packetIn); } catch (SocketException) { return(null); } } }
/// <summary> /// Sends UDP packet. /// </summary> /// Sends one UDP packet and expects another in return. /// Returns null if respose is not received within 250 milliseconds. /// <param name="packet">Packet to send.</param> /// <param name="address">IP address of destination for packet. It can be broadcast address.</param> /// <param name="port">Port of destination for packet.</param> /// <exception cref="System.ArgumentNullException">Packet is null. -or- Remote IP end point is null.</exception> public static TinyPacket SendAndReceive(TinyPacket packet, IPAddress address, Int32 port) { return(SendAndReceive(packet, new IPEndPoint(address, port), 250)); }
/// <summary> /// Sends one UDP packet and expects another in return. /// Returns null if respose is not received within 250 milliseconds. /// </summary> /// <param name="packet">Packet to send.</param> /// <param name="address">IP address of destination for packet. It can be broadcast address.</param> /// <exception cref="System.ArgumentNullException">Packet is null. -or- Remote IP end point is null.</exception> public static TinyPacket SendAndReceive(TinyPacket packet, IPAddress address) { return(SendAndReceive(packet, new IPEndPoint(address, TinyMessage.DefaultPort), 250)); }
/// <summary> /// Sends UDP packet. /// </summary> /// <param name="packet">Packet to send.</param> /// <param name="address">IP address of destination for packet. It can be broadcast address.</param> /// <param name="port">Port of destination for packet.</param> /// <exception cref="System.ArgumentNullException">Packet is null. -or- Remote IP end point is null.</exception> public static void Send(TinyPacket packet, IPAddress address, Int32 port) { Send(packet, new IPEndPoint(address, port)); }
/// <summary> /// Sends UDP packet. /// </summary> /// <param name="packet">Packet to send.</param> /// <param name="address">IP address of destination for packet. It can be broadcast address.</param> /// <exception cref="System.ArgumentNullException">Packet is null. -or- Remote IP end point is null.</exception> public static void Send(TinyPacket packet, IPAddress address) { Send(packet, new IPEndPoint(address, TinyMessage.DefaultPort)); }
private void Run() { try { this.ListenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); this.ListenSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); this.ListenSocket.Bind(this.LocalEndPoint); var buffer = new byte[16384]; EndPoint remoteEP; int inCount; while (!this.IsCanceled) { try { remoteEP = new IPEndPoint(IPAddress.Any, 0); inCount = this.ListenSocket.ReceiveFrom(buffer, ref remoteEP); } catch (SocketException ex) { if (ex.SocketErrorCode == SocketError.Interrupted) { return; } else { throw; } } if (TinyPacketReceived != null) { var newBuffer = new byte[inCount]; Buffer.BlockCopy(buffer, 0, newBuffer, 0, inCount); Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "TinyMessage [{0} <- {1}]", TinyPacket.ParseHeaderOnly(newBuffer, 0, inCount), remoteEP)); var invokeArgs = new object[] { this, new TinyPacketEventArgs(newBuffer, 0, inCount, remoteEP as IPEndPoint) }; foreach (Delegate iDelegate in TinyPacketReceived.GetInvocationList()) { ISynchronizeInvoke syncer = iDelegate.Target as ISynchronizeInvoke; if (syncer == null) { iDelegate.DynamicInvoke(invokeArgs); } else { syncer.BeginInvoke(iDelegate, invokeArgs); } } } } } catch (ThreadAbortException) { } finally { if (this.ListenSocket != null) { ((IDisposable)this.ListenSocket).Dispose(); this.ListenSocket = null; } } }
public TinyPacket GetPacketWithoutData() { return(TinyPacket.ParseHeaderOnly(this.Buffer, this.Offset, this.Count)); }
public TinyPacket GetPacket() { return(TinyPacket.Parse(this.Buffer, this.Offset, this.Count)); }