private void ConnectionInit(InitPack pack) { hbtime = pack.hbinterval; conn_sid = pack.sid; logger("eventhost_connection", "basic connection good"); client.SendAsync(Encoding.UTF8.GetBytes("421[\"GetWebConn\",\"" + qq + "\"]"), WebSocketMessageType.Text, true, ctoken).Wait(); logger("eventhost_connection", "iotq handshake sent"); }
/// <summary> /// IOTQ协议 事件主机 /// </summary> /// <param name="host">目标IP或主机地址</param> /// <param name="qq">当前登录的qq</param> /// <param name="threadpool">事件发生时用于处理的线程池</param> /// <param name="logger">日志管理器,将处理所有日志信息</param> /// <param name="buffersize">接收缓冲区大小 过大导致内存浪费,过小导致多次循环接收</param> public EventHost(string host, long qq, pThreadPool threadpool, logger_ logger, int buffersize = 1024) { start = DateTime.Now; this.logger = logger; this.threadpool = threadpool; this.host = host; heartbeat = new Task(() => { while (client.State != WebSocketState.Open) { Thread.Sleep(500); } logger("eventhost_connection", "HB manager started."); while (!ctoken.IsCancellationRequested) { try { logger("eventhost_connection", "<heartbeat> sent"); client.SendAsync(Encoding.UTF8.GetBytes("2"), WebSocketMessageType.Text, true, ctoken).Wait(); Thread.Sleep(hbtime); } catch (Exception err) { Die(); logger("eventhost_fatal", "heartbeat manager crashed:" + err.Message, ConsoleColor.DarkRed); logger("eventhost_fatal", "recovering..."); conn(host, logger); Thread.Sleep(1000); } } logger("eventhost_connection", "HB manager cancelled by token."); }); receiver = new Task(() => { while (client.State != WebSocketState.Open) { Thread.Sleep(500); } logger("eventhost_receiver", "started."); while (!ctoken.IsCancellationRequested) { try { StringBuilder sb = new StringBuilder(); Task <WebSocketReceiveResult> t; do { byte[] buffer = new byte[buffersize]; t = client.ReceiveAsync(buffer, ctoken); t.Wait(); //logger("eventhost_receiver_debug", "new pack comming in, SIZE=" + t.Result.Count); byte[] bRec = new byte[t.Result.Count]; Array.Copy(buffer, bRec, t.Result.Count); sb.Append(Encoding.UTF8.GetString(bRec)); /* * if (!t.Result.EndOfMessage) * { * logger("eventhost_receiver_debug", " - Large pack truncated, receiving next part...", ConsoleColor.DarkBlue); * } */ } while (!t.Result.EndOfMessage); if (InitPack.isInitPack(sb.ToString())) { threadpool.submitWorkload(() => { InitPack i = new InitPack(sb.ToString()); onConnectionInit.Invoke(i); }); } else { threadpool.submitWorkload(() => { NetPack packet = new NetPack(sb.ToString()); if (packet.code == 3 && (packet.eventstr == null || packet.eventstr.Length < 1)) { //心跳包 } else { onEventTrigger.Invoke(packet); } }); } /* * if (pack.IndexOf("[\"OK\"]") > -1) * { * logger("eventhost_connection", "ready."); * ready = true; * } * if(pack.IndexOf("[\"当前已存在活动的WebSocket 已为您切换当前Socket\"]") > -1) * { * logger("eventhost_connection", "TARGET_SWITCHED - ready."); * ready = true; * } * if (pack.IndexOf("[\"尚未在线\"]") > -1) { * logger("eventhost_fatal", "Target not online. Will deleberately crash."); * ready = false; * throw new Exception("Target not online. Try again later."); * } */ //logger("eventhost_receiver", "pack=" + pack); } catch (Exception err) { Die(); logger("eventhost_fatal", "main receiver crashed:" + err.Message, ConsoleColor.DarkRed); logger("eventhost_fatal", "recovering..."); conn(host, logger); Thread.Sleep(500); } } logger("eventhost_receiver", "cancelled by ctoken.", ConsoleColor.DarkBlue); }); this.qq = qq; conn(host, logger); onConnectionInit += ConnectionInit; onEventTrigger += PackRecv; if (heartbeat.Status != TaskStatus.Running) { heartbeat.Start(); } if (receiver.Status != TaskStatus.Running) { receiver.Start(); } }