Skip to content

Commit

Permalink
add thread mode
Browse files Browse the repository at this point in the history
  • Loading branch information
smok95 committed Aug 1, 2021
1 parent 57fd4d9 commit 4df01bf
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 101 deletions.
8 changes: 4 additions & 4 deletions Resource.rc
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ END
//

VS_VERSION_INFO VERSIONINFO
FILEVERSION 0,0,9,0
PRODUCTVERSION 0,0,9,0
FILEVERSION 0,0,10,0
PRODUCTVERSION 0,0,10,0
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
Expand All @@ -69,12 +69,12 @@ BEGIN
BEGIN
VALUE "CompanyName", "TODO: <ȸ�� �̸�>"
VALUE "FileDescription", "https://github.com/smok95/kw_"
VALUE "FileVersion", "0.0.9.0"
VALUE "FileVersion", "0.0.10.0"
VALUE "InternalName", "kw_.dll"
VALUE "LegalCopyright", "Copyright (C) 2021 Kim, Jong-Kook"
VALUE "OriginalFilename", "kw_.dll"
VALUE "ProductName", "TODO: <��ǰ �̸�>"
VALUE "ProductVersion", "0.0.9.0"
VALUE "ProductVersion", "0.0.10.0"
END
END
BLOCK "VarFileInfo"
Expand Down
85 changes: 43 additions & 42 deletions example/exampleConsole/exampleConsole.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,57 @@
#include "kw_.h"
#pragma comment(lib, "kw_.lib")

void OnEventConnect(long errCode) {
if (errCode == 0) {
puts("로그인 성공");

// 종목코드로 종목명 확인
char* s = kw_GetMasterCodeName("005930");
printf("종목명=%s\n", s);

kw_SetInputValue("종목코드", "005930");
long ret = kw_CommRqData("jktest", "opt10001", 0, "jktestscr");
printf("ret1=%d\n", ret);

kw_SetInputValue("종목코드", "000660");
ret = kw_CommRqData("jktes2t", "opt10001", 0, "jktestscr2");
printf("ret2=%d\n", ret);
kw_FreeString(s);
}
else {
puts("로그인 실패");
kw_Disconnect();
}
}
void OnEventConnect(long errCode);

void OnReceiveTrData(PCSTR sScrNo, PCSTR sRQName,
PCSTR sTrCode, PCSTR sRecordName, PCSTR sPrevNext, long nDataLength,
PCSTR sErrorCode, PCSTR sMessage, PCSTR sSplmMsg) {
printf("%s) scrNo=%s, rqName=%s, trCode=%s\n", __FUNCTION__, sScrNo,
sRQName, sTrCode);
}
PCSTR sTrCode, PCSTR sRecordName, PCSTR sPrevNext, long nDataLength,
PCSTR sErrorCode, PCSTR sMessage, PCSTR sSplmMsg);


void OnErceiveRealData(PCSTR sRealKey,
PCSTR sRealType, PCSTR sRealData) {
printf("%s) %s\n",__FUNCTION__, sRealData);
}
void OnReceiveRealData(PCSTR sRealKey, PCSTR sRealType, PCSTR sRealData);

int main()
{
kw_Initialize(0);

// 이벤트 핸들러 설정
kw_SetOnEventConnect(OnEventConnect);
kw_SetOnReceiveTrData(OnReceiveTrData);
kw_SetOnReceiveRealData(OnErceiveRealData);
kw_SetOnEventConnect(OnEventConnect);
kw_SetOnReceiveTrData(OnReceiveTrData);
kw_SetOnReceiveRealData(OnReceiveRealData);

// 로그인
kw_CommConnect();
//kw_Wait();
kw_CommConnect();

kw_Wait();

while (1) {
Sleep(100);
}
kw_Uninitialize();
return 0;
}

void OnEventConnect(long errCode) {
if (errCode == 0) {
puts("연결됨");

// 종목코드로 종목명 확인
char* s = kw_GetMasterCodeName("005930");
printf("종목명=%s\n", s);
kw_FreeString(s);

// 시세 조회
kw_SetInputValue("종목코드", "005930");
long ret = kw_CommRqData("rqName1", "opt10001", 0, "scrNo1");
}
else {
puts("연결 종료됨");

}
}

void OnReceiveTrData(PCSTR sScrNo, PCSTR sRQName,
PCSTR sTrCode, PCSTR sRecordName, PCSTR sPrevNext, long nDataLength,
PCSTR sErrorCode, PCSTR sMessage, PCSTR sSplmMsg) {
printf("TrData) %s\n", sScrNo);
}

void OnReceiveRealData(PCSTR sRealKey, PCSTR sRealType, PCSTR sRealData) {
printf("RealData) %s\n", sRealData);
}
42 changes: 40 additions & 2 deletions include/kw_.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,46 @@
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

KW_API int kw_Initialize();

// 리턴코드
typedef enum OP_ERR {
OP_ERR_NONE = 0, // 정상처리
OP_ERR_FAIL = -10, // 실패
OP_ERR_COND_NOTFOUND = -11, // 조건번호 없음
OP_ERR_COND_MISMATCH = -12, // 조건번호와 조건식 틀림
OP_ERR_COND_OVERFLOW = -13, // 조건검색 조회요청 초과
OP_ERR_LOGIN = -100, // 사용자정보 교환실패
OP_ERR_CONNECT = -101, // 서버접속 실패
OP_ERR_VERSION = -102, // 버전처리 실패
OP_ERR_FIREWALL = -103, // 개인방화벽 실패
OP_ERR_MEMORY = -104, // 메모리보호 실패
OP_ERR_INPUT = -105, // 함수입력값 오류
OP_ERR_SOCKET_CLOSED = -106, // 통신 연결종료
OP_ERR_SISE_OVERFLOW = -200, // 시세조회 과부하
OP_ERR_RQ_STRUCT_FAIL = -201, // 전문작성 초기화 실패
OP_ERR_RQ_STRING_FAIL = -202, // 전문작성 입력값 오류
OP_ERR_NO_DATA = -203, // 데이터 없음
OP_ERR_OVER_MAX_DATA = -204, // 조회 가능한 종목수 초과
OP_ERR_DATA_RCV_FAIL = -205, // 데이터수신 실패
OP_ERR_OVER_MAX_FID = -206, // 조회 가능한 FID수초과
OP_ERR_REAL_CANCEL = -207, // 실시간 해제 오류
OP_ERR_ORD_WRONG_INPUT = -300, // 입력값 오류
OP_ERR_ORD_WRONG_ACCTNO = -301, // 계좌 비밀번호 없음
OP_ERR_OTHER_ACC_USE = -302, // 타인계좌사용 오류
OP_ERR_MIS_2BILL_EXC = -303, // 주문가격이 20억원을 초과
OP_ERR_MIS_5BILL_EXC = -304, // 주문가격이 50억원을 초과
OP_ERR_MIS_1PER_EXC = -305, // 주문수량이 총발행주수의 1%초과오류
OP_ERR_MIS_3PER_EXC = -306, // 주문수량이 총발행주수의 3%초과오류
OP_ERR_SEND_FAIL = -307, // 주문전송 실패
OP_ERR_ORD_OVERFLOW = -308, // 주문전송 과부하
OP_ERR_ORD_OVERFLOW2 = -311, // 주문전송 과부하
OP_ERR_MIS_300CNT_EXC = -309, // 주문수량 300계약 초과
OP_ERR_MIS_500CNT_EXC = -310, // 주문수량 500계약 초과
OP_ERR_ORD_WRONG_ACCTINFO = -340, // 계좌정보없음
OP_ERR_ORD_SYMCODE_EMPTY = -500, // 종목코드없음
}OP_ERR;

KW_API long kw_Initialize(int option);
KW_API void kw_Uninitialize();

KW_API long kw_CommConnect();
Expand Down
76 changes: 34 additions & 42 deletions src/kw_.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <string>
#include <codecvt>
#include <thread>
#include <future>
#include "kwapi.h"

using namespace std;
Expand Down Expand Up @@ -30,49 +31,46 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call,
return TRUE;
}

int kw_Initialize() {
thread_ = thread([](kwapi* pApi) {
pApi->create();
long kw_Initialize(int option) {
if (option == 1) {
promise<bool> p;
future<bool> f = p.get_future();

MSG msg;
while (GetMessage(&msg, nullptr, 0, 0) != 0) {
DispatchMessage(&msg);
}

printf("exit");

}, &api_);

Sleep(1000);
return 0;
thread_ = thread([&p](kwapi* pApi) {
p.set_value(pApi->create());
pApi->waitMessageLoop();
}, &api_);
return f.get() ? OP_ERR_NONE : OP_ERR_FAIL;
}
else {
// 커스텀 오류코드 정의 필요함.
return api_.create() ? OP_ERR_NONE : -9999;
}
}

void kw_Uninitialize() {

api_.destroy();
}

void kw_SetOnEventConnect(kw_OnEventConnect handler) {
api_.setOnEventConnectHandler(handler);
}

void kw_SetOnReceiveTrDataW(kw_OnReceiveTrDataW handler) {
api_->setOnReceiveTrDataHandlerW(handler);
//jktest api_.setOnReceiveTrDataHandlerW(handler);
api_.setOnReceiveTrDataHandlerW(handler);
}

void kw_SetOnReceiveTrDataA(kw_OnReceiveTrDataA handler) {
api_->setOnReceiveTrDataHandlerA(handler);
//jktest api_.setOnReceiveTrDataHandlerA(handler);
api_.setOnReceiveTrDataHandlerA(handler);
}

void kw_SetOnReceiveRealDataW(kw_OnReceiveRealDataW handler) {
api_->SetOnReceiveRealDataW(handler);
//api_.SetOnReceiveRealDataW(handler);
api_.SetOnReceiveRealDataW(handler);
}

void kw_SetOnReceiveRealDataA(kw_OnReceiveRealDataA handler) {
api_->SetOnReceiveRealDataA(handler);
//api_.SetOnReceiveRealDataA(handler);
api_.SetOnReceiveRealDataA(handler);
}

void kw_SetOnReceiveMsgW(kw_OnReceiveMsgW handler) {
Expand Down Expand Up @@ -133,13 +131,11 @@ void kw_SetCharsetUtf8(int useUtf8) {
}

long kw_CommConnect() {
return pApi_->commConnect();
//jktest return api_.commConnect();
return api_.commConnect();
}

long kw_GetConnectState() {
return pApi_->GetConnectState();
//jktest return api_.GetConnectSate();
return api_.GetConnectState();
}

PWSTR kw_GetMasterCodeNameW(PCWSTR sTrCode) {
Expand Down Expand Up @@ -522,14 +518,12 @@ long kw_GetMarketTypeA(PCSTR sTrCode) {

long kw_CommRqDataW(PCWSTR sRQName, PCWSTR sTrCode,
long nPrevNext, PCWSTR sScreenNo) {
return pApi_->CommRqData(sRQName, sTrCode, nPrevNext, sScreenNo);
//jktest return api_.CommRqData(sRQName, sTrCode, nPrevNext, sScreenNo);
return api_.CommRqData(sRQName, sTrCode, nPrevNext, sScreenNo);
}

long kw_CommRqDataA(PCSTR sRQName, PCSTR sTrCode,
long nPrevNext, PCSTR sScreenNo) {
return pApi_->CommRqData(A2B(sRQName), A2B(sTrCode), nPrevNext, A2B(sScreenNo));
//jktest return api_.CommRqData(A2B(sRQName), A2B(sTrCode), nPrevNext, A2B(sScreenNo));
return api_.CommRqData(A2B(sRQName), A2B(sTrCode), nPrevNext, A2B(sScreenNo));
}

PWSTR kw_GetLoginInfoW(PCWSTR sTag) {
Expand Down Expand Up @@ -570,13 +564,11 @@ long kw_SendOrderFOA(PCSTR sRQName, PCSTR sScreenNo, PCSTR sAccNo,
}

void kw_SetInputValueW(PCWSTR sID, PCWSTR sValue) {
pApi_->SetInputValue(sID, sValue);
//jktest api_.SetInputValue(sID, sValue);
api_.SetInputValue(sID, sValue);
}

void kw_SetInputValueA(PCSTR sID, PCSTR sValue) {
pApi_->SetInputValue(A2B(sID), A2B(sValue));
//jktest api_.SetInputValue(A2B(sID), A2B(sValue));
api_.SetInputValue(A2B(sID), A2B(sValue));
}

void kw_DisconnectRealDataW(PCWSTR sScnNo) {
Expand All @@ -603,19 +595,19 @@ long kw_CommKwRqDataW(PCWSTR sArrCode, long bNext, int nCodeCount,

long kw_CommKwRqDataA(PCSTR sArrCode, long bNext, int nCodeCount,
int nTypeFlag, PCSTR sRQName, PCSTR sScreenNo) {
return pApi_->CommKwRqData(A2B(sArrCode), bNext, nCodeCount, nTypeFlag,
return api_.CommKwRqData(A2B(sArrCode), bNext, nCodeCount, nTypeFlag,
A2B(sRQName), A2B(sScreenNo));
//jktest return api_.CommKwRqData(A2B(sArrCode), bNext, nCodeCount, nTypeFlag,
// A2B(sRQName), A2B(sScreenNo));
}


void kw_Wait() {

MSG msg;
while (GetMessage(&msg, nullptr, 0, 0) != 0) {
DispatchMessage(&msg);
if(thread_.joinable()) {
thread_.join();

}
else {
api_.waitMessageLoop();
}
}

void kw_Sleep(int msec) {
Expand Down
Loading

0 comments on commit 4df01bf

Please sign in to comment.