Skip to content

Instantly share code, notes, and snippets.

@Amanieu
Created July 2, 2013 14:24
Show Gist options
  • Save Amanieu/5909736 to your computer and use it in GitHub Desktop.
Save Amanieu/5909736 to your computer and use it in GitHub Desktop.
class VM {
public:
protected:
virtual void Syscall(int32_t index, Reader& input, Writer& output) = 0;
void DoRPC(const Writer& input, Reader& output);
private:
NaCl::Module* module;
};
// On the VM side, the code looks very similar to the syscall handler
// void vmMain(Reader& input, Writer& output);
class GameVM {
public:
// Example VMCall that takes a string and returns an int
int Example(const char* str)
{
Writer args;
args.WriteInt32(G_EXAMPLE_VMCALL);
args.WriteString(cmd);
if (args.Error())
Com_Error(ERR_DROP, "RPC buffer overflow");
Reader result;
DoRPC(args, result);
int result = result.ReadInt32();
if (result.Error())
return result;
}
protected:
// Syscall handler
virtual void Syscall(int32_t index, Reader& input, Writer& output)
{
// Example syscall that takes a string and returns an int
switch (index) {
case G_EXAMPLE_SYSCALL:
{
char arg[1024];
input.ReadString(arg, sizeof(arg));
if (input.Error())
Com_Error(ERR_DROP, "RPC buffer underflow");
output.WriteInt32(atoi(arg));
if (output.Error())
Com_Error(ERR_DROP, "RPC buffer overflow");
}
default:
Com_Error(ERR_DROP, "Invalid syscall");
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment