64 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			64 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
#include "ModelUrlMsg.h"
 | 
						|
 | 
						|
#include <string.h>
 | 
						|
 | 
						|
namespace RoboidControl {
 | 
						|
 | 
						|
ModelUrlMsg::ModelUrlMsg(unsigned char networkId, Thing* thing) {
 | 
						|
  this->networkId = networkId;
 | 
						|
  this->thingId = thing->id;
 | 
						|
  if (thing->modelUrl == nullptr)
 | 
						|
    this->urlLength = 0;
 | 
						|
  else
 | 
						|
    this->urlLength = (unsigned char)strlen(thing->modelUrl);
 | 
						|
 | 
						|
  // the url string in the buffer is not \0 terminated!
 | 
						|
  char* url = new char[this->urlLength + 1];
 | 
						|
  for (int i = 0; i < this->urlLength; i++)
 | 
						|
    url[i] = thing->modelUrl[i];
 | 
						|
  url[this->urlLength] = '\0';
 | 
						|
  this->url = url;
 | 
						|
}
 | 
						|
 | 
						|
ModelUrlMsg::ModelUrlMsg(const char* buffer) {
 | 
						|
  unsigned char ix = 1;  // first byte is msg id
 | 
						|
  this->networkId = buffer[ix++];
 | 
						|
  this->thingId = buffer[ix++];
 | 
						|
  this->urlLength = buffer[ix++];
 | 
						|
  // this->url = &buffer[ix];  // dangerous! name should not be used anymore
 | 
						|
  // after
 | 
						|
  //                           // buffer has been re-used...
 | 
						|
 | 
						|
  // the url string in the buffer is not \0 terminated!
 | 
						|
  char* url = new char[this->urlLength + 1];
 | 
						|
  for (int i = 0; i < this->urlLength; i++)
 | 
						|
    url[i] = buffer[ix++];
 | 
						|
  url[this->urlLength] = '\0';
 | 
						|
  this->url = url;
 | 
						|
}
 | 
						|
 | 
						|
ModelUrlMsg::~ModelUrlMsg() {
 | 
						|
  delete[] this->url;
 | 
						|
}
 | 
						|
 | 
						|
unsigned char ModelUrlMsg::Serialize(char* buffer) {
 | 
						|
  if (this->urlLength == 0 || this->url == nullptr)
 | 
						|
    return 0;
 | 
						|
#if defined(DEBUG)
 | 
						|
  std::cout << "Send ModelUrlMsg [" << (int)this->networkId << "/"
 | 
						|
            << (int)this->thingId << "] " << (int)this->urlLength << " "
 | 
						|
            << this->url << std::endl;
 | 
						|
#endif
 | 
						|
  unsigned char ix = 0;
 | 
						|
  buffer[ix++] = this->id;
 | 
						|
  buffer[ix++] = this->networkId;
 | 
						|
  buffer[ix++] = this->thingId;
 | 
						|
  // LowLevelMessages::SendFloat16(buffer, &ix, this->scale);
 | 
						|
  buffer[ix++] = this->urlLength;
 | 
						|
  for (int urlIx = 0; urlIx < this->urlLength; urlIx++)
 | 
						|
    buffer[ix++] = url[urlIx];
 | 
						|
  return ix;
 | 
						|
}
 | 
						|
 | 
						|
}  // namespace RoboidControl
 |