private SuperQNodeResponse get_msg(Socket socket) { // first byte will always be a marker to verify begining of Request byte[] data = recv(socket, 1); if (data.Count() != 1 || data[0] != 42) { throw new Exception("Marker byte not read. Bad message."); } // next 4 bytes must always be message body length data = recv(socket, 4); if (data.Count() != 4) { throw new Exception("Msg length not read. Bad message."); } // convert length int messageLength = BitConverter.ToInt32(data, 0); // now read the rest of the message data = recv(socket, messageLength); // decode character data string msg = Encoding.UTF8.GetString(data); // build response object from string SuperQNodeResponse response = new SuperQNodeResponse(); response.from_str(msg); return(response); }
public static bool superq_exists(string name, string host) { // build request object from string SuperQNodeRequest request = new SuperQNodeRequest(); request.cmd = "superq_exists"; request.args = name; SuperQNetworkClientMgr mgr = new SuperQNetworkClientMgr(); SuperQNodeResponse response = mgr.send_msg(host, request.ToString()); return(bool.Parse(response.result)); }
public static superq superq_query(superq sq, string query) { // build request object from string SuperQNodeRequest request = new SuperQNodeRequest(); request.cmd = "superq_query"; request.args = sq.publicName; request.body = query; SuperQNetworkClientMgr mgr = new SuperQNetworkClientMgr(); SuperQNodeResponse response = mgr.send_msg(sq.host, request.ToString()); if (bool.Parse(response.result) == false) { throw new Exception("Not sure what to raise here yet."); } return(new superq(response.body, "", "", "", false, true)); }
public static superq superq_read(string name, string host) { // build request object from string SuperQNodeRequest request = new SuperQNodeRequest(); request.cmd = "superq_read"; request.args = name; SuperQNetworkClientMgr mgr = new SuperQNetworkClientMgr(); SuperQNodeResponse response = mgr.send_msg(host, request.ToString()); if (bool.Parse(response.result) == false) { throw new Exception(name + " does not exist."); } // deserialize response body into a detached superq and return return(new superq(response.body, "", "", "", false, true)); }
private SuperQNodeResponse send_msg(string host, string msg) { // SSL support is not currently implemented bool ssl = false; int port = DEFAULT_TCP_PORT; // 'local' is shorthand for localhost:DEFAULT_PORT if (host == "local") { host = "localhost"; } else { if (host.StartsWith("ssl:")) { string[] elems = host.Split(':'); ssl = true; host = elems[1]; port = Int32.Parse(elems[2]); } else { try { string[] elems = host.Split(':'); host = elems[0]; port = Int32.Parse(elems[1]); } catch { port = DEFAULT_TCP_PORT; } } } // allocate buffer byte[] buf = new byte[5 + msg.Count()]; // set header byte buf[0] = 0x2A; // 42 // add message length BitConverter.GetBytes(msg.Count()).CopyTo(buf, 1); // add message Encoding.UTF8.GetBytes(msg).CopyTo(buf, 5); // open socket TcpClient client = new TcpClient(host, port); Socket socket = client.Client; // send message send(socket, buf); // get response SuperQNodeResponse response = get_msg(socket); // close socket client.Close(); return(response); }