/// <summary> /// 命令轮询 /// </summary> /// <returns></returns> private void PollTask() { StationProgram.WriteLine($"【{StationName}】poll start"); var timeout = new TimeSpan(0, 0, 5); _inPoll = true; while (RunState == StationState.Run) { try { if (!_socket.TryReceiveFrameString(timeout, out var title, out var more) || !more) { continue; } if (!_socket.TryReceiveFrameBytes(out var description, out more) || !more) { continue; } PublishItem item = new PublishItem { Title = title }; int idx = 1; while (more) { if (!_socket.TryReceiveFrameString(out var val, out more)) { continue; } switch (description[idx++]) { case ZeroHelper.zero_pub_sub: item.SubTitle = val; break; case ZeroHelper.zero_pub_publisher: item.Station = val; break; case ZeroHelper.zero_arg: item.Content = val; break; } } _items.Push(item); } catch (Exception e) { StationProgram.WriteLine($"【{StationName}】poll error{e.Message}..."); LogRecorder.Exception(e); RunState = StationState.Failed; } } _inPoll = false; StationProgram.WriteLine($"【{StationName}】poll stop"); _items.Save(CacheFileName); CloseSocket(); }
/// <summary> /// 发送广播 /// </summary> /// <param name="item"></param> /// <param name="socket"></param> /// <returns></returns> private static bool Send(RequestSocket socket, PublishItem item) { try { lock (socket) { List <string> frames = new List <string> { item.Title }; socket.SendMoreFrame(item.Title); byte[] description = new byte[5]; description[0] = (byte)3; description[1] = ZeroHelper.zero_pub_publisher; description[2] = ZeroHelper.zero_pub_sub; description[3] = ZeroHelper.zero_arg; description[4] = ZeroHelper.zero_end; socket.SendMoreFrame(item.Title); socket.SendMoreFrame(description); socket.SendMoreFrame(StationProgram.Config.StationName); socket.SendMoreFrame(item.SubTitle); socket.SendFrame(item.Content); var word = socket.ReceiveFrameString(); StationProgram.WriteLine($"【{item.Station}-{item.Title}】{word}"); return(word == "ok"); } } catch (Exception e) { LogRecorder.Exception(e); StationProgram.WriteLine($"【{item.Station}-{item.Title}】request error =>{e.Message}"); } lock (Publishers) { Publishers.Remove(item.Station); } return(false); }
/*// <summary> * /// 命令处理方法 * /// </summary> * public Action<string> ExecFunc { get; set; } * * /// <summary> * /// 执行命令 * /// </summary> * /// <param name="args"></param> * /// <returns></returns> * public virtual void ExecCommand(string args) * { * ExecFunc?.Invoke(args); * }*/ /// <summary> /// 执行命令 /// </summary> /// <param name="args"></param> /// <returns></returns> public abstract void Handle(PublishItem args);