2015-06-21 21:02:10 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* Copyright (c) FIRST 2015. All Rights Reserved. */
|
|
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2015-06-25 23:12:49 -07:00
|
|
|
#include "MessageWriter.h"
|
2015-06-21 21:02:10 -07:00
|
|
|
|
2015-06-25 23:12:49 -07:00
|
|
|
#include "internal.h"
|
2015-06-21 21:02:10 -07:00
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
using namespace ntimpl;
|
2015-06-21 21:02:10 -07:00
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
MessageWriter::~MessageWriter() {}
|
2015-06-21 21:02:10 -07:00
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
void MessageWriter::WriteKeepAlive() { Write8(NT_MSG_KEEP_ALIVE); }
|
2015-06-21 21:02:10 -07:00
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
void MessageWriter::WriteClientHello(const NT_String& self_id) {
|
|
|
|
|
Write8(NT_MSG_CLIENT_HELLO);
|
|
|
|
|
Write16(m_proto_rev);
|
|
|
|
|
if (m_proto_rev < 0x0300u) return;
|
|
|
|
|
WriteString(self_id);
|
2015-06-21 21:02:10 -07:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
void MessageWriter::WriteProtoUnsup() {
|
|
|
|
|
Write8(NT_MSG_PROTO_UNSUP);
|
|
|
|
|
Write16(m_proto_rev);
|
2015-06-21 21:02:10 -07:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
void MessageWriter::WriteServerHelloDone() { Write8(NT_MSG_SERVER_HELLO_DONE); }
|
2015-06-21 21:02:10 -07:00
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
void MessageWriter::WriteServerHello(unsigned int flags,
|
|
|
|
|
const NT_String& self_id) {
|
|
|
|
|
if (m_proto_rev < 0x0300u) return; // new message in version 3.0
|
|
|
|
|
Write8(NT_MSG_SERVER_HELLO);
|
|
|
|
|
Write8(flags);
|
|
|
|
|
WriteString(self_id);
|
2015-06-21 21:02:10 -07:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
void MessageWriter::WriteClientHelloDone() {
|
|
|
|
|
if (m_proto_rev < 0x0300u) return; // new message in version 3.0
|
|
|
|
|
Write8(NT_MSG_CLIENT_HELLO_DONE);
|
2015-06-21 21:02:10 -07:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
void MessageWriter::WriteEntryAssign(const NT_String& name, unsigned int id,
|
|
|
|
|
unsigned int seq_num,
|
|
|
|
|
const NT_Value& value,
|
|
|
|
|
unsigned int flags) {
|
|
|
|
|
Write8(NT_MSG_ENTRY_ASSIGN);
|
|
|
|
|
WriteString(name);
|
|
|
|
|
WriteType(value.type);
|
|
|
|
|
Write16(id);
|
|
|
|
|
Write16(seq_num);
|
|
|
|
|
if (m_proto_rev >= 0x0300u) Write8(flags);
|
|
|
|
|
WriteValue(value);
|
2015-06-21 21:02:10 -07:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
void MessageWriter::WriteEntryUpdate(unsigned int id, unsigned int seq_num,
|
|
|
|
|
const NT_Value& value) {
|
|
|
|
|
Write8(NT_MSG_ENTRY_UPDATE);
|
|
|
|
|
Write16(id);
|
|
|
|
|
Write16(seq_num);
|
|
|
|
|
if (m_proto_rev >= 0x0300u) Write8(value.type);
|
|
|
|
|
WriteValue(value);
|
2015-06-21 21:02:10 -07:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
void MessageWriter::WriteFlagsUpdate(unsigned int id, unsigned int flags) {
|
|
|
|
|
if (m_proto_rev < 0x0300u) return; // new message in version 3.0
|
|
|
|
|
Write8(NT_MSG_FLAGS_UPDATE);
|
|
|
|
|
Write16(id);
|
|
|
|
|
Write8(flags);
|
2015-06-21 21:02:10 -07:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
void MessageWriter::WriteEntryDelete(unsigned int id) {
|
|
|
|
|
if (m_proto_rev < 0x0300u) return; // new message in version 3.0
|
|
|
|
|
Write8(NT_MSG_ENTRY_DELETE);
|
|
|
|
|
Write16(id);
|
2015-06-21 21:02:10 -07:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
void MessageWriter::WriteClearEntries() {
|
|
|
|
|
if (m_proto_rev < 0x0300u) return; // new message in version 3.0
|
2015-06-21 21:02:10 -07:00
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
Write8(NT_MSG_CLEAR_ENTRIES);
|
|
|
|
|
Write32(NT_CLEAR_ALL_MAGIC);
|
2015-06-21 21:02:10 -07:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
void MessageWriter::WriteExecuteRpc(unsigned int id, unsigned int uid,
|
|
|
|
|
const NT_Value* params_start,
|
|
|
|
|
const NT_Value* params_end) {
|
|
|
|
|
WriteRpc(NT_MSG_EXECUTE_RPC, id, uid, params_start, params_end);
|
2015-06-21 21:02:10 -07:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
void MessageWriter::WriteRpcResponse(unsigned int id, unsigned int uid,
|
|
|
|
|
const NT_Value* results_start,
|
|
|
|
|
const NT_Value* results_end) {
|
|
|
|
|
WriteRpc(NT_MSG_RPC_RESPONSE, id, uid, results_start, results_end);
|
2015-06-21 21:02:10 -07:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 22:57:43 -07:00
|
|
|
void MessageWriter::WriteRpc(unsigned int msg_type, unsigned int id,
|
|
|
|
|
unsigned int uid, const NT_Value* values_start,
|
|
|
|
|
const NT_Value* values_end) {
|
|
|
|
|
if (m_proto_rev < 0x0300u) return; // new message in version 3.0
|
|
|
|
|
|
|
|
|
|
Write8(msg_type);
|
|
|
|
|
Write16(id);
|
|
|
|
|
Write16(uid);
|
|
|
|
|
|
|
|
|
|
unsigned long len = 0;
|
|
|
|
|
for (const NT_Value* value = values_start; value != values_end; ++value)
|
|
|
|
|
len += GetValueSize(*value);
|
|
|
|
|
WriteULEB128(len);
|
|
|
|
|
|
|
|
|
|
for (const NT_Value* value = values_start; value != values_end; ++value)
|
|
|
|
|
WriteValue(*value);
|
2015-06-21 21:02:10 -07:00
|
|
|
}
|