public int CacheScript(int webContentsId, string script) { ManualResetEvent resetEvent = new ManualResetEvent(false); _resetEvents.Add(resetEvent); int value = 0; SocketronData data = new SocketronData() { Func = "cacheScript", Args = new object[] { script }, WebContentsId = webContentsId }; Emit("text", data, (Callback)((result) => { value = Convert.ToInt32(result); resetEvent.Set(); }), (Callback)((result) => { Console.Error.WriteLine("error: " + GetType().Name + ".CacheScript"); throw new InvalidOperationException(result as string); })); resetEvent.WaitOne(); _resetEvents.Remove(resetEvent); return(value); }
protected void _OnText(object[] args) { SocketronData data = args[0] as SocketronData; Callback success = args[1] as Callback; Callback error = args[2] as Callback; if (success != null) { int id = _sequenceId; if (_freeIds.Count > 0) { id = _freeIds.Pop(); } else { _sequenceId++; } data.SequenceId = id; _successList[id] = success; if (error != null) { _errorList[id] = error; } } if (LocalConfig.IsDebug && LocalConfig.EnableDebugPayloads) { _DebugLog("send: {0}", data.Stringify()); } Write(data.ToBuffer(DataType.Text16, LocalConfig.Encoding)); }
public void InsertCSS(string css, Callback success = null) { SocketronData data = new SocketronData() { Func = "insertCSS", Args = new object[] { css } }; Emit("text", data, success, null); }
public void InsertJavaScript(string url, Callback success = null) { SocketronData data = new SocketronData() { Func = "insertJavaScript", Args = new object[] { url } }; Emit("text", data, success, null); }
public void ExecuteJavaScript(int webContentsId, string script, Callback success = null, Callback error = null) { SocketronData data = new SocketronData() { Func = "executeJavaScript", Args = new object[] { script }, WebContentsId = webContentsId }; Emit("text", data, success, error); }
public void ExecuteJavaScript(string script, Callback success = null, Callback error = null) { SocketronData data = new SocketronData() { Type = Type, Func = "executeJavaScript", Args = new object[] { script } }; Emit("text", data, success, error); }
public static new SocketronData Parse(string json) { JsonObject jsonObject = JsonObject.Parse(json); SocketronData data = new SocketronData() { Status = jsonObject["status"] as string, Type = jsonObject["type"] as string, Func = jsonObject["func"] as string, Args = jsonObject["args"] as object }; if (jsonObject["sequenceId"] != null) { int sequenceId = (int)jsonObject["sequenceId"]; data.SequenceId = (ushort)sequenceId; } return(data); }
public T ExecuteCachedScript <T>(int webContentsId, int script) { ManualResetEvent resetEvent = new ManualResetEvent(false); _resetEvents.Add(resetEvent); T value = default(T); Type typeofT = typeof(T); SocketronData data = new SocketronData() { Func = "executeCachedScript", Args = new object[] { script }, WebContentsId = webContentsId }; Emit("text", data, (Callback)((result) => { if (result == null) { resetEvent.Set(); return; } if (typeofT == typeof(object)) { value = (T)result; resetEvent.Set(); return; } value = (T)Convert.ChangeType(result, typeofT); resetEvent.Set(); }), (Callback)((result) => { Console.Error.WriteLine("error: " + GetType().Name + ".ExecuteCachedScript"); throw new InvalidOperationException(result as string); })); resetEvent.WaitOne(); _resetEvents.Remove(resetEvent); return(value); }
protected void _OnCallback(SocketronData data) { if (data.SequenceId == null) { if (data.Status == "error") { throw new InvalidOperationException(data.Args as string); } return; } int sequenceId = (int)data.SequenceId; if (data.Status == "error") { if (_errorList.ContainsKey(sequenceId)) { Callback error = _errorList[sequenceId]; error?.Invoke(data.Args); _successList.Remove(sequenceId); _errorList.Remove(sequenceId); } else { throw new InvalidOperationException(data.Args as string); } _freeIds.Push(sequenceId); return; } if (_successList.ContainsKey(sequenceId)) { Callback success = _successList[sequenceId]; success?.Invoke(data.Args); _successList.Remove(sequenceId); _errorList.Remove(sequenceId); } _freeIds.Push(sequenceId); }
protected void _OnData(object[] args) { SocketronData data = args[0] as SocketronData; if (data == null) { return; } //Console.WriteLine("data.Func: " + data.Func); //Console.WriteLine("data.Function: " + data.Function); //Console.WriteLine("data.Arguments: " + data.Arguments); //Console.WriteLine("data.SequenceId: " + data.SequenceId); //Console.WriteLine("sequenceId: " + data.SequenceId); switch (data.Func) { case "id": ID = data.Args as string; //DebugLog("ID: {0}", ID); break; case "config": RemoteConfig = new JsonObject(data.Args); _DebugLog(RemoteConfig.Stringify()); //DebugLog("ID: {0}", ID); break; case "callback": _OnCallback(data); break; case "event": _OnEvent(data); break; } }
protected void _OnData(byte[] data, int bytesReaded) { if (data != null) { _payload.Data.Write(data, 0, bytesReaded); } uint offset = _payload.DataOffset; uint remain = (uint)_payload.Data.Length - offset; switch (_payload.State) { case ReadState.Type: if (remain < 1) { return; } break; case ReadState.CommandLength: switch (_payload.DataType) { case DataType.Text16: if (remain < 2) { return; } break; case DataType.Text32: if (remain < 4) { return; } break; } break; case ReadState.Command: if (remain < _payload.DataLength) { return; } break; } switch (_payload.State) { case ReadState.Type: _payload.DataType = (DataType)_payload.Data[offset]; _payload.DataOffset += 1; _payload.State = ReadState.CommandLength; //Debug.WriteLine("_payload.DataType: " + _payload.DataType); break; case ReadState.CommandLength: switch (_payload.DataType) { case DataType.Text16: _payload.DataLength = _payload.Data.ReadUInt16LE(offset); _payload.DataOffset += 2; break; case DataType.Text32: _payload.DataLength = _payload.Data.ReadUInt32LE(offset); _payload.DataOffset += 4; break; } //Debug.WriteLine("_payload.DataLength: " + _payload.DataLength); _payload.State = ReadState.Command; break; case ReadState.Command: switch (_payload.DataType) { case DataType.Text16: case DataType.Text32: string text = _payload.GetStringData(); if (Config.IsDebug && Config.EnableDebugPayloads) { _DebugLog("receive: {0}", text); } EmitNewThread("data", SocketronData.Parse(text)); break; } var newData = _payload.Data.Slice(offset + _payload.DataLength); _payload.Data.Dispose(); _payload.Data = newData; _payload.DataOffset = 0; _payload.State = ReadState.Type; break; } _OnData(null, 0); }
protected void _OnEvent(SocketronData data) { JsonObject json = new JsonObject(data.Args); //DebugLog("Return: {0}", data.Arguments[0].GetType()); string eventName = json["name"] as string; object args = json["args"]; //_DebugLog("Return: {0}: {1}", eventName, args); if (eventName == "__event") { Thread thread = Thread.CurrentThread; if (!_Clients.ContainsKey(thread)) { _Clients.Add(thread, this); } object[] list = args as object[]; int instanceId = Convert.ToInt32(list[0]); string eventId = list[1] as string; int callbackId = Convert.ToInt32(list[2]); object[] callbackParams = null; const int baseCount = 3; if (list.Length > baseCount) { callbackParams = new object[list.Length - baseCount]; Array.Copy(list, baseCount, callbackParams, 0, list.Length - baseCount); } /* * Callback callback = null; * * className = "Socketron." + Char.ToUpper(className[0]) + className.Substring(1); * Type type = GetType().Assembly.GetType(className); * if (type == null) { * _Clients.Remove(thread); * return; * } * MethodInfo method = type.GetMethod( * "GetCallbackFromId", * BindingFlags.Static | BindingFlags.Public * ); * if (method == null) { * _Clients.Remove(thread); * return; * } * callback = method.Invoke(null, new[] { (object)callbackId }) as Callback; * callback?.Invoke(callbackParams); * //*/ CallbackItem item = Callbacks.GetItem(instanceId, eventId, callbackId); item?.Callback?.Invoke(callbackParams); _Clients.Remove(thread); /* * switch (className) { * case BrowserWindow.Name: * callback = BrowserWindow.GetCallbackFromId((ushort)callbackId); * callback?.Invoke(callbackParams); * break; * case WebContents.Name: * callback = WebContents.GetCallbackFromId((ushort)callbackId); * callback?.Invoke(callbackParams); * break; * } * //*/ return; } if (args is object[]) { Thread thread = Thread.CurrentThread; if (!_Clients.ContainsKey(thread)) { _Clients.Add(thread, this); } Emit(eventName, args as object[]); _Clients.Remove(thread); return; } Emit(eventName, args); }