public void Connect() { try { IPEndPoint ep = new IPEndPoint(IPAddress.Parse(Host), Port); client_.Connect(ep); requestProcessor_.Start(); JObject obj = ControlledRequests.FormatMessage("request", "connection"); obj["signature"] = Guid.NewGuid().ToString(); byte[] bytes = Encoding.ASCII.GetBytes(obj.ToString()); bool state = false; HasCallbackBeenRecieved callbackstate = AddCallback(obj, (JObject message) => { RouterVpnManagerLogLibrary.Log(message["data"].ToString()); state = true; }); NetworkStream ns = client_.GetStream(); ns.Write(bytes, 0, bytes.Length); callbackstate.Wait(CallbackTimeout); if (!state) { throw new Exception("was not able to connect properly"); } } catch (Exception e) { RouterVpnManagerLogLibrary.Log(e.ToString()); throw e; } }
private HasCallbackBeenRecieved AddBroadcastCallback(JObject response) { HasCallbackBeenRecieved callbackRecieved = new HasCallbackBeenRecieved(); callbackRecieved.ResponseState = requestProcessor_.AddBroadcastHandleListener(response, callbackRecieved); callbackRecieved.SetupAsyncSignal(); return(callbackRecieved); }
public bool AddBroadcastHandleListener(JObject obj, HasCallbackBeenRecieved callback) { if (!this.broadcastCallback_.ContainsKey(obj)) { return(broadcastCallback_.TryAdd(obj, callback)); } return(false); }
private HasCallbackBeenRecieved AddCallback(JObject response, RequestProcessor.Callback callback) { HasCallbackBeenRecieved callbackRecieved = new HasCallbackBeenRecieved(); callbackRecieved.ResponseState = requestProcessor_.AddPrivateCallbackHandler(response, (JObject o) => { callback(o); callbackRecieved.SignalEvent.Set(); }); callbackRecieved.SetupAsyncSignal(); return(callbackRecieved); }
/// <summary> /// Sends json to the server /// </summary> /// <param name="obj">The Json object</param> /// <param name="callback">A callback function which will be called when a response is recived</param> /// <param name="broadcastCallback">if it's a broadcast callback it will return a HasCallbackBeenRecieved which will trigger when the broadcast is recived</param> /// <returns></returns> public HasCallbackBeenRecieved SendJson(JObject obj, RequestProcessor.Callback callback = null, bool broadcastCallback = false) { HasCallbackBeenRecieved state = new HasCallbackBeenRecieved(); JObject o = (JObject)obj.DeepClone(); if (client_.Connected) { o["signature"] = Guid.NewGuid().ToString(); NetworkStream ns = client_.GetStream(); if (broadcastCallback) { state = AddBroadcastCallback(o); } else if (callback != null) { state = AddCallback(o, callback); } byte[] bytes = Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(o)); ns.Write(bytes, 0, bytes.Length); } return(state); }