목차

XPlusHttpSvr 가이드

이 화면은 XPlusHttpSvr.ocx 컴포넌트에 대한 샘플 화면이다.

XPlusHttpSvr.ocx 컴포넌트는 HTTP 프로토콜에 대한 서버 기능을 제공하는 컴포넌트이다.

예시

템플릿 위치: /XPLUS/xplus_httpsvr

템플릿 파일

화면 스크립트

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/////////////////////////////////////////////////////////////////////////////////////////
// EVENT START
/////////////////////////////////////////////////////////////////////////////////////////
 
/**
 * XTcpSvrComm 데이터 수신 이벤트 처리
 * @param objInst XTcpSvrComm 오브젝트
 * @param pIpAddr 데이터를 송신한 시스템의 IP 주소
 * @param nPortNo 데이터를 송신한 시스템의 TCP 포트 번호
 * @param nSocketKey 원격지 시스템 연결 소켓 키 값
 * @param pHeader HTTP 헤더 문자열(맨 마지막 \r\n은 제거되어 있음)
 * @param nLength 수신한 데이터 길이
 * @param pTranKey 수신한 데이터에 대한 Key값, GetData 함수에서 사용됨
 */
function objXPlusHttpSvr_OnReceive(objInst,pRemoteIpAddr,nRemotePortNo,nSocketKey,pHeader,nLength,pDataKey)
{
    factory.consoleprint("OnReceive> pRemoteIpAddr = " + pRemoteIpAddr);
    factory.consoleprint("OnReceive> nRemotePortNo = " + nRemotePortNo);
    factory.consoleprint("OnReceive> nSocketKey = " + nSocketKey);
    factory.consoleprint("OnReceive> pHeader = " + pHeader);
    factory.consoleprint("OnReceive> nLength = " + nLength);
    factory.consoleprint("OnReceive> pDataKey = " + pDataKey);
 
    fldRecvIpAddr.settext(pRemoteIpAddr);
    fldRecvPortNo.settext(nRemotePortNo);
    fldRecvDataLength.settext(nLength);
 
    /**
     * 실제 수신된 데이터를 구함
     * @return strRecvData 수신한 데이터
     */
    var strRecvData = objXPlusHttpSvr.innerctrl.GetData(pDataKey);
    factory.consoleprint("RecvData = " + strRecvData);
    fldRecvData.settext(strRecvData);
 
    if (chkAutoReply.getcheck()) {
        SendResponseData(pRemoteIpAddr, nRemotePortNo, nSocketKey);
    }
 
    return;
}
 
/**
 * TCP 세션이 해제되면 발생하는 이벤트 처리
 * CloseSession 함수 호출시에는 이벤트가 발생하지 않는다.
 * @param objInst XTcpSvrComm 오브젝트
 * @param pRemoteIpAddr 세션이 해제된 시스템의 IP 주소
 * @param nRemotePortNo 세션이 해제된 시스템의 TCP 포트 번호
 * @param nSocketKey 원격지 시스템 연결 소켓 키 값
 */
function objXPlusHttpSvr_OnClose(objInst,pRemoteIpAddr,nRemotePortNo,nSocketKey)
{
    factory.consoleprint("OnClose> pRemoteIpAddr = " + pRemoteIpAddr);
    factory.consoleprint("OnClose> nRemotePortNo = " + nRemotePortNo);
    factory.consoleprint("OnClose> nSocketKey = " + nSocketKey);
 
    var nRowCount = grdSessionList.getrowcount();
    var i;
 
    for(i = 0; i < nRowCount; i++) {
        if(grdSessionList.getitemtext(i, 0) == pRemoteIpAddr) {
            if(grdSessionList.getitemtext(i, 1) == nRemotePortNo) {
                grdSessionList.deleterow(i);
                break;
            }
        }
    }
 
    return;
}
 
/**
 * TCP 세션이 연결되면 발생하는 이벤트 처리
 * @param objInst XTcpSvrComm 오브젝트
 * @param pRemoteIpAddr 원격지 시스템의 IP 주소
 * @param nRemotePortNo 원격지 시스템의 TCP 포트 번호
 * @param nSocketKey 원격지 시스템 연결 소켓 키 값
 */
function objXPlusHttpSvr_OnConnect(objInst,pRemoteIpAddr,nRemotePortNo,nSocketKey)
{
    factory.consoleprint("OnConnect> pRemoteIpAddr = " + pRemoteIpAddr);
    factory.consoleprint("OnConnect> nRemotePortNo = " + nRemotePortNo);
    factory.consoleprint("OnConnect> nSocketKey = " + nSocketKey);
 
    var nRow = grdSessionList.additem();
    grdSessionList.setitemtext(nRow, 0, pRemoteIpAddr);
    grdSessionList.setitemtext(nRow, 1, nRemotePortNo);
    grdSessionList.setitemtext(nRow, 2, nSocketKey);
}
 
/////////////////////////////////////////////////////////////////////////////////////////
// EVENT END
/////////////////////////////////////////////////////////////////////////////////////////
 
// XTcpSvrComm 초기화
function btnInitXSvrComm_on_mouseup(objInst)
{
    var strLogDir = "C:\\xFrame\\log";
    var nRet;
 
    /**
     * XPlusHttpSvr 컴포넌트를 초기화한다.
     * @param pLogDir 로그 저장 디렉토리
     * @param pLogFilePrefix 로그 파일 이름
     * @param pLogLevel 로그 레벨("DEBUG"<"INFO"<"WARN"<"ERROR")
     *                  ("DEBUG"로 지정시에만 송수신 데이터 덤프 로그가 기록됨)
     * @param nLogEncFlag 로그 파일 내용 암호화 여부
     * @param nBindPortNo Listen TCP 포트 번호
     * @return
     *  0 : Success
     *  1 : Invalid Length Field Length
     *  9 : Already Initialized
     */
    nRet = objXPlusHttpSvr.innerctrl.InitXPlusHttpSvr(strLogDir, "XPlusHttpSvr", "DEBUG", 0, fldBindPortNo.gettext());
    factory.consoleprint("InitXPlusHttpSvr Return Value = " + nRet);
    if(nRet != 0) {
        screen.alert("InitXPlusHttpSvr() Fail, ErrorCode = " + nRet);
    }
 
    /**
     * TCP 송수신 데이터에 대한 UTF8 문자셋 처리 설정
     * @param nSocketKey 소켓 키 값
     * @returns 연결 여부 값
     *      0: Success
     *      1: Invalid Parameter
     */
    nRet = objXPlusHttpSvr.innerctrl.SetUtf8Flag(1);
    factory.consoleprint("SetUtf8Flag Return Value = " + nRet);
 
    /**
     * TCP 데이터 송신 타임아웃(단위: 밀리초) 설정
     * @param nSocketKey 소켓 키 값
     * @returns 연결 여부 값
     *      0: Success
     *      1: Invalid Parameter
     */
    nRet = objXPlusHttpSvr.innerctrl.SetSendTimeout(5000);  // 5초
    factory.consoleprint("SetSendTimeout Return Value = " + nRet);
}
 
// XTcpSvrComm 시작 버튼 클릭 이벤트 처리
function btnStartXSvrComm_on_mouseup(objInst)
{
    /**
     * XPlusHttpSvr 소켓을 열고 TCP 접속 대기
    * @param nBindPortNo Listen할 TCP 포트 번호 (0 값을 지정할 경우, InitXPlusHttpSvr 함수에서 지정한 포트 사용)
     * @returns 처리 결과 값
     *      0: Success
     *      1: Invalid Port No
     *      2: Fail To Create Socket
     *      3: Fail To Socket Listen
     *      9: Not Initialized
     */
    nRet = objXPlusHttpSvr.innerctrl.StartXPlusHttpSvr(0);
    factory.consoleprint("StartXSvrComm StartXPlusHttpSvr Value = " + nRet);
    if(nRet != 0) {
        screen.alert("StartXPlusHttpSvr() Fail, ErrorCode = " + nRet);
    }
}
 
// XTcpSvrComm 종료 버튼 클릭 이벤트 처리
function btnStopXSvrComm_on_mouseup(objInst)
{
    /**
     * XPlusHttpSvr 소켓을 닫고, 연결된 TCP 접속 해제
     * @param nCloseSessionFlag 연결되어 있는 TCP 세션 해제 여부
     * @param nFireEventFlag 세션 해제 이벤트 발생 처리 여부 플래그
     * @returns 처리 결과 값
     *      0: Success
     *      9: Not Initialized
     */
    var nCloseSessionFlag = 1;
    var nFireEventFlag = 0;
 
    nRet = objXPusHttpSvr.innerctrl.StopXPlusHttpSvr(nCloseSessionFlag, nFireEventFlag);
    factory.consoleprint("StopXPlusHttpSvr Return Value = " + nRet);
    if(nRet != 0) {
        screen.alert("StopXPlusHttpSvr() Fail, ErrorCode = " + nRet);
    }
 
    if(nCloseSessionFlag == 1 && nFireEventFlag == 0) {
        grdSessionList.deleteall();
    }
}
 
// 세션 해제 버튼 클릭 이벤트 처리
function btnCloseSession_on_mouseup(objInst)
{
    var nCheckedRowCount = grdSessionList.getcheckedrowcount();
    if(nCheckedRowCount == 0) {
        screen.alert("연결 해제할 세션을 체크하세요.");
        return;
    }
 
    var nCheckedRow = grdSessionList.getcheckedrow(0);
    var strRemoteIpAddr = grdSessionList.getitemtext(nCheckedRow, 0);
    var strRemotePortNo = grdSessionList.getitemtext(nCheckedRow, 1);
    var nRet;
 
    /**
     * 지정된 TCP 세션을 해재한다.
     * @param strRemoteIpAddr 데이터 수신 대상 시스템 IP 주소
     * @param strRemotePortNo 데이터 수신 대상 포트 번호
     * @param nFireEventFlag 세션 해제 이벤트 발생 처리 여부 플래그
     * @return
     *  0 : OK
     *  1 : Invalid Parameter
     *  2 : Fail To Find Session
     *  9 : Not Initialized
     */
    var nFireEventFlag = 1;
    nRet = objXPusHttpSvr.innerctrl.CloseSession(strRemoteIpAddr, strRemotePortNo, nFireEventFlag);
    // nRet = objXPusHttpSvr.innerctrl.CloseSessionByKey(nSocketKey, nFireEventFlag);
    factory.consoleprint("CloseSession Return Value = " + nRet);
    if(nRet != 0) {
        screen.alert("CloseSession() Fail, ErrorCode = " + nRet);
    }
}
 
// 데이터 송신 버튼 클릭 이벤트 처리
function btnSendData_on_mouseup(objInst)
{
    var nCheckedRowCount = grdSessionList.getcheckedrowcount();
    if(nCheckedRowCount == 0) {
        screen.alert("데이터를 송신할 세션을 체크하세요.");
        return;
    }
 
    var nCheckedRow = grdSessionList.getcheckedrow(0);
    var strRemoteIpAddr = grdSessionList.getitemtext(nCheckedRow, 0);
    var nRemotePortNo = grdSessionList.getitemtext(nCheckedRow, 1);
    var nSocketKey = grdSessionList.getitemtext(nCheckedRow, 2);
    var strSendData = fldSendData.gettext();
    var nRet;
 
    SendResponseData(strRemoteIpAddr, nRemotePortNo, nSocketKey, strSendData, strSendData);
}
 
function SendResponseData(strRemoteIpAddr, nRemotePortNo, nSocketKey, strData)
{
    var strSendDataArr = [];
 
    if (strData === undefined || strData.length == 0) {
        strData = factory.getsystemtime("%Y-%M-%D %h:%m:%s %ms");
    }
 
    /**
     * 데이터를 지정된 세션으로 송신한다.
     * @param strRemoteIpAddr 데이터 수신 대상 시스템 IP 주소
     * @param strRemotePortNo 데이터 수신 대상 포트 번호
     * @param nSendDataLength 송신 데이터 길이 (단위: 바이트)
     * @param strSendData 송신 데이터
     * @return
     *  0 : OK
     *  1 : Fail To Find Session
     *  2 : Invalid Parameter
     *  3 : Session is Not Connected
     *  4 : Fail To Send Length Part
     *  5 : Fail To Send Data Part
     *  9 : Not Initialized
     */
 
    // HTTP Header 데이터는 맨 뒤에 \r\n 데이터를 붙여야 함(예: "aaa:bbb\r\nccc:ddd\r\n");
    var strHeader = "";
 
    nRet = objXPlusHttpSvr.innerctrl.SendData(strRemoteIpAddr, nRemotePortNo, strHeader, strData);
    // nRet = objXPlusHttpSvr.innerctrl.SendDataByKey(nSocketKey, "", strData);
    factory.consoleprint("SendData Return Value = " + nRet);
    if(nRet != 0) {
        screen.alert("SendData() Fail, ErrorCode = " + nRet);
    }
}
 
// 수신 데이터 정보 초기화 버튼 클릭 이벤트 처리
function btnClearData_on_mouseup(objInst)
{
    fldRecvIpAddr.settext("");
    fldRecvPortNo.settext("");
    fldRecvDataLength.settext("");
    fldRecvData.settext("");
}
 
function btnGetSessionCount_on_mouseup(objInst)
{
    screen.alert(objXPlusHttpSvr.innerctrl.GetSessionCount());
}