c++ - 将旧的 C++ 代码集成到 PNaCI 模块中

标签 c++ google-nativeclient

我多次阅读了 Google 的 native 客户端教程,了解如何构建我自己的基于 C++ 的 PNaCl 模块,但不知怎的,我并没有变得更聪明,我知道如果我想实现消息传递功能。我在 .cc 文件中有以下内容作为 PNaCI 代码的基础,所有这些都取自 Google 的 Hello World 教程:

#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/var_array.h"
#include "ppapi/cpp/var.h"

namespace {
// The expected string sent by the browser.
const char* const kHelloString = "hello";
// The string sent back to the browser upon receipt of a message
// containing "hello".
const char* const kReplyString = "hello from NaCl";
} // namespace

class job1Instance : public pp::Instance {
    public:
        explicit job1Instance(PP_Instance instance): pp::Instance(instance) {}
        virtual ~job1Instance() {}

    virtual void HandleMessage(const pp::Var& message) {
        if (!message.is_string()) {
            return;
        }
        std::string message_txt = message.AsString();
        pp::Var reply;
        if (message_txt == kHelloString) {
            reply = pp::Var(kReplyString);
            PostMessage(kReplyString);
        }
    }
};

class job1 : public pp::Module {
    public:
        job1() : pp::Module() {}
        virtual ~job1() {}

        virtual pp::Instance* CreateInstance(PP_Instance instance) {
        return new job1Instance(instance);
    }
};

namespace pp {

    Module* CreateModule() {
        return new job1();
    }
}  // namespace pp

据我所知,PNaCl 模块不使用 main() 函数,但假设我有一个旧的 C++ 代码,它创建了 2 个数组 unsorted1unsorted2带有我想在 PNaCl 模块中使用的随机数:

#include <iostream>
#include <stdint.h>
#include <unistd.h>
#include <array>

// a function to create a random number between min and max
int32_t rangeRandomAlg (int32_t min, int32_t max) { 
    int32_t num = max - min + 1; 
    int32_t remainder = RAND_MAX % num; 
    int32_t x; 
    do { 
        x = rand(); 
    } while (x >= RAND_MAX - remainder); 
    return min + x % num; 
} 

// a function to create arrays with random numbers 
void unsortedArrays(int32_t unsorted1[], int32_t unsorted2[],int32_t arrayElements, int32_t minNum, int32_t maxNum){ 
    for(int32_t i = 0; i <= arrayElements; i++) { 
        if (i < arrayElements/2) { 
            unsorted1[i] = rangeRandomAlg(minNum, maxNum); 
        } else { 
            unsorted2[i] = rangeRandomAlg(minNum, maxNum); 
        } 
    } 
} 

// the main function
int32_t main(int32_t argc, char *argv[]) { 
    // declare all the zises 
    int32_t minNum = 0; 
    int32_t maxNum = 100; 
    int32_t arrayElements = maxNum; 

    // the arrays 
    int32_t unsorted1[arrayElements/2]; 
    int32_t unsorted2[arrayElements/2]; 

    // fill the arrays with random numbers 
    unsortedArrays(unsorted1, unsorted2, arrayElements, minNum, maxNum); 

    return 0; 
}

我的问题是我不太明白如何将此代码集成到 PNaCl 模块中并使用 HandleMessage()函数发送 unsorted1unsorted2数组回到JavaScriptPostMesage()功能。我知道我必须使用数组而不是 HandleMessage() 中的字符串功能。

我希望在这里得到一些帮助,因为我对整个原生客户端的事情非常陌生。

最佳答案

这是花了几个小时才找到的解决方案:

// pepper includes
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/var_array.h"
#include "ppapi/cpp/var.h"
#include "json/json.h"
#include <sstream>

// cpp includes
#include <stdint.h>
#include <unistd.h>
#include <array>
#include <string>

//static variables
namespace {
// The expected string sent by the browser.
const char* const kHelloString = "Bereit fuer dein Modul";
// The string sent back to the browser upon receipt of a message
// containing "hello".
const char* const kReplyString = "PNaCl hat Ergebnisse geschickt";
} // namespace

class job1Instance : public pp::Instance {
public:
        explicit job1Instance(PP_Instance instance): pp::Instance(instance) {}
        virtual ~job1Instance() {}

    virtual void HandleMessage(const pp::Var& message) {
        /*
        if (!message.is_string()) {
            return;
        }
        std::string message_txt = message.AsString();
        pp::Var reply;
        if (message_txt == kHelloString) {
            reply = pp::Var(kReplyString);
            PostMessage(kReplyString);
        }
        */

/*** my functions and data for the cpp code to integrate start here ***/
        // declare all the zises
        int32_t minNum = 0;
        int32_t maxNum = 100;
        int32_t arrayElements = maxNum;

        // the arrays
        int32_t unsorted1[arrayElements/2];
        int32_t unsorted2[arrayElements/2];

        // fill the arrays with random numbers
        unsortedArrays(unsorted1, unsorted2, arrayElements, minNum, maxNum);
        std::string outRes1, outRes2;
        arrayToString(unsorted1, arrayElements/2, outRes1);
        arrayToString(unsorted2, arrayElements/2, outRes2);
        PostMessage(outRes1); // send the unsorted1 array as a string to the JavaScript back
    }

private:
    // function to create a random number between min and max
    int32_t rangeRandomAlg (int32_t min, int32_t max) {
        int32_t num = max - min + 1;
        int32_t remainder = RAND_MAX % num;
        int32_t x;
        do {
            x = rand();
        } while (x >= RAND_MAX - remainder);
        return min + x % num;
    }

    // function to create arrays with random numbers
    void unsortedArrays (int32_t unsorted1[], int32_t unsorted2[],int32_t arrayElements, int32_t minNum, int32_t maxNum) {
        for(int32_t i = 0; i <= arrayElements; i++) {
            if (i < arrayElements/2) {
                unsorted1[i] = rangeRandomAlg(minNum, maxNum);
            } else {
                unsorted2[i] = rangeRandomAlg(minNum, maxNum);
            }
        }
    }

    // convert the arrays to string
    void arrayToString (int32_t array[], int32_t arraySize, std::string& arrayString) {
        for (int32_t i = 0; i <= arraySize; ++i){
            arrayString+= std::to_string(array[i]);
            if (i != arraySize) {
                arrayString+= ',';
            }
        }
    }
};
/*** my functions and data for the cpp code to integrate end here ***/


class job1 : public pp::Module {
    public:
        job1() : pp::Module() {}
        virtual ~job1() {}

        virtual pp::Instance* CreateInstance(PP_Instance instance) {
        return new job1Instance(instance);
    }
};

namespace pp {

    Module* CreateModule() {
        return new job1();
    }
}  // namespace pp

这只是 PNaCl 模块的预编译 C++ 代码,其中集成了我的代码,它仅将 outRes1 变量 = unsorted1 数组变量作为字符串发送到 JavaScript代码在index.html文件中。您必须分别编写 .nmf 和 index.html 文件。如果有人想查看这些文件的代码(其中包含此 PNaCI 模块的基本和工作代码),请给我写一条评论,我会将其发布到。

关于c++ - 将旧的 C++ 代码集成到 PNaCI 模块中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39191103/

相关文章:

c++ - cpp中的接口(interface)

c++ - STL 容器上的指针

javascript - 从 JavaScript postMessage ArrayBuffer 到 native 客户端模块

c++ - 如何在 C++ 中获取 bool 变量的相反值

c++ - 如何理解 char * ch ="123"?

c++ - 打开不存在的文件时如何使 ofstream 构造函数失败?

c++ - 我可以使用 Google NaCl 访问文件系统吗?

linux - 构建 Google Chrome NaCl 应用程序 .nexe 文件时找不到有效的库

google-nativeclient - 运行 Google Native Client 的互联网百分比?