public OutStream @out(TcpSocket fan) { if (m_out == null) { throw IOErr.make("not connected").val; } return(m_out); }
public File createFile(string name) { if (!isDir()) { throw IOErr.make("Not a directory: " + this).val; } return(this.plus(FanStr.toUri(name)).create()); }
public InStream @in(TcpSocket fan) { if (m_in == null) { throw IOErr.make("not connected").val; } return(m_in); }
public void send(UdpSocket fan, UdpPacket packet) { // map buf bytes to packet MemBuf data = (MemBuf)packet.data(); byte[] buf = data.m_buf; int off = data.m_pos; int len = data.m_size - off; // map address, port IpAddr addr = packet.addr(); Long port = packet.port(); if (isConnected(fan)) { if (addr != null || port != null) { throw ArgErr.make("Address and port must be null to send while connected").val; } try { m_dotnet.Send(buf, off, len, SocketFlags.None); } catch (SocketException e) { throw IOErr.make(e).val; } } else { if (addr == null || port == null) { throw ArgErr.make("Address or port is null").val; } try { if (m_dotnet == null) { m_dotnet = createSocket(); } IPEndPoint endPoint = new IPEndPoint(addr.m_peer.m_dotnet, port.intValue()); m_dotnet.SendTo(buf, off, len, SocketFlags.None, endPoint); } catch (SocketException e) { throw IOErr.make(e).val; } } // lastly drain buf data.m_pos += len; }
public File createDir(string name) { if (!isDir()) { throw IOErr.make("Not a directory: " + this).val; } if (!name.EndsWith("/")) { name = name + "/"; } return(this.plus(FanStr.toUri(name)).create()); }
////////////////////////////////////////////////////////////////////////// // Communication ////////////////////////////////////////////////////////////////////////// public TcpSocket bind(TcpSocket fan, IpAddr addr, Long port) { try { IPAddress dotnetAddr = (addr == null) ? IPAddress.Any : addr.m_peer.m_dotnet; int dotnetPort = (port == null) ? 0 : port.intValue(); m_dotnet.Bind(new IPEndPoint(dotnetAddr, dotnetPort)); return(fan); } catch (SocketException e) { throw IOErr.make(e).val; } }
////////////////////////////////////////////////////////////////////////// // Collection (@collection) ////////////////////////////////////////////////////////////////////////// private bool writeCollectionItems(Type type, object obj, bool first) { // lookup each method Method m = type.method("each", false); if (m == null) { throw IOErr.make("Missing " + type.qname() + ".each").val; } // call each(it) EachIterator it = new EachIterator(this, first); m.invoke(obj, new object[] { it }); return(it.first); }
public static File createTemp(string prefix, string suffix, File dir) { if (prefix == null || prefix.Length == 0) { prefix = "fan"; } if (suffix == null) { suffix = ".tmp"; } string parent = null; if (dir == null) { parent = System.IO.Path.GetTempPath(); } else { if (!(dir is LocalFile)) { throw IOErr.make("Dir is not on local file system: " + dir).val; } parent = ((LocalFile)dir).m_file.FullName; } try { string name = parent + '\\' + prefix + suffix; int count = 1; while (System.IO.File.Exists(name)) { name = parent + '\\' + prefix + (count++) + suffix; } LocalFile temp = new LocalFile(new System.IO.FileInfo(name)); temp.create(); return(temp); } catch (System.IO.IOException e) { throw IOErr.make(e).val; } }
public UdpSocket connect(UdpSocket fan, IpAddr addr, long port) { try { if (m_dotnet == null) { m_dotnet = createSocket(); } m_dotnet.Connect(addr.m_peer.m_dotnet, (int)port); IPEndPoint endPoint = m_dotnet.RemoteEndPoint as IPEndPoint; m_remoteAddr = IpAddrPeer.make(endPoint.Address); m_remotePort = endPoint.Port; return(fan); } catch (SocketException e) { throw IOErr.make(e).val; } }
internal static System.Exception err(string msg, int line, System.Exception ex) { return(IOErr.make(msg + " [Line " + line + "]", ex).val); }
private void doCopyTo(File to, object exclude, object overwrite) { // check exclude if (exclude is Regex) { if (((Regex)exclude).matches(m_uri.toStr())) { return; } } else if (exclude is Func) { if (((Func)exclude).call(this) == Boolean.True) { return; } } // check for overwrite if (to.exists()) { if (overwrite is Boolean) { if (overwrite == Boolean.False) { return; } } else if (overwrite is Func) { if (((Func)overwrite).call(this) == Boolean.False) { return; } } else { throw IOErr.make("No overwrite policy for `" + to + "`").val; } } // copy directory if (isDir()) { to.create(); List kids = list(); for (int i = 0; i < kids.sz(); ++i) { File kid = (File)kids.get(i); kid.doCopyTo(to.plusNameOf(kid), exclude, overwrite); } } // copy file contents else { OutStream @out = to.@out(); try { @in().pipe(@out); } finally { @out.close(); } } }
////////////////////////////////////////////////////////////////////////// // Write ////////////////////////////////////////////////////////////////////////// public void writeObj(object obj) { if (obj == null) { w("null"); return; } if (obj.GetType().FullName[0] == 'S') { if (obj is bool && (bool)obj) { w("true"); return; } if (obj is bool && !(bool)obj) { w("false"); return; } if (obj is double) { FanFloat.encode((double)obj, this); return; } if (obj is long) { FanInt.encode((long)obj, this); return; } if (obj is string) { wStrLiteral(obj.ToString(), '"'); return; } } if (obj.GetType().FullName[0] == 'F') { if (obj is Boolean && (obj as Boolean).booleanValue()) { w("true"); return; } if (obj is Boolean && !(obj as Boolean).booleanValue()) { w("false"); return; } if (obj is Double) { FanFloat.encode((obj as Double).doubleValue(), this); return; } if (obj is Long) { FanInt.encode((obj as Long).longValue(), this); return; } if (obj is BigDecimal) { FanDecimal.encode((BigDecimal)obj, this); return; } } if (obj is Literal) { ((Literal)obj).encode(this); return; } Type type = FanObj.@typeof(obj); Serializable ser = (Serializable)type.facet(Sys.SerializableType, false); if (ser != null) { if (ser.m_simple) { writeSimple(type, obj); } else { writeComplex(type, obj, ser); } } else { if (skipErrors) { w("null /* Not serializable: ").w(type.qname()).w(" */"); } else { throw IOErr.make("Not serializable: " + type).val; } } }
public UdpPacket receive(UdpSocket fan, UdpPacket packet) { // create packet if null if (packet == null) { packet = UdpPacket.make(null, null, new MemBuf(1024)); } // map buf bytes to packet MemBuf data = (MemBuf)packet.data(); byte[] buf = data.m_buf; int off = data.m_pos; int len = buf.Length - off; int recv = 0; EndPoint sender = new IPEndPoint(IPAddress.Any, 0); // receive if (isConnected(fan)) { try { recv = m_dotnet.Receive(buf, off, len, SocketFlags.None); sender = m_dotnet.RemoteEndPoint; } catch (SocketException e) { // .NET will truncate contents correctly, but still throws a // SocketException, so catch that specific case and allow it if (e.Message.StartsWith("A message sent on a datagram socket was larger")) { recv = len; sender = m_dotnet.RemoteEndPoint; } else { throw IOErr.make(e).val; } } } else { try { if (m_dotnet == null) { m_dotnet = createSocket(); } recv = m_dotnet.ReceiveFrom(buf, off, len, SocketFlags.None, ref sender); } catch (SocketException e) { // .NET will truncate contents correctly, but still throws a // SocketException, so catch that specific case and allow it if (e.Message.StartsWith("A message sent on a datagram socket was larger")) { recv = len; } else { throw IOErr.make(e).val; } } } // update packet with received message IPEndPoint endPoint = sender as IPEndPoint; packet.addr(IpAddrPeer.make(endPoint.Address)); packet.port(Long.valueOf(endPoint.Port)); data.m_pos += recv; data.m_size += recv; return(packet); }