public async Task <database_response> SendRequestToSocket(byte[] req) { byte[] response = null; bool failed = false; Utils utils = new Utils(); using (var ws = new WebSocket(url)) { ws.OnError += (sender, e) => { failed = true; }; ws.OnMessage += (sender, e) => { response = e.RawData; }; ws.OnClose += (sender, e) => { }; ws.Connect(); try { ws.Send(req); } catch (Exception e) { Console.WriteLine(e); return(null); } while (response == null) { await Task.Delay(100); if (failed) { return(null); } } bzn_envelope env = new bzn_envelope(); database_response dbresp = new database_response(); using (var stream = new MemoryStream(response)) { env = bzn_envelope.Parser.ParseFrom(stream); dbresp = database_response.Parser.ParseFrom(env.DatabaseResponse); if (dbresp.Redirect != null) { this.url = $"ws://{dbresp.Redirect.LeaderHost}:{dbresp.Redirect.LeaderPort}"; return(await SendRequestToSocket(req)); } return(dbresp); } } }
public void RemoveNonceHeader(database_response res) { if (res.Header.DbUuid == this.uuid) { var nonce = res.Header.Nonce; try { if (this.nonceMap[nonce]) { this.nonceMap.Remove(nonce); } else { Console.WriteLine("Metadata layer: nonce doesn't belong to map. Was it terminated too early?"); } } catch (KeyNotFoundException) { Console.WriteLine("Metadata layer: nonce doesn't belong to map. Was it terminated too early?"); } } }