/// <summary> /// Binds a UDP port /// </summary> /// <param name="name">The UDP port</param> /// <returns>The node</returns> public static unsafe Node Open(string name) { int port = int.Parse(name); if (port == -1) { return(null); } UDPSocket sock = new UDPSocket(); bool found = sock.Bind((ushort)port); if (!found) { return(null); } Node node = new Node(); node.Flags = NodeFlags.FILE; node.Read = readImpl; node.Write = writeImpl; node.GetSize = getSizeImpl; node.Close = closeImpl; UDPSocketCookie cookie = new UDPSocketCookie(sock); node.Cookie = cookie; return(node); }
/// <summary> /// Opens a UDP client socket /// </summary> /// <param name="name">The name (IP:PORT)</param> /// <returns>The node</returns> public static unsafe Node Open(string name) { int foundIndex = name.IndexOf(':'); if (foundIndex == -1) { return(null); } string ip = name.Substring(0, foundIndex); string portText = name.Substring(foundIndex + 1, name.Length - foundIndex - 1); int port = int.Parse(portText); if (port == -1) { Heap.Free(portText); Heap.Free(ip); return(null); } UDPSocket sock = new UDPSocket(); bool found = sock.Connect(ip, (ushort)port); if (!found) { Heap.Free(portText); Heap.Free(ip); return(null); } Node node = new Node(); node.Flags = NodeFlags.FILE; node.Read = readImpl; node.Write = writeImpl; node.GetSize = getSizeImpl; node.Close = closeImpl; UDPSocketCookie cookie = new UDPSocketCookie(sock); node.Cookie = cookie; Heap.Free(portText); Heap.Free(ip); return(node); }
/// <summary> /// Gets the UDP socket from a node /// </summary> /// <param name="node">The node</param> /// <returns>The socket</returns> private static unsafe UDPSocket getSocketFromNode(Node node) { UDPSocketCookie cookie = (UDPSocketCookie)node.Cookie; return(cookie.Socket); }