/** * Read an Erlang atom from the stream. * * @return a String containing the value of the atom. * * @exception OtpErlangDecodeException * if the next term in the stream is not an atom. */ public String read_atom() { int tag; int len; byte[] strbuf; String atom; tag = read1skip_version(); if (tag != OtpExternal.atomTag) { throw new OtpErlangDecodeException("wrong tag encountered, expected " + OtpExternal.atomTag + ", got " + tag); } len = read2BE(); strbuf = new byte[len]; this.readN(strbuf); atom = OtpErlangString.newString(strbuf); if (atom.Length > OtpExternal.maxAtomLength) { atom = atom.Substring(0, OtpExternal.maxAtomLength); } return(atom); }
private void r4_port(TcpClient s, OtpInputStream ibuf) { try { int len = (int)(ibuf.Length - 1); byte[] alive = new byte[len]; ibuf.readN(alive); String name = OtpErlangString.newString(alive); OtpPublishedNode node = null; if (traceLevel >= traceThreshold) { log.Debug("<- PORT (r4) " + name); } lock (portmap) { if (portmap.ContainsKey(name)) { node = portmap[name]; } } OtpOutputStream obuf = new OtpOutputStream(); if (node != null) { obuf.write1(port4resp); obuf.write1(0); obuf.write2BE(node.Port); obuf.write1(node.Type); obuf.write1(node.Proto); obuf.write2BE(node.DistHigh); obuf.write2BE(node.DistLow); obuf.write2BE(len); obuf.writeN(alive); obuf.write2BE(0); } else { obuf.write1(port4resp); obuf.write1(1); } obuf.WriteTo(s.GetStream()); } catch (IOException) { if (traceLevel >= traceThreshold) { log.Debug("<- (no response)"); } throw new IOException("Request not responding"); } return; }
private void r4_publish(TcpClient s, OtpInputStream ibuf) { try { int port = ibuf.read2BE(); int type = ibuf.read1(); int proto = ibuf.read1(); int distHigh = ibuf.read2BE(); int distLow = ibuf.read2BE(); int len = ibuf.read2BE(); byte[] alive = new byte[len]; ibuf.readN(alive); int elen = ibuf.read2BE(); byte[] extra = new byte[elen]; ibuf.readN(extra); String name = OtpErlangString.newString(alive); OtpPublishedNode node = new OtpPublishedNode(name); node.Type = type; node.DistHigh = distHigh; node.DistLow = distLow; node.Proto = proto; node.Port = port; if (traceLevel >= traceThreshold) { log.Debug("<- PUBLISH (r4) " + name + " port=" + node.Port); } OtpOutputStream obuf = new OtpOutputStream(); obuf.write1(publish4resp); obuf.write1(0); obuf.write2BE(epmd.Creation); obuf.WriteTo(s.GetStream()); lock (portmap) { portmap.Add(name, node); } publishedPort.Add(name); } catch (IOException) { if (traceLevel >= traceThreshold) { log.Debug("<- (no response)"); } throw new IOException("Request not responding"); } return; }
/** * Read a string from the stream. * * @return the value of the string. * * @exception OtpErlangDecodeException * if the next term in the stream is not a string. */ public String read_string() { int tag; int len; byte[] strbuf; tag = read1skip_version(); switch (tag) { case OtpExternal.stringTag: len = read2BE(); strbuf = new byte[len]; this.readN(strbuf); return(OtpErlangString.newString(strbuf)); case OtpExternal.nilTag: return(""); case OtpExternal.listTag: // List when unicode + len = read4BE(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < len; i++) { uint cp = (uint)read_int(); if (!OtpErlangString.isValidCodePoint((int)cp)) { throw new OtpErlangDecodeException("Invalid CodePoint: " + cp); } if (1 <= (cp >> 16)) { cp -= 0x10000; char high = (char)((cp / 0x400) + 0xD800); char low = (char)((cp % 0x400) + 0xDC00); sb.Append(high); sb.Append(low); } else { sb.Append((char)cp); } } read_nil(); return(sb.ToString()); default: throw new OtpErlangDecodeException("Wrong tag encountered, expected " + OtpExternal.stringTag + " or " + OtpExternal.listTag + ", got " + tag); } }
/** * Read an Erlang float from the stream. * * @return the float value, as a double. * * @exception OtpErlangDecodeException * if the next term in the stream is not a float. */ public double read_double() { int tag; // parse the stream tag = read1skip_version(); switch (tag) { case OtpExternal.newFloatTag: { return(BitConverter.ToDouble(BitConverter.GetBytes(readBE(8)), 0)); } case OtpExternal.floatTag: { byte[] strbuf = new byte[31]; String str; double val; // get the string this.readN(strbuf); str = OtpErlangString.newString(strbuf); if (!Double.TryParse(str, out val)) { throw new OtpErlangDecodeException("Invalid float format: '" + str + "'"); } return(val); } default: throw new OtpErlangDecodeException("Wrong tag encountered, expected " + OtpExternal.newFloatTag + ", got " + tag); } }
public static String[] lookupNames(IPAddress address) { try { OtpOutputStream obuf = new OtpOutputStream(); using (TcpClient s = new TcpClient(address.ToString(), EpmdPort.get())) { obuf.write2BE(1); obuf.write1(names4req); // send request obuf.WriteTo(s.GetStream()); if (traceLevel >= traceThreshold) { log.Debug("-> NAMES (r4) "); } // get reply byte[] buffer = new byte[256]; MemoryStream ms = new MemoryStream(256); while (true) { int bytesRead = s.GetStream().Read(buffer, 0, buffer.Length); if (bytesRead == -1) { break; } ms.Write(buffer, 0, bytesRead); } byte[] tmpbuf = ms.GetBuffer(); OtpInputStream ibuf = new OtpInputStream(tmpbuf, 0); ibuf.read4BE(); // read port int // int port = ibuf.read4BE(); // check if port = epmdPort int n = tmpbuf.Length; byte[] buf = new byte[n - 4]; Array.Copy(tmpbuf, 4, buf, 0, n - 4); String all = OtpErlangString.newString(buf); return(all.Split(new char[] { '\n' })); } } catch (SocketException) { if (traceLevel >= traceThreshold) { log.Debug("<- (no response)"); } throw new IOException("Nameserver not responding on " + address); } catch (IOException) { if (traceLevel >= traceThreshold) { log.Debug("<- (no response)"); } throw new IOException("Nameserver not responding when requesting names"); } catch (OtpErlangDecodeException) { if (traceLevel >= traceThreshold) { log.Debug("<- (invalid response)"); } throw new IOException("Nameserver not responding when requesting names"); } }