public QueryTimeout(QueryTimeout value) { /// Initiliaze the protected variables m_MsgHeader = new MsgHeader(); m_IsCommand = false; /// Copy the values m_MsgHeader = value.m_MsgHeader; }
public Resume(Resume value) { /// Initiliaze the protected variables m_MsgHeader = new MsgHeader(); m_IsCommand = true; /// Copy the values m_MsgHeader = value.m_MsgHeader; }
public ConfirmEventRequest(ConfirmEventRequest value) { /// Initiliaze the protected variables m_MsgHeader = new MsgHeader(); m_Body = new Body(); m_IsCommand = false; /// Copy the values m_MsgHeader = value.m_MsgHeader; m_Body = value.m_Body; }
public SetEmergency(SetEmergency value) { /// Initiliaze the protected variables m_MsgHeader = new MsgHeader(); m_Body = new Body(); m_IsCommand = true; /// Copy the values m_MsgHeader = value.m_MsgHeader; m_Body = value.m_Body; }
public RejectControl(RejectControl value) { /// Initiliaze the protected variables m_MsgHeader = new MsgHeader(); m_Body = new Body(); m_IsCommand = false; /// Copy the values m_MsgHeader = value.m_MsgHeader; m_Body = value.m_Body; }
public ReportTimeout(ReportTimeout value) { /// Initiliaze the protected variables m_MsgHeader = new MsgHeader(); m_Body = new Body(); m_IsCommand = false; /// Copy the values m_MsgHeader = value.m_MsgHeader; m_Body = value.m_Body; }
public CreateCommandEvent(CreateCommandEvent value) { /// Initiliaze the protected variables m_MsgHeader = new MsgHeader(); m_Body = new Body(); m_IsCommand = true; /// Copy the values m_MsgHeader = value.m_MsgHeader; m_Body = value.m_Body; }
public QueryEvents(QueryEvents value) { /// Initiliaze the protected variables m_MsgHeader = new MsgHeader(); m_Body = new Body(); m_IsCommand = false; /// Copy the values m_MsgHeader = value.m_MsgHeader; m_Body = value.m_Body; }
public SetAuthority(SetAuthority value) { /// Initiliaze the protected variables m_MsgHeader = new MsgHeader(); m_Body = new Body(); m_IsCommand = true; /// Copy the values m_MsgHeader = value.m_MsgHeader; m_Body = value.m_Body; }
public RequestControl(RequestControl value) { /// Initiliaze the protected variables m_MsgHeader = new MsgHeader(); m_Body = new Body(); m_IsCommand = true; /// Copy the values m_MsgHeader = value.m_MsgHeader; m_Body = value.m_Body; }
public ClearEmergency(ClearEmergency value) { /// Initiliaze the protected variables m_MsgHeader = new MsgHeader(); m_Body = new Body(); m_IsCommand = true; /// Copy the values m_MsgHeader = value.m_MsgHeader; m_Body = value.m_Body; }
public ReportIdentification(ReportIdentification value) { /// Initiliaze the protected variables m_MsgHeader = new MsgHeader(); m_Body = new Body(); m_IsCommand = false; /// Copy the values m_MsgHeader = value.m_MsgHeader; m_Body = value.m_Body; }
private void CmdProcStopStreamingReply(MsgHeader pkt) { if (pkt is not StopStreamingReply msg) { return; } if (stopStreamingWaitHandle.Set()) { eventAggregator.GetEvent <StopStreamingReplyEvent>().Publish(msg); } }
private void CmdProcLensArgsReply(MsgHeader pkt) { if (pkt is not LensArgsReply msg) { return; } if (lenArgsWaitHandle.Set()) { this.eventAggregator.GetEvent <LensArgsReplyEvent>().Publish(msg); } }
/// <summary> /// Parses for MsgHeader and MsgStatus /// </summary> /// <param name="bytes">A byte array of a serialized MsgStatus/MsgHeader class.</param> /// <param name="byteCount">Count of bytes in the serialized array.</param> public HeaderStatusReader(byte[] bytes, int byteCount) { // basic validation first to help fail fast if (bytes == null || bytes.Length == 0) { throw new NATSInvalidHeaderException(SerializedHeaderCannotBeNullOrEmpty); } if (bytes.Length < byteCount) { throw new NATSInvalidHeaderException("invalid byte count"); } if (byteCount < MinimalValidHeaderLen) { throw new NATSInvalidHeaderException(); } // is tis the correct version for (int x = 0; x < HeaderVersionBytesLen; x++) { if (bytes[x] != HeaderVersionBytes[x]) { throw new NATSInvalidHeaderException(InvalidHeaderVersion); } } // does the header end properly _serializedLength = byteCount; Token terminus = new Token(bytes, _serializedLength, _serializedLength - 2, TokenType.Crlf); Token token = new Token(bytes, _serializedLength, HeaderVersionBytesLen, null); _header = new MsgHeader(); bool hadStatus = false; if (token.IsType(TokenType.Space)) { token = InitStatus(bytes, _serializedLength, token); if (token.SamePoint(terminus)) { return; // status only } hadStatus = true; } if (token.IsType(TokenType.Crlf)) { InitHeader(bytes, _serializedLength, token, hadStatus); } else { throw new NATSInvalidHeaderException(InvalidHeaderComposition); } }
/// <summary> /// 将消息按JT/T 808协议进行编码为字节数组 /// </summary> /// <returns></returns> public byte[] Encode() { if (MsgHeader == null) { throw new Exception("Field: msgHead cannot be null."); } // 消息头编码 byte[] msgHeadBytes = MsgHeader.Encode(); // body byte[] msgBodyBytes = null; if (MsgBody != null) { msgBodyBytes = MsgBody; } // 构建消息(不包含校验码和头尾标识) byte[] message = null; if (msgBodyBytes != null) { ByteBuffer msgbuf = ByteBuffer.Allocate( msgHeadBytes.Length + msgBodyBytes.Length); msgbuf.Put(msgHeadBytes); msgbuf.Put(msgBodyBytes); message = msgbuf.Array(); } else { message = msgHeadBytes; } // 计算校验码 byte checkCode = JT808ProtoDecoder.CheckCode(message); ByteBuffer checkedBuffer = ByteBuffer.Allocate(message.Length + 1); checkedBuffer.Put(message); checkedBuffer.Put(checkCode); byte[] checkedMessage = checkedBuffer.Array(); // 转义 byte[] escapedMessage = JT808ProtoDecoder.Escape(checkedMessage); // 增加标识位 ByteBuffer buffer = ByteBuffer.Allocate(escapedMessage.Length + 2); buffer.Put(JT808Constant.PKG_DELIMITER); buffer.Put(escapedMessage); buffer.Put(JT808Constant.PKG_DELIMITER); return(buffer.Array()); }
public void TestHeaderCopyConstructor() { var mh = new MsgHeader(); mh["foo"] = "bar"; var mh2 = new MsgHeader(mh); Assert.Equal("bar", mh2["foo"]); Assert.Throws <ArgumentNullException>(() => new MsgHeader(null)); Assert.Throws <ArgumentException>(() => new MsgHeader(new MsgHeader())); }
static KeyValueUtil() { DeleteHeaders = new MsgHeader { { KvOperationHeaderKey, KeyValueOperation.Delete.HeaderValue } }; PurgeHeaders = new MsgHeader { { KvOperationHeaderKey, KeyValueOperation.Purge.HeaderValue }, { JetStreamConstants.RollupHeader, JetStreamConstants.RollupHeaderSubject } }; }
private PublishAck PublishSyncInternal(string subject, byte[] data, MsgHeader hdr, PublishOptions options) { MsgHeader merged = MergePublishOptions(hdr, options); Msg msg = new Msg(subject, null, merged, data); if (JetStreamOptions.IsPublishNoAck) { Conn.Publish(msg); return(null); } return(ProcessPublishResponse(Conn.Request(msg), options)); }
void OnData(DataStreamReader stream, int i) { NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); stream.ReadBytes(bytes); string message = Encoding.ASCII.GetString(bytes.ToArray()); MsgHeader header = JsonUtility.FromJson <MsgHeader>(message); if ((header.cmd & Commands.PLAYER_UPDATE) == Commands.PLAYER_UPDATE) { ProcessPlayerMessage(message); } }
private void CmdProcDisconnectCameraReply(MsgHeader pkt) { if (pkt is not DisconnectCameraReply msg) { return; } disconnectCameraAck = msg.Ack == 0; if (disconnectCameraWaitHandle.Set()) { eventAggregator.GetEvent <DisconnectCameraReplyEvent>().Publish(msg); } }
private void CmdProcCaptureReply(MsgHeader pkt) { if (pkt is not CaptureReply msg) { return; } this.captureAck = msg.ACK; if (captureWaitHandle.Set()) { eventAggregator.GetEvent <CaptureReplyEvent>().Publish(msg); } }
/// <summary> /// /// </summary> public void QueryTopInfo() { MsgHeader mh = new MsgHeader(); //参数赋值 mh.MsgID = ConstIDs.O_TDMOM_TOP_INFO_REQ; mh.puData = 0; mh.DataLen = 0; mh.MsgLen = (uint)Marshal.SizeOf(mh); byte[] res_mh = StructConverter.StructToBytes(mh); }
void OnData(DataStreamReader stream) { NativeArray <byte> bytes = new NativeArray <byte>(stream.Length, Allocator.Temp); stream.ReadBytes(bytes); string recMsg = Encoding.ASCII.GetString(bytes.ToArray()); MsgHeader header = JsonUtility.FromJson <MsgHeader>(recMsg); if ((header.cmd & Commands.SERVER_UPDATE) == Commands.SERVER_UPDATE) { ProcessServerMessage(recMsg); } }
private void CmdProcConfigAlgReply(MsgHeader pkt) { if (pkt is not ConfigAlgReply msg) { return; } configAlgAck = msg.ConfigAck == 0; if (configAlgWaitHandle.Set()) { eventAggregator.GetEvent <ConfigAlgReplyEvent>().Publish(msg); } }
private void CmdProConnectCameraReply(MsgHeader pkt) { if (pkt is not ConnectCameraReply msg) { return; } this.CamChipID = msg.ToFChipID; if (connectCameraWaitHandle.Set()) { eventAggregator.GetEvent <ConnectCameraReplyEvent>().Publish(msg); } }
private void CmdProConfigCameraReply(MsgHeader pkt) { if (!(pkt is ConfigCameraReply msg)) { return; } configCameraSuccess = msg.ConfigAck; if (configCameraWaitHandle.Set()) { eventAggregator.GetEvent <ConfigCameraReplyEvent>().Publish(msg); } }
/// <summary> /// /// </summary> public void QueryRouteInfo() { MsgHeader mh = new MsgHeader(); //参数赋值 mh.MsgID = ConstIDs.O_TDMOM_ROUTE_INFO_REQ; mh.puData = 0; mh.DataLen = 0; mh.MsgLen = (uint)Marshal.SizeOf(mh); byte[] res_mh = StructConverter.StructToBytes(mh); //发送 }
public void TestHeaderMultiValueSerialization() { string headers = $"NATS/1.0\r\nfoo:bar\r\nfoo:baz,comma\r\n\r\n"; byte[] headerBytes = Encoding.UTF8.GetBytes(headers); var mh = new MsgHeader(headerBytes, headerBytes.Length); byte[] bytes = mh.ToByteArray(); Assert.True(bytes.Length == headerBytes.Length); for (int i = 0; i < bytes.Length; i++) { Assert.True(headerBytes[i] == bytes[i]); } }
private void Init() { JSONNode miNode = JsonNode[ApiConstants.Message]; Subject = miNode[ApiConstants.Subject].Value; Sequence = miNode[ApiConstants.Seq].AsUlong; Time = JsonUtils.AsDate(miNode[ApiConstants.Time]); Data = JsonUtils.AsByteArrayFromBase64(miNode[ApiConstants.Data]); byte[] bytes = JsonUtils.AsByteArrayFromBase64(miNode[ApiConstants.Hdrs]); if (bytes != null) { Headers = new HeaderStatusReader(bytes, bytes.Length).Header; } }
public override bool SendTo(ClientId id, IMessage msg) { var cl = clients[id]; if (cl.DeferredDisconnect) { Root.Log(LogLevel.Debug, "Server: attempt to send message to disconnected client"); return(false); } using (var rawBuf = new MemoryStream()) { var buf = rawBuf.AsUnclosable(); var header = new MsgHeader { Id = msg.Id, Flags = MessageFlags.None }; if (cl.Secure) { using (var container = new MessageCryptoContainer()) { symCp.Key = cl.SessionKey; // XXX: generate new symCp.IV container.Store(msg, symCp, Protocol); container.Save(buf, Protocol); header.Size = (int)buf.Length; header.Flags |= MessageFlags.Secure; } } else { msg.Save(buf, Protocol); header.Size = (int)buf.Length; } try { header.Save(cl.NetStream, Protocol); rawBuf.WriteTo(cl.NetStream); } catch (IOException e) { if (NetUtil.CheckConnectionReset(e)) { OnClientConnectionReset(cl); return(false); } throw; } return(true); } }
private bool ReceiveMessage(MsgHeader hdr, out IMessage msg) { msg = null; // XXX: could be optimized - use one large buffer + unclosable MemoryStream var buf = new byte[hdr.Size]; try { netStream.Read(buf, 0, buf.Length); } catch (IOException e) { if (NetUtil.CheckConnectionReset(e)) { OnServerConnectionReset(); return(false); } throw; } using (var ms = new MemoryStream(buf)) { var msWrap = ms.AsUnclosable(); if ((hdr.Flags & MessageFlags.Secure) == MessageFlags.Secure) { using (var container = new MessageCryptoContainer()) { container.Load(msWrap, cfg.Protocol); symCp.Key = sessionKey; msg = container.Extract(hdr.Id, symCp, cfg.Protocol); } } else { msg = MessageFactory.CreateMessage(hdr.Id); try { msg.Load(msWrap, cfg.Protocol); } catch (MessageLoadException e) { Root.Log(LogLevel.Warning, "Client: bad message received from server ({0})", e.Message); Disconnect(); return(false); } } } return(true); }
public override void Update() { var hdr = new MsgHeader(); // try to read one message from each client foreach (var pair in clients) { var cl = pair.Value; if (!CheckClientConnection(cl)) { OnClientConnectionReset(cl); continue; } if (cl.Params.Socket.Available == 0) { continue; } if (!ReceiveMessageHeader(hdr, cl)) { continue; } if (hdr.Id.IsAuthRequired() && !cl.Authenticated) { Root.Log(LogLevel.Error, "Discarding message [{0}] from non-authenticated client: {1}", hdr.Id, GetClientName(cl)); DisconnectClient(cl, DisconnectReason.Unknown, "Can't process message without authentication"); continue; } var reqAccess = hdr.Id.GetRequiredAccess(); var clAccess = cl.AccessFlags; if ((reqAccess & clAccess) != reqAccess) { Root.Log(LogLevel.Error, "Discarding message [{0}] from client {1}: insufficient permissions", hdr.Id, GetClientName(cl)); DisconnectClient(cl, DisconnectReason.Unknown, "Insufficient permissions"); continue; } IMessage msg; if (!ReceiveMessage(hdr, cl, out msg)) { continue; } OnMessage(msg, cl.Id); } ProcessDisconnectedClients(); ProcessNewClients(); ProcessSvcMessages(); }
public void TestHeaderExceptions() { Assert.Throws <NATSException>(() => new MsgHeader(null, 1)); Assert.Throws <NATSException>(() => new MsgHeader(new byte[16], 17)); Assert.Throws <NATSException>(() => new MsgHeader(null, 1)); Assert.Throws <NATSException>(() => new MsgHeader(new byte[16], 0)); Assert.Throws <NATSException>(() => new MsgHeader(new byte[16], -1)); Assert.Throws <ArgumentNullException>(() => new MsgHeader(null)); byte[] b = Encoding.UTF8.GetBytes("GARBAGE"); Assert.Throws <NATSInvalidHeaderException>(() => new MsgHeader(b, b.Length)); // No headers b = Encoding.UTF8.GetBytes("NATS/1.0"); Assert.Throws <NATSInvalidHeaderException>(() => new MsgHeader(b, b.Length)); // No headers b = Encoding.UTF8.GetBytes("NATS/1.0\r\n"); Assert.Throws <NATSInvalidHeaderException>(() => new MsgHeader(b, b.Length)); // Missing last \r\n b = Encoding.UTF8.GetBytes("NATS/1.0\r\nk1:v1\r\n"); Assert.Throws <NATSInvalidHeaderException>(() => new MsgHeader(b, b.Length)); // invalid headers b = Encoding.UTF8.GetBytes("NATS/1.0\r\ngarbage\r\n\r\n"); Assert.Throws <NATSInvalidHeaderException>(() => new MsgHeader(b, b.Length)); // missing key b = Encoding.UTF8.GetBytes("NATS/1.0\r\n:value\r\n\r\n"); Assert.Throws <NATSInvalidHeaderException>(() => new MsgHeader(b, b.Length)); // test invalid characters var mh = new MsgHeader(); Assert.Throws <ArgumentException>(() => mh["k\r\ney"] = "value"); Assert.Throws <ArgumentException>(() => mh["key"] = "val\r\nue"); Assert.Throws <ArgumentException>(() => mh["foo:bar"] = "value"); Assert.Throws <ArgumentException>(() => mh["foo"] = "value\f"); Assert.Throws <ArgumentException>(() => mh["foo\f"] = "value"); // test constructor with invalid assignment Assert.Throws <ArgumentException>(() => new MsgHeader() { ["foo:bar"] = "baz" }); }
public void TestHeaderSerialization() { string headers = $"NATS/1.0\r\nfoo:bar\r\n\r\n"; byte[] headerBytes = System.Text.Encoding.UTF8.GetBytes(headers); // can only test with one because order isn't guaranteed var mh = new MsgHeader(); mh["foo"] = "bar"; byte[] bytes = mh.ToByteArray(); Assert.True(bytes.Length == headerBytes.Length); for (int i = 0; i < bytes.Length; i++) { Assert.True(headerBytes[i] == bytes[i]); } // now serialize back var mh2 = new MsgHeader(bytes, bytes.Length); Assert.Equal("bar", mh2["foo"]); // large header StringBuilder sb = new StringBuilder(); for (int i = 0; i < 20480; i++) { sb.Append("N"); } string lv = sb.ToString(); mh["LargeValue"] = lv; // test null and empty values mh["Null-Value"] = null; mh["Empty-Value"] = ""; bytes = mh.ToByteArray(); // now serialize back mh2 = new MsgHeader(bytes, bytes.Length); Assert.Equal("bar", mh2["foo"]); Assert.Equal("", mh2["Null-Value"]); Assert.Equal("", mh2["Empty-Value"]); Assert.Equal(lv, mh2["LargeValue"]); }
public int Receive(Socket s) { byte[] bytes = new byte[MsgHeader.GetSize()]; s.Receive(bytes, MsgHeader.GetSize(), SocketFlags.None); m_header.TransformToMsgHeader(bytes); if (m_header.m_size > 0) { bytes = new byte[m_header.m_size + 1]; s.Receive(bytes, m_header.m_size + 1, SocketFlags.None); m_data = cp1251.GetString(bytes, 0, m_header.m_size); } return(m_header.m_type); }
private MsgHeader MergePublishOptions(MsgHeader headers, PublishOptions opts) { // never touch the user's original headers MsgHeader merged = headers == null ? null : new MsgHeader(headers); if (opts != null) { merged = MergeNum(merged, JetStreamConstants.ExpLastSeqHeader, opts.ExpectedLastSeq); merged = MergeNum(merged, JetStreamConstants.ExpLastSubjectSeqHeader, opts.ExpectedLastSubjectSeq); merged = MergeString(merged, JetStreamConstants.ExpLastIdHeader, opts.ExpectedLastMsgId); merged = MergeString(merged, JetStreamConstants.ExpStreamHeader, opts.ExpectedStream); merged = MergeString(merged, JetStreamConstants.MsgIdHeader, opts.MessageId); } return(merged); }
/// <summary> /// /// </summary> public void SendIpInfo() { MsgHeader mh = new MsgHeader(); //参数赋值 mh.MsgID = ConstIDs.O_TDMOM_IP_PORT_CFG; mh.puData = 0; mh.DataLen = 0; mh.MsgLen = (uint)Marshal.SizeOf(mh); byte[] res_mh = StructConverter.StructToBytes(mh); IpPortCFCStruct ips = new IpPortCFCStruct(); byte[] strbytes= System.Text.Encoding.Unicode.GetBytes(_configService.ConfigInfos.TermialIP); Buffer.BlockCopy(strbytes, 0, ips.IpAddr, 0, ips.IpAddr.Length > strbytes.Length ? strbytes.Length : ips.IpAddr.Length); ips.PortNum = (uint)_configService.ConfigInfos.TerminalPort; //发送 }
public MsgHeader(MsgHeader value) { /// Initiliaze the protected variables m_HeaderRec = new HeaderRec(); m_HeaderRec.setParent(this); /// Copy the values m_HeaderRec = value.getHeaderRec(); m_HeaderRec.setParent(this); }
public SetAuthority() { m_MsgHeader = new MsgHeader(); m_Body = new Body(); m_IsCommand = true; }
public void setMsgHeader(MsgHeader value) { m_MsgHeader = value; }
public SetAuthority setAs(SetAuthority value) { m_MsgHeader = value.m_MsgHeader; m_Body = value.m_Body; return this; }
public HeaderRec() { m_parent = null; m_MessageID = 0x4003; }
public ConfirmEventRequest() { m_MsgHeader = new MsgHeader(); m_Body = new Body(); m_IsCommand = false; }
public ReportTimeout() { m_MsgHeader = new MsgHeader(); m_Body = new Body(); m_IsCommand = false; }
public ReportTimeout setAs(ReportTimeout value) { m_MsgHeader = value.m_MsgHeader; m_Body = value.m_Body; return this; }
public RejectEventRequest.MsgHeader setMsgHeader(MsgHeader value) { m_HeaderRec = value.getHeaderRec(); m_HeaderRec.setParent(this); return this; }
public HeaderRec(HeaderRec value) { /// Initiliaze the protected variables m_parent = null; m_MessageID = 0x0001; /// Copy the values m_MessageID = value.m_MessageID; }
public SetAuthority.MsgHeader setMsgHeader(MsgHeader value) { m_HeaderRec = value.getHeaderRec(); m_HeaderRec.setParent(this); return this; }
public bool isEqual(MsgHeader value) { if (!this.getHeaderRec().isEqual(value.getHeaderRec())) { return false; } return true; }
public ReportTimeout.MsgHeader setMsgHeader(MsgHeader value) { m_HeaderRec = value.getHeaderRec(); m_HeaderRec.setParent(this); return this; }
public bool notEquals(MsgHeader value) { return !this.isEqual(value); }
public ConfirmEventRequest setAs(ConfirmEventRequest value) { m_MsgHeader = value.m_MsgHeader; m_Body = value.m_Body; return this; }
public HeaderRec() { m_parent = null; m_MessageID = 0x0001; }
public RejectEventRequest setAs(RejectEventRequest value) { m_MsgHeader = value.m_MsgHeader; m_Body = value.m_Body; return this; }
public void setParent(MsgHeader parent) { m_parent = parent; }
public RejectEventRequest() { m_MsgHeader = new MsgHeader(); m_Body = new Body(); m_IsCommand = false; }