private bool isDataOrThrow(ObjectMap resp) { bool ret = false; if (!resp.ContainsKey("result_code")) { throw new SFQueueClientException(1050); } int rc = int.Parse(resp.s("result_code")); if (rc == SFQ_RC_SUCCESS) { ret = true; } else { string msg = "* Un-Known *"; if (resp.ContainsKey("error_message")) { msg = resp.s("error_message"); } LastError = rc; LastMessage = "remote error rc=" + rc + " msg=" + msg; if (rc >= SFQ_RC_FATAL_MIN) { throw new SFQueueClientException(2000 + rc, "remote error rc=" + rc + " msg=" + msg); } } return(ret); }
public static SFQueueClientInterface newClient(ObjectMap param) { if (param.ContainsKey("host")) { return(new SFQueueClientRemote(param)); } return(null); }
public bool Contains(object o) { return(_map.ContainsKey(o)); }
private ObjectMap remote_io(String opname, ObjectMap param) { var ret = new ObjectMap(); try { string host = (string)conn_params["host"]; var portMap = (Dictionary <String, int>)conn_params.GetOrDefault("port", new Dictionary <String, int>()); int port = portMap.GetOrDefault(opname); if (port == -1) { switch (opname) { case "push": { port = 12701; break; } case "pop": { port = 12711; break; } case "shift": { port = 12721; break; } default: { throw new Exception(opname + ": unknown operation"); } } } int timeout = conn_params.i("timeout", 2000); // var header_arr = new List <String>(); var copyKeys = new List <String>() { "querootdir", "quename", "eworkdir" }; var ignoreKeys = new List <String>() { "payload" }; // byte[] body = null; if (param.ContainsKey("payload")) { Object o = param["payload"]; if (o is String) { body = Encoding.UTF8.GetBytes((String)o); header_arr.Add("payload-type: text"); } else { body = (byte[])o; header_arr.Add("payload-type: binary"); } header_arr.Add("payload-length: " + body.Length); } // header_arr.AddRange ( conn_params.Keys .Where(e => copyKeys.Contains(e)) .Select(e => e.Replace("_", "-") + ": " + conn_params.s(e)) ); header_arr.AddRange ( param.Keys .Where(e => !ignoreKeys.Contains(e)) .Select(e => e.Replace("_", "-") + ": " + param.s(e)) ); //header_arr.ForEach(Console.WriteLine); byte[] respAll = null; string header = string.Join(CRLF, header_arr) + CRLF + CRLF; using (Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { sock.NoDelay = true; IPHostEntry hostent = Dns.GetHostEntry(host); sock.Connect(new System.Net.IPEndPoint(hostent.AddressList[0], port)); sock.ReceiveTimeout = timeout; sock.SendTimeout = timeout; sock.Send(Encoding.UTF8.GetBytes(header)); if (body != null) { sock.Send(body); } using (MemoryStream ms = new MemoryStream()) { using (BinaryWriter bw = new BinaryWriter(ms)) { byte[] buff = new byte[8192]; int rb = 0; do { rb = sock.Receive(buff); if (rb > 0) { bw.Write(buff, 0, rb); } }while (rb != 0); } respAll = ms.ToArray(); } ret = parse_response(respAll); } } catch (SFQueueClientException ex) { throw ex; } catch (Exception ex) { throw new SFQueueClientException(1050, ex.Message); } return(ret); }