/home/mjc1/public_html/plugin/nicepay/lib/nicepay/core/IoAdaptorTransport.php


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
<?php
/**
 * 
 * @author kblee
 *
 */
class IoAdaptorTransport {
    
    
/**
     * 
     * @var $socket
     */
    
private $socket;
    
    
/**
     * 
     */
    
public function IoAdaptorTransport(){
        
    }
    
    
/**
     * 
     * @param unknown_type $socket
     */
    
public function setSocket($socket){
        
$this->socket $socket;
    }
    
    
/**
     * 
     * @param unknown_type $msg
     */
    
public function doTrx($msg) {
        
//set_time_limit(CONNECT_TIMEOUT);
        
        
if(LogMode::isAppLogable()){
            
$logJournal NicePayLogJournal::getInstance();
        }

        try{
            
$address gethostbyname(NICEPAY_DOMAIN_NAME);
        }catch (
Exception $e){
            if(
LogMode::isAppLogable()) $logJournal->writeAppLog("PG SERVER NOT FOUND" );
            throw new 
ServiceException("X001","¼­¹ö µµ¸ÞÀθíÀÌ À߸ø ¼³Á¤µÇ¾ú½À´Ï´Ù. : "+$e->getMessage());
        }
        
$socket null;
        try{
            
$socket socket_create(AF_INETSOCK_STREAMSOL_TCP);
            
socket_connect($socket$addressNICEPAY_ADAPTOR_LISTEN_PORT);
        }catch(
Exception $e){
            if(
LogMode::isAppLogable()) $logJournal->writeAppLog("SERVER CONNECT ERROR" );
            throw new 
ServiceException("X002","¼­¹ö·Î ¼ÒÄÏ ¿¬°á Áß ¿À·ù°¡ ¹ß»ýÇÏ¿´½À´Ï´Ù. : "+$e->getMessage());
        }
        
        if(
LogMode::isAppLogable()) $logJournal->writeAppLog("SERVER CONNECT OK" );
        

        
socket_write($socket,$msg);
        
        
$recvMessage $this->readData($socket);
        
        
        
        
socket_close($socket);
        
        return 
$recvMessage;
    }
    
    
/**
     * 
     * @param unknown_type $socket
     */
    
private function readData($socket){
        
$buffer = array();
        try{
            
$data socket_read($socket,256,PHP_BINARY_READ);
            
            
$dataLength strlen($data);
            
            if(
$dataLength >= LENGTH_END_POS){
                
                
                
$readLengthStr substr($data,LENGTH_START_POS,LENGTH_MSG_SIZE);
                
                
$readLengthStr $readLengthStr==null?"0":$readLengthStr;
                
                
                
$mustReadLength = (int)$readLengthStr;
                
                
$buffer array_merge($buffer,str_split($data));
                
                
                
$repeatReadCnt 0;
                
$readCnt strlen($data);
                
$readData null;
                
                
                
                
                while((
$readData socket_read($socket,1024,PHP_BINARY_READ))!==false){
                    
$buffer array_merge($buffer,str_split($readData));
                    
$repeatReadCount strlen($readData);
                    
$readCnt+=$repeatReadCount;
                    if(
$readCnt>=$mustReadLength){
                        break;
                    }
                }
                
                return 
implode($buffer);
            }else{
                throw new 
ServiceException("T002","ºñÁ¤»óÀûÀΠ¼ö½Å Àü¹®ÀÔ´Ï´Ù.");
            }
            
        }catch(
ServiceException $e){
            throw 
$e;
        }catch (
Exception $e){
            throw new 
ServiceException("T002","ºñÁ¤»óÀûÀΠ¼ö½Å Àü¹®ÀÔ´Ï´Ù.");
        }
        
    }
    
}

?>