程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> C++編寫nodejs擴展實戰

C++編寫nodejs擴展實戰

編輯:C++入門知識

C++編寫nodejs擴展實戰

之前有用PHP寫過根據IP地址查詢IP歸屬地,後來改用C語言編寫,效率果然大幅度提高,然後轉化為PHP的擴展.

想起之前有過使用手機號碼查詢號碼歸屬地,最近又有在研究nodejs,於是就使用C++編寫了nodejs的擴展.

遇到的問題確實不少,記錄下來,供大家參考:

1.字符編碼的問題:nodejs對於gbk編碼支持不夠好,為了提高程序效率,先把IP歸屬地的資源轉化為utf-8的編碼,具體使用PHP腳本foreach使用iconv轉換

2.參數傳遞的問題,很多程序裡面都寫個helloworld的nodejs擴展的C++實現,但是這裡面沒有參數傳遞吶,參考了nodejs官網,總是找不到int類型,怎麼從javascript轉化為C++,本著有困難找度娘的心理,果然讓我找到了:使用args[0]->Int32Value()就可以轉化了

3.具體編寫的過程如下:

a.編寫binding.gyp,先寫好最簡配置:

{
'targets':[
{
'targetname':'mtc'
'source':['mtc.cc']
}
]
}
其中target
name是模塊名,require的時候用
source是C++源文件的名字放在binding.gyp同目錄下

b.編寫C++的源代碼:

#include 
#include 
using namespace v8;
const int MaxCityLength = 25;

char* Search(const char* fileName, const int& number);
class Info{
public:
Info():mbefore(0),mafter(0),m_cityIndex(0) {

}
Info(int begin, unsigned short skip, unsigned short city_index){
    setBegin(begin);
    setSkip(skip);
    m_cityIndex = city_index;
}
int getBegin() {
    int lastTwo = m_after - getNumberExceptLastTwo()*100;
    return m_before * 100 + lastTwo;
}

unsigned short getNumberExceptLastTwo() {
    return m_after * 0.01;
}

unsigned short getCityIndex() {
    return m_cityIndex;
}

unsigned short getLastTwo(int number) {
    int exceptLastTwoNum = number * 0.01;
    return (number - exceptLastTwoNum * 100);
}

void setBegin(int& number) {
    int lastTwo = getLastTwo(number);
    m_before = number * 0.01;
    m_after = getNumberExceptLastTwo();
}

void setSkip(unsigned short skip) {
    m_after = getNumberExceptLastTwo() * 100 + getLastTwo(m_after);
}

void setCityIndex(unsigned short& city) {
    m_cityIndex = city;
}
char* FindResult(FILE* file, const int& count, Info info){
    int totalOffset = sizeof(int) + count*sizeof(Info) + info.getCityIndex()* MaxCityLength;
    fseek(file, totalOffset,SEEK_SET);
    char* location = new char [MaxCityLength];
    fread(location, MaxCityLength, 1, file);
    fclose(file);
    return location;
}

private:
unsigned short mbefore;
unsigned short m
after;
unsigned short m_cityIndex;

};

char* search(const char* fileName, const int& number) {
FILE* file = 0;
file = fopen(fileName, "rb");
if(file == 0) {
return (char*) "";

}
int count = 0;
fread(&count ,sizeof(int), 1, file);
int left = 0, right = count - 1;
Info info;    
while(left < right) {
    int middle = (left + right)/2;
    fseek(file, sizeof(int) + middle * 6 ,SEEK_SET);
    fread(&info ,sizeof(unsigned short)*3, 1, file);
    if(number < info.getBegin()) {
        right =middle -1;
    } else if(number >info.getBegin() + info.getNumberExceptLastTwo()) {
        left = middle +1;
    } else {
        return info.FindResult(file, count,info);
    }
}
return (char*) "";

}

Handle mtc(const Arguments& args) {
HandleScope scope;
char* ret = search("AreaData.dat", args[0]->Int32Value());
return scope.Close(String::New((char*)ret));

}

void init(Handle target) {
NODESETMETHOD(target, "m2c", mtc);
}

NODE_MODULE(mtc, init);

c.使用下面的兩條命令就OK了:

node-gyp configure 
node-gyp build

最後就會生成mtc.node文件:

\寫測試文件test.js
var mtc = require("mtc');
console.log('mtc.mtc()=',mtc.mtc(1895926))
執行:node test.js就會輸出福建廈門

n塊ズ岫?錼褅9鬛黑喎?http://www.Bkjia.com/soft下載

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved