added functionality to send and decode scripts

This commit is contained in:
Joakim Kilby
2015-06-23 09:33:09 +02:00
parent ba49755d83
commit b2b7563018
2 changed files with 66 additions and 2 deletions

View File

@@ -30,6 +30,8 @@
#include <openspace/properties/propertyowner.h>
#include <openspace/scripting/scriptengine.h>
#include <openspace/util/powerscaledcoordinate.h>
//glm includes
#include <glm/gtx/quaternion.hpp>
//std includes
@@ -38,6 +40,7 @@
#include <atomic>
#include <thread>
#include <sstream>
#include <mutex>
#ifdef __WIN32__
#ifndef WIN32_LEAN_AND_MEAN
@@ -133,10 +136,13 @@ namespace openspace{
void setPassword(const std::string &password);
void sendScript(const std::string script);
enum MessageTypes{
Authentication=0,
Initialization,
Data,
Script,
HostInfo,
InitializationRequest,
HostshipRequest
@@ -189,6 +195,8 @@ namespace openspace{
void decodeInitializationMessage();
void decodeDataMessage();
void decodeScript();
void decodeHostInfoMessage();
@@ -196,8 +204,8 @@ namespace openspace{
void broadcast();
int receiveData(_SOCKET & socket, std::vector<char> &buffer, int length, int flags);
int receiveData(_SOCKET & socket, std::vector<char> &buffer, int length, int flags);
uint32_t _passCode;
std::string _port;
std::string _address;

View File

@@ -248,6 +248,8 @@ namespace openspace {
case MessageTypes::Data:
decodeDataMessage();
break;
case MessageTypes::Script:
break;
case MessageTypes::HostInfo:
decodeHostInfoMessage();
break;
@@ -296,6 +298,33 @@ namespace openspace {
OsEng.interactionHandler()->addKeyframe(kf);
}
void OSParallelConnection::decodeScript(){
int result;
uint16_t msglen;
std::vector<char> buffer;
buffer.resize(sizeof(msglen));
result = receiveData(_clientSocket, buffer, sizeof(msglen), 0);
if (result <= 0){
//error
return;
}
msglen = (*(reinterpret_cast<uint16_t*>(buffer.data())));
buffer.clear();
buffer.resize(msglen);
result = receiveData(_clientSocket, buffer, msglen, 0);
if (result <= 0){
//error
return;
}
std::string script(buffer.data());
OsEng.scriptEngine()->queueScript(script);
}
void OSParallelConnection::decodeHostInfoMessage(){
std::vector<char> hostflag;
hostflag.resize(1);
@@ -457,6 +486,33 @@ namespace openspace {
void OSParallelConnection::setPassword(const std::string& pwd){
_passCode = hash(pwd);
}
void OSParallelConnection::sendScript(const std::string script){
uint16_t msglen = static_cast<uint16_t>(script.length());
std::vector<char> buffer;
buffer.reserve(headerSize + sizeof(msglen) + msglen);
//header
buffer.insert(buffer.end(), 'O');
buffer.insert(buffer.end(), 'S');
buffer.insert(buffer.end(), 0);
buffer.insert(buffer.end(), 0);
//type of message
int type = OSParallelConnection::MessageTypes::Script;
buffer.insert(buffer.end(), reinterpret_cast<char*>(&type), reinterpret_cast<char*>(&type) + sizeof(type));
//size of message
buffer.insert(buffer.end(), reinterpret_cast<char*>(&msglen), reinterpret_cast<char*>(&msglen) + sizeof(msglen));
//actual message
buffer.insert(buffer.end(), script.begin(), script.end());
//send message
send(_clientSocket, buffer.data(), buffer.size(), 0);
}
void OSParallelConnection::disconnect(){
//must be run before trying to join communication threads, else the threads are stuck trying to receive data