/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object can be used to retrieve data from input parameters and /// to store data in output parameters.</param> protected override void SolveInstance(IGH_DataAccess DA) { switch (_status) { case Status.Idle: var botUrl = ""; DA.GetData(_xIn, ref botUrl); if (_socket == null && !string.IsNullOrEmpty(botUrl)) { OpenSocket(botUrl); } DA.SetData(_resultOut, _data); break; case Status.Busy: if (_socket == null) { AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "socket is null"); return; } _socket.Send(_data as string); _socket.Close(); _socket = null; _status = Status.Idle; ExpireSolution(true); break; default: throw new ArgumentOutOfRangeException(); } }
private void OnOpen() { LogManager.GetLogger(Global.CallerName("", 0, "")).Info("open"); this.Cleanup(); this.ReadyState = Manager.ReadyStateEnum.OPEN; this.Emit(Manager.EVENT_OPEN); Quobject.EngineIoClientDotNet.Client.Socket engineSocket = this.EngineSocket; this.Subs.Enqueue(ClientOn.Create((Emitter)engineSocket, Quobject.EngineIoClientDotNet.Client.Socket.EVENT_DATA, (IListener) new ListenerImpl((Action <object>)(data => { if (data is string) { this.OnData((string)data); } else { if (!(data is byte[])) { return; } this.Ondata((byte[])data); } })))); this.Subs.Enqueue(ClientOn.Create((Emitter)this.Decoder, "decoded", (IListener) new ListenerImpl((Action <object>)(data => this.OnDecoded((Packet)data))))); this.Subs.Enqueue(ClientOn.Create((Emitter)engineSocket, Quobject.EngineIoClientDotNet.Client.Socket.EVENT_ERROR, (IListener) new ListenerImpl((Action <object>)(data => this.OnError((Exception)data))))); this.Subs.Enqueue(ClientOn.Create((Emitter)engineSocket, Quobject.EngineIoClientDotNet.Client.Socket.EVENT_CLOSE, (IListener) new ListenerImpl((Action <object>)(data => this.OnClose((string)data))))); }
private void OpenSocket(string botUrl) { _botUrl = botUrl; var socket = new Socket(botUrl); _socket = socket; socket.On(Socket.EVENT_OPEN, () => { socket.On(Socket.EVENT_MESSAGE, (data) => { _data = data; _status = Status.Busy; ExpireSolution(true); }); }); socket.On(Socket.EVENT_CLOSE, () => { _socket = null; }); socket.Open(); }
private Manager Open(IOpenCallback fn) { var log = LogManager.GetLogger(Global.CallerName()); log.Info(string.Format("readyState {0}", ReadyState)); if (ReadyState == ReadyStateEnum.OPEN) { return(this); } log.Info(string.Format("opening {0}", Uri)); EngineSocket = new Engine(Uri, Opts); Quobject.EngineIoClientDotNet.Client.Socket socket = EngineSocket; ReadyState = ReadyStateEnum.OPENING; OpeningSockets.Add(Socket(Uri.PathAndQuery)); SkipReconnect = false; var openSub = SocketIoClientDotNet.Client.On.Create(socket, Engine.EVENT_OPEN, new ListenerImpl(() => { OnOpen(); if (fn != null) { fn.Call(null); } })); var errorSub = Client.On.Create(socket, Engine.EVENT_ERROR, new ListenerImpl((data) => { log.Info("connect_error"); Cleanup(); ReadyState = ReadyStateEnum.CLOSED; EmitAll(EVENT_CONNECT_ERROR, data); if (fn != null) { var err = new SocketIOException("Connection error", data is Exception ? (Exception)data : null); fn.Call(err); } MaybeReconnectOnOpen(); })); if (_timeout >= 0) { var timeout = (int)_timeout; log.Info(string.Format("connection attempt will timeout after {0}", timeout)); var timer = EasyTimer.SetTimeout(() => { var log2 = LogManager.GetLogger(Global.CallerName()); log2.Info("Manager Open start"); log2.Info(string.Format("connect attempt timed out after {0}", timeout)); openSub.Destroy(); socket.Close(); socket.Emit(Engine.EVENT_ERROR, new SocketIOException("timeout")); EmitAll(EVENT_CONNECT_TIMEOUT, timeout); log2.Info("Manager Open finish"); }, timeout); Subs.Enqueue(new On.ActionHandleImpl(timer.Stop)); ; } Subs.Enqueue(openSub); Subs.Enqueue(errorSub); EngineSocket.Open(); return(this); }
private Manager Open(IOpenCallback fn) { var log = LogManager.GetLogger(Global.CallerName()); log.Info(string.Format("readyState {0}", ReadyState)); if (ReadyState == ReadyStateEnum.OPEN) { return this; } log.Info(string.Format("opening {0}", Uri)); EngineSocket = new Engine(Uri, Opts); Quobject.EngineIoClientDotNet.Client.Socket socket = EngineSocket; ReadyState = ReadyStateEnum.OPENING; var openSub = SocketIoClientDotNet.Client.On.Create(socket, Engine.EVENT_OPEN, new ListenerImpl(() => { OnOpen(); if (fn != null) { fn.Call(null); } })); var errorSub = Client.On.Create(socket, Engine.EVENT_ERROR, new ListenerImpl((data) => { log.Info("connect_error"); Cleanup(); ReadyState = ReadyStateEnum.CLOSED; EmitAll(EVENT_CONNECT_ERROR, data); if (fn != null) { var err = new SocketIOException("Connection error", data is Exception ? (Exception) data : null); fn.Call(err); } MaybeReconnectOnOpen(); })); if (_timeout >= 0) { var timeout = (int) _timeout; log.Info(string.Format("connection attempt will timeout after {0}", timeout)); var timer = EasyTimer.SetTimeout(() => { var log2 = LogManager.GetLogger(Global.CallerName()); log2.Info("Manager Open start"); log2.Info(string.Format("connect attempt timed out after {0}", timeout)); openSub.Destroy(); socket.Close(); socket.Emit(Engine.EVENT_ERROR, new SocketIOException("timeout")); EmitAll(EVENT_CONNECT_TIMEOUT, timeout); log2.Info("Manager Open finish"); }, timeout); Subs.Enqueue(new On.ActionHandleImpl(timer.Stop)); ; } Subs.Enqueue(openSub); Subs.Enqueue(errorSub); EngineSocket.Open(); return this; }
public override void RemovedFromDocument(GH_Document document) { base.RemovedFromDocument(document); _socket.Close(); _socket = null; }
private Manager Open(Manager.IOpenCallback fn) { LogManager log = LogManager.GetLogger(Global.CallerName("", 0, "")); log.Info(string.Format("readyState {0}", (object)this.ReadyState)); if (this.ReadyState == Manager.ReadyStateEnum.OPEN) { return(this); } log.Info(string.Format("opening {0}", (object)this.Uri)); this.EngineSocket = (Quobject.EngineIoClientDotNet.Client.Socket) new Engine(this.Uri, this.Opts); Quobject.EngineIoClientDotNet.Client.Socket socket = this.EngineSocket; this.ReadyState = Manager.ReadyStateEnum.OPENING; this.OpeningSockets.Add(this.Socket(this.Uri.PathAndQuery)); this.SkipReconnect = false; ClientOn.IHandle openSub = ClientOn.Create((Emitter)socket, EngineIoClientDotNet.Client.Socket.EVENT_OPEN, (IListener) new ListenerImpl((ActionTrigger)(() => { this.OnOpen(); if (fn == null) { return; } fn.Call((Exception)null); }))); ClientOn.IHandle handle = ClientOn.Create((Emitter)socket, EngineIoClientDotNet.Client.Socket.EVENT_ERROR, (IListener) new ListenerImpl((Action <object>)(data => { log.Info("connect_error"); this.Cleanup(); this.ReadyState = Manager.ReadyStateEnum.CLOSED; this.EmitAll(Manager.EVENT_CONNECT_ERROR, data); if (fn != null) { fn.Call((Exception) new SocketIOException("Connection error", data is Exception ? (Exception)data : (Exception)null)); } else { this.MaybeReconnectOnOpen(); } }))); if (this._timeout >= 0L && this.ReadyState == Manager.ReadyStateEnum.CLOSED) { int timeout = (int)this._timeout; log.Info(string.Format("connection attempt will timeout after {0}", (object)timeout)); this.Subs.Enqueue((ClientOn.IHandle) new ClientOn.ActionHandleImpl(new ActionTrigger(EasyTimer .SetTimeout((ActionTrigger)(() => { LogManager logger = LogManager.GetLogger(Global.CallerName("", 0, "")); logger.Info("Manager Open start"); logger.Info(string.Format("connect attempt timed out after {0}", (object)timeout)); openSub.Destroy(); socket.Close(); socket.Emit(Quobject.EngineIoClientDotNet.Client.Socket.EVENT_ERROR, (object)new SocketIOException("timeout")); this.EmitAll(Manager.EVENT_CONNECT_TIMEOUT, (object)timeout); logger.Info("Manager Open finish"); }), timeout).Stop))); } this.Subs.Enqueue(openSub); this.Subs.Enqueue(handle); this.EngineSocket.Open(); return(this); }