네이미즈 MYPI

네이미즈
접속 : 5571   Lv. 62

Category

Profile

Counter

  • 오늘 : 4 명
  • 전체 : 31269 명
  • Mypi Ver. 0.3.1 β
[기타] node.js 모듈(addon) 만드는 중.. async 콜백함수 호출 (1) 2015/07/02 AM 10:42
node.js 버전은 v0.12.3
async callback 하는 부분이 대부분 v0.10 이하에서만 정보가 많아서, 정보대로 했는데, 잘 안되네요.
이래저래 찾다보니 v0.10과 v0.12 에서 바뀐게 있어서 그부분 적용된 코드를 만들어 봤는데, 작동은 되는것 같은데, 맞게 한건지..ㅎ


// nodeINB.cpp : 해당 DLL의 초기화 루틴을 정의합니다.
//

#include "stdafx.h"
#include "nodeINB.h"

#include
#include
#include

#if defined(_M_X64)
#pragma comment(lib, ".\\node\\x64\\node.lib")
#else
#pragma comment(lib, ".\\node\\x86\\node.lib")
#endif

using namespace v8;

struct Baton
{
uv_work_t request;
Persistent callback;
int err_code;
std::string error_message;
};

void AsyncWork(uv_work_t* req)
{
printf("asyncwork sleep now\n");
Sleep(5000);
}

void AsyncAfter(uv_work_t* req, int status)
{
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);

printf("after\n");
Baton* baton = static_cast(req->data);
const unsigned argc = 1;
Local argv[argc] = { String::NewFromUtf8(isolate, "hello world") };
Local cb = Local::New(isolate, baton->callback);
cb->Call(isolate->GetCurrentContext()->Global(), argc, argv);
baton->callback.Reset();
delete baton;
}


//-----------------------------------------------------------------------------
void CallbackFunction(const FunctionCallbackInfo& args)
{
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);

Local cb = Local::Cast(args[0]);
const unsigned argc = 1;
Local argv[argc] = { String::NewFromUtf8(isolate, "hello world") };
cb->Call(isolate->GetCurrentContext()->Global(), argc, argv);
}

void Async(const FunctionCallbackInfo& args)
{
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);

if (!args[0]->IsFunction())
{
isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Callback function required")));
return;
}

Local callback = Local::Cast(args[0]);

Baton* baton = new Baton();
baton->callback.Reset(isolate, callback);
baton->request.data = baton;

uv_queue_work(uv_default_loop(), &baton->request, AsyncWork, AsyncAfter);

Undefined(isolate);
return;
}

void test(const FunctionCallbackInfo& args)
{
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
CString str;
AfxExtractSubString(str, "TEST|한글", 1, '|');
USES_CONVERSION;
const WCHAR* wStr = T2CW(str);
int nUTF8codeSize = WideCharToMultiByte(CP_UTF8, 0, wStr, -1, NULL, 0, NULL, NULL);
char *utf8Str = new char[nUTF8codeSize];
WideCharToMultiByte(CP_UTF8, 0, wStr, -1, utf8Str, nUTF8codeSize, 0, 0);

args.GetReturnValue().Set(String::NewFromUtf8(isolate, utf8Str));
delete utf8Str;
}


//-----------------------------------------------------------------------------
void Init(Handle exports)
{
NODE_SET_METHOD(exports, "callbacktest", CallbackFunction);
NODE_SET_METHOD(exports, "async", Async);
NODE_SET_METHOD(exports, "test", test);
}

NODE_MODULE(nodeINB, Init)


원래 자바스크립트 개발자도 아니고, 그래서 node.js이해는 더 어렵고... 학교다닐때는 java위주로 개발하다가 첫 직장부터는 MFC만 쭉 해오고 있는데.... 암튼...ㅈㅈ

신고

 

네이미즈    친구신청

https://strongloop.com/strongblog/node-js-v0-12-c-apis-breaking/
X