public static Payload makePayload() { Payload p = new Payload(); p.path = new List<String>(); p.payload = new Dictionary<String, Object>(); p.request = ""; p.sessionid = 0; p.identity = 0; p.sessionkey = ""; return p; }
private bool StopTalking() { audioCaller.Close(); connector.ReceivedData -= CancelRequested; //Stop recording Payload p = new Payload(); p.request = "cancel"; p.sessionkey = client.SessionKey; p.sessionid = client.SessionID; p.path = ClassRequested.path.Concat(new String[] {"audio"}).ToList(); Random rnd = new Random(); p.identity = packetIdentity = rnd.Next(Int32.MaxValue); ResponsePayload r = new ResponsePayload(); r.message = "Connection: timeout!"; Task k = Task.Factory.StartNew(() => { r = connector.SendAndReceive(p); }); k.Wait(5000); if (r.status == "success") { return true; } else { client.currentError = r.message; return false; } }
public bool CancelRequest() { if (CurrentAsk == null) return false; CurrentAsk.stopListening(); CurrentAsk = null; RaisePropertyChanged("CurrentAsk"); //Send request to remove from queue here! Payload p = new Payload(); p.request = "cancel"; p.sessionkey = client.SessionKey; p.sessionid = client.SessionID; p.path = path.Concat(new String[] {"audio"}).ToList(); Random rnd = new Random(); p.identity = rnd.Next(Int32.MaxValue); ResponsePayload r = new ResponsePayload(); r.message = "Connection: timeout!"; connector.Send (p); return true; }
// // // public void SendBlock(Payload payload) { String s = JsonConvert.SerializeObject(payload); byte[] sendbuf = Encoding.ASCII.GetBytes(s); if (sendbuf.Length > 4096) throw new Exception("Packet size too large!"); Socket.Send(sendbuf, sendbuf.Length); }
/* SendAndReceive(Payload p) * Blocks until response is received * * Has timeout of 10 second for identity = 0 * Unlimited timeout for specific identity, please timeout the thread manually. * */ public ResponsePayload SendAndReceive(Payload p) { if (p.identity != 0) { SendBlock(p); while (responseWaiter == null || responseWaiter.identity != p.identity) { } return responseWaiter; } else { SendBlock(p); return waitForPayload(); } }
/* SendAndForget neithers blocks the thread nor wait for response * The client also timeouts in 3 seconds.*/ public void Send(Payload p) { Task tk = Task.Factory.StartNew(() => { try { SendBlock(p); } catch { } }); }