public void TestMemQueueOneThreadException() { var bill = new Bill { Count = 0 }; var memq = new MemQueue <Order>("Test3", x => { if (x.Id % 2 == 0) { throw new InvalidOperationException(); } else { bill.Count++; } }); memq.Start(); for (var i = 0; i < 100; i++) { memq.Enqueue(new Order { Id = i }); } while (!memq.IsEmpty) { } Thread.Sleep(10); Assert.Equal(50, bill.Count); memq.Close(); }
public static Int32 PopMsgFromMemQueue(MemQueue cMsgQueue, Message cMsg) { Int32 n32Get = cMsgQueue.GetFront(cMsg.GetMsgBuffer(), (UInt32)mMsgHeaderSize); if (0 != n32Get) { return(n32Get); } Int32 n32MsgSize = cMsg.GetMsgSize(); if (n32MsgSize >= cMsg.mMsgSize) { return(-2); } Int32 n32Pop = cMsgQueue.PopFront(cMsg.GetMsgBuffer(), (UInt32)n32MsgSize); if (0 != n32Pop) { return(-3); } cMsg.SetReadPos(mMsgHeaderSize); cMsg.SetWritePos(n32MsgSize); return(0); }
public void Start() { innerq = new MemQueue <Tuple <string, string> >("inner", Publish); server = new ResponseSocket(); publisher = new PublisherSocket(); publisher.Bind(publishEndPiont); server.Bind(serverEndPoint); recvThread = new Thread(Recv); recvThread.Start(); innerq.Start(); }
public void TestMemQueueOneThread() { var handler = new DefaultHandler <Order>(); var memq = new MemQueue <Order>("Test1", handler); Assert.Equal("Test1", memq.QueueName); Assert.True(memq.IsEmpty); memq.Start(); var tasks = new Task[100]; for (var i = 0; i < tasks.Length; i++) { var order = new Order { Id = i }; tasks[i] = new Task(() => memq.Enqueue(order)); } foreach (var t in tasks) { t.Start(); } Task.WaitAll(tasks); while (!memq.IsEmpty) { Thread.Sleep(10); } Thread.Sleep(20); Assert.Equal(100, handler.RequestCount); memq.Close(); memq.Enqueue(new Order { Id = 101 }); memq.Enqueue(new Order { Id = 102 }); memq.Enqueue(new Order { Id = 103 }); Assert.Equal(100, handler.RequestCount); Assert.True(memq.IsEmpty); }
private void DeQueueTransact(Transact transact) { MemQueue.DeQueueTransact(transact); }
public void EnQueueTransact(Transact transact) { MemQueue.EnQueueTransact(transact); }
/// <summary> /// 基本执行tcp操作 /// </summary> /// <param name="cmdTag">指令</param> /// <returns></returns> public static TcpResponse TcpSendCmdBase(CmdTag cmdTag) { int timeDelay = 0; bool waitRsp = false; TcpResponse rsp = new TcpResponse(); MemQueue <CmdTag> cmdlist = new MemQueue <CmdTag>(); TcpCommunication tcpService; CmdTag cmdRspTag; tcpService = new TcpCommunication(); timeDelay = 0; //待socket准备好 while (timeDelay < tcpService.TcpWait) { if ((tcpService.SOCKET_STATE == TcpCommunication.TCP_SocketState.SOCKET_CONNECTED) || (tcpService.SOCKET_STATE == TcpCommunication.TCP_SocketState.SOCKET_CLOSED)) { break; } Thread.Sleep(100); timeDelay = timeDelay + 1; } if (tcpService.SOCKET_STATE != TcpCommunication.TCP_SocketState.SOCKET_CONNECTED) { rsp.Description = "与网关通信失败!"; rsp.Status = false; return(rsp); } timeDelay = 0; byte[] cmd_send = Command.BuildPlatformCmd(cmdTag); tcpService.SendData(cmd_send, 0, cmd_send.Length); cmdRspTag = new CmdTag(); timeDelay = 0; while (timeDelay < tcpService.TcpWait) { if (tcpService.socketData.Buffer.Length >= CommandCommon.CMD_MIN_LENGTH) { Command.ParsePlatFormCmd(ref tcpService.socketData.Buffer, ref cmdlist, ""); while (cmdlist.Count > 0) { cmdRspTag = cmdlist.Get(); //非本设备的回复 if ((cmdRspTag.DeviceInfo.SerialLong != cmdTag.DeviceInfo.SerialLong)) { continue; } if ((cmdRspTag.CmdType == cmdTag.CmdType) && (cmdRspTag.CmdCode == cmdTag.CmdCode)) { waitRsp = true; break; } } } if (waitRsp == true) { break; } if (tcpService.SOCKET_STATE == TcpCommunication.TCP_SocketState.SOCKET_CLOSED) { break; } Thread.Sleep(100); timeDelay = timeDelay + 1; } tcpService.Close(); if (waitRsp == false) { rsp.Status = false; rsp.Description = "等待设备回复超时!"; return(rsp); } rsp.Status = false; rsp.Parameter = new byte[cmdRspTag.Param.Length]; if (rsp.Parameter.Length > 0) { Array.Copy(cmdRspTag.Param, rsp.Parameter, cmdRspTag.Param.Length); } switch (cmdRspTag.CmdState) { case CommandState.CMD_STATE_SUCCESS: { rsp.Status = true; rsp.Description = "设备回复成功"; break; } case CommandState.CMD_STATE_FAIL: { rsp.Description = "设备回复失败"; break; } case CommandState.CMD_STATE_NOT_SUPPORT: { rsp.Description = "设备回复不支持"; break; } default: { rsp.Description = "未知回复"; break; } } return(rsp); }