public Manager(Uri uri, Options opts) { LogManager.GetLogger(Global.CallerName("", 0, "")).Info("Init Manager: " + (object)uri); if (opts == null) { opts = new Options(); } if (opts.Path == null) { opts.Path = "/socket.io"; } this.Opts = (Quobject.EngineIoClientDotNet.Client.Socket.Options)opts; this.Nsps = ImmutableDictionary.Create <string, QSocket>(); this.Subs = new ConcurrentQueue <ClientOn.IHandle>(); this.Reconnection(opts.Reconnection); this.ReconnectionAttempts(opts.ReconnectionAttempts != 0 ? opts.ReconnectionAttempts : int.MaxValue); this.ReconnectionDelay(opts.ReconnectionDelay != 0L ? opts.ReconnectionDelay : 1000L); this.ReconnectionDelayMax(opts.ReconnectionDelayMax != 0L ? opts.ReconnectionDelayMax : 5000L); this.Timeout(opts.Timeout < 0L ? 20000L : opts.Timeout); this.ReadyState = Manager.ReadyStateEnum.CLOSED; this.Uri = uri; this.Attempts = 0; this.Encoding = false; this.PacketBuffer = new List <Packet>(); this.OpeningSockets = new HashSet <QSocket>(); this.Encoder = new Quobject.SocketIoClientDotNet.Parser.Parser.Encoder(); this.Decoder = new Quobject.SocketIoClientDotNet.Parser.Parser.Decoder(); this.AutoConnect = opts.AutoConnect; if (!this.AutoConnect) { return; } this.Open(); }
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 OnClose(string reason) { LogManager.GetLogger(Global.CallerName("", 0, "")).Info("start"); this.Cleanup(); this.ReadyState = Manager.ReadyStateEnum.CLOSED; this.Emit(Manager.EVENT_CLOSE, (object)reason); if (!this._reconnection || this.SkipReconnect) { return; } this.Reconnect(); }
public void Close() { this.SkipReconnect = true; this.Reconnecting = false; if (this.ReadyState != Manager.ReadyStateEnum.OPEN) { this.Cleanup(); } LogManager.GetLogger(Global.CallerName("", 0, "")).Info("Manager.Close()"); this.ReadyState = Manager.ReadyStateEnum.CLOSED; if (this.EngineSocket == null) { return; } this.EngineSocket.Close(); }
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); }