Example #1
0
	void XlsSheet::parseXlsSheet( const xlsWorkSheet &xlsSheet ){
		m_MaxRows = xlsSheet.rows.lastrow + 1;

		m_MaxColumns = xlsSheet.rows.lastcol;
		XlsRowStruct tmpRow;
		tmpRow.reserve( m_MaxColumns );
		struct st_row::st_row_data *p_Row;
		for( int indexOfRow = 0; indexOfRow < m_MaxRows; ++indexOfRow ){
			tmpRow.clear();
			p_Row = &( xlsSheet.rows.row[indexOfRow] );
			for( int indexOfColumn = 0; indexOfColumn < m_MaxColumns; ++indexOfColumn ){
				if( p_Row->cells.cell[indexOfColumn].str != NULL ){
					string xlsValue = reinterpret_cast<char*>( p_Row->cells.cell[indexOfColumn].str );
					// 三方库转换数字问题:例如,1被转换为1.000000。需要去除后面额外添加的零。
					if( checkNumber( xlsValue ) ){
						xlsValue = xlsValue.substr( 0, xlsValue.find(".") );
					}
					tmpRow.push_back( xlsValue );
				}
				else{
					tmpRow.push_back("");
				}
			}
			m_XlsContent.push_back( tmpRow );
		}
		DEBUG_D("end sheet parse");
	}
Example #2
0
void NVEventUnhandledEvent(NVEvent* ev)
{
	if (s_supportPauseResume && (ev->m_type == NV_EVENT_PAUSE || ev->m_type == NV_EVENT_QUIT))
	{
		DEBUG_D("\"supportPauseResume\" applications should handle NV_EVENT_PAUSE and NV_EVENT_QUIT");
	}
}
static void my_debug( void *ctx, int level,
		const char *file, int line,
		const char *str )
{
#if PLATFORM_ID!=3
	DEBUG_D("%s:%04d: %s", file, line, str);
#else
	fprintf(stdout, "%s:%04d: %s", file, line, str);
	fflush(stdout);
#endif
}
Example #4
0
	void doTest(const string &arg, const string &ip){
		SingleLogServer::getInstance()->InitLog("./log", "network_");

		g_Ip = ip;

		string argTmp = strToLower(arg);
		DEBUG_D("run doTest : = " << argTmp);

		//doLink(argTmp);
		//doMemCpy(argTmp);
		doPerformance(argTmp);
	}
Example #5
0
	void doMemCpy(const string &arg){
		// 转换class为二进制流的方法未找到(有复杂的方式,但没必要使用),暂时只能使用结构体。
		DataStruct dataStruct;
		dataStruct.i = 100;
		dataStruct.d = 100.1;
		dataStruct.c = 'A';

		if(arg == ARG_CLIENT){
			if(net_connect(g_IP, TEST_CONNECT_PORT));

			PackerPtr pPacker(new Packer(NULL));
			DEBUG_D("data struct size " << sizeof(dataStruct));
			pPacker->setBuffer(&dataStruct, sizeof(dataStruct));
			net_sendAll_C(pPacker);

			sleep(10);
		}
		if(arg == ARG_SERVER){
			net_listen(g_IP, TEST_LISTEN_PORT);
			int recvSize = 0;
			while(1){
				PackerPtr pPacker;
				if(net_recv_S(pPacker)){
					DataStruct* pDataStruct = new DataStruct();
					memcpy(pDataStruct, pPacker->getBuffer(), pPacker->getBufferSize());
					if(dataStruct.i == pDataStruct->i && dataStruct.d == pDataStruct->d && dataStruct.c == pDataStruct->c ){
						DEBUG_D("Struct 接收到消息");
						break;
					}
					else{
						DEBUG_D("接收到的消息和发送消息不一致. ");
						break;
					}
					delete pDataStruct; pDataStruct = NULL;
				}
			}
		}

	}
Example #6
0
	void BinaryMemory::print(string expand)const{
		if(m_Buffer == NULL){
			return;
		}

		Byte* msg = (Byte*)m_Buffer;
		size_t index(0);
		ostringstream ostr;
		ostr << expand << " buffer is [";
		while(index < m_CurBufferSize){
			ostr << (uint16)(unsigned char)msg[index] << "|";
			++index;
		}
		ostr << "]";
		DEBUG_D(ostr.str());
	}
Example #7
0
	void doLink(const string &arg){
		if(arg == ARG_CLIENT){
			bool bRet = net_connect(g_IP, TEST_CONNECT_PORT);
			if(!bRet){ DEBUG_E("client 连接失败。"); }

			DEBUG_D("链接成功,准备发送数据。");
			PackerPtr pPacker(new Packer(NULL));
			pPacker->setBuffer(g_TestContext_1.c_str(), g_TestContext_1.size());

			DEBUG_D("发送数据:" << g_TestContext_1);
			net_sendAll_C(pPacker);

			sleep(10);
		}
		if(arg == ARG_SERVER){
			bool bRet = net_listen(g_IP, TEST_LISTEN_PORT);
			if(!bRet){ DEBUG_E("server 监听失败。"); }

			while(bRet){
				DEBUG_D("开始监听,等待链接和数据。");
				PackerPtr pPacker;
				if(net_recv_S(pPacker)){
					DEBUG_D("成功接收数据。");
					string ret((char*)pPacker->getBuffer(), pPacker->getBufferSize());
					if(g_TestContext_1 == ret){
						DEBUG_D("接收到消息" << ret );
						break;
					}
					else{
						DEBUG_D("接收到的消息和发送消息不一致. " << g_TestContext_1 << " != " << ret);
						break;
					}
				}

				usleep(1000);
			}
		}
	}
void ConnectionSocket::openConnection(std::string address, uint16_t port, bool ipv6, int32_t networkType) {
    currentNetworkType = networkType;
    isIpv6 = ipv6;
    currentAddress = address;
    currentPort = port;
    int epolFd = ConnectionsManager::getInstance().epolFd;
    ConnectionsManager::getInstance().attachConnection(this);

    memset(&socketAddress, 0, sizeof(sockaddr_in));
    memset(&socketAddress6, 0, sizeof(sockaddr_in6));

    if (!ConnectionsManager::getInstance().proxyAddress.empty()) {
        std::string &proxyAddress = ConnectionsManager::getInstance().proxyAddress;
        if ((socketFd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
            DEBUG_E("connection(%p) can't create proxy socket", this);
            closeSocket(1);
            return;
        }
        proxyAuthState = 1;
        socketAddress.sin_family = AF_INET;
        socketAddress.sin_port = htons(ConnectionsManager::getInstance().proxyPort);
        bool continueCheckAddress;
        if (inet_pton(AF_INET, proxyAddress.c_str(), &socketAddress.sin_addr.s_addr) != 1) {
            continueCheckAddress = true;
            DEBUG_D("connection(%p) not ipv4 address %s", this, proxyAddress.c_str());
        } else {
            continueCheckAddress = false;
        }
        if (continueCheckAddress) {
            if (inet_pton(AF_INET6, proxyAddress.c_str(), &socketAddress.sin_addr.s_addr) != 1) {
                continueCheckAddress = true;
                DEBUG_D("connection(%p) not ipv6 address %s", this, proxyAddress.c_str());
            } else {
                continueCheckAddress = false;
            }
            if (continueCheckAddress) {
                struct hostent *he;
                if ((he = gethostbyname(proxyAddress.c_str())) == nullptr) {
                    DEBUG_E("connection(%p) can't resolve host %s address", this, proxyAddress.c_str());
                    closeSocket(1);
                    return;
                }
                struct in_addr **addr_list = (struct in_addr **) he->h_addr_list;
                if (addr_list[0] != nullptr) {
                    socketAddress.sin_addr.s_addr = addr_list[0]->s_addr;
                    DEBUG_D("connection(%p) resolved host %s address %x", this, proxyAddress.c_str(), addr_list[0]->s_addr);
                } else {
                    DEBUG_E("connection(%p) can't resolve host %s address", this, proxyAddress.c_str());
                    closeSocket(1);
                    return;
                }
            }
        }
    } else {
        proxyAuthState = 0;
        if ((socketFd = socket(ipv6 ? AF_INET6 : AF_INET, SOCK_STREAM, 0)) < 0) {
            DEBUG_E("connection(%p) can't create socket", this);
            closeSocket(1);
            return;
        }
        if (ipv6) {
            socketAddress6.sin6_family = AF_INET6;
            socketAddress6.sin6_port = htons(port);
            if (inet_pton(AF_INET6, address.c_str(), &socketAddress6.sin6_addr.s6_addr) != 1) {
                DEBUG_E("connection(%p) bad ipv6 %s", this, address.c_str());
                closeSocket(1);
                return;
            }
        } else {
            socketAddress.sin_family = AF_INET;
            socketAddress.sin_port = htons(port);
            if (inet_pton(AF_INET, address.c_str(), &socketAddress.sin_addr.s_addr) != 1) {
                DEBUG_E("connection(%p) bad ipv4 %s", this, address.c_str());
                closeSocket(1);
                return;
            }
        }
    }

    int yes = 1;
    if (setsockopt(socketFd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(int))) {
        DEBUG_E("connection(%p) set TCP_NODELAY failed", this);
    }

    if (fcntl(socketFd, F_SETFL, O_NONBLOCK) == -1) {
        DEBUG_E("connection(%p) set O_NONBLOCK failed", this);
        closeSocket(1);
        return;
    }

    if (connect(socketFd, (ipv6 ? (sockaddr *) &socketAddress6 : (sockaddr *) &socketAddress), (socklen_t) (ipv6 ? sizeof(sockaddr_in6) : sizeof(sockaddr_in))) == -1 && errno != EINPROGRESS) {
        closeSocket(1);
    } else {
        eventMask.events = EPOLLOUT | EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLET;
        eventMask.data.ptr = eventObject;
        if (epoll_ctl(epolFd, EPOLL_CTL_ADD, socketFd, &eventMask) != 0) {
            DEBUG_E("connection(%p) epoll_ctl, adding socket failed", this);
            closeSocket(1);
        }
    }
}
void ConnectionSocket::onEvent(uint32_t events) {
    if (events & EPOLLIN) {
        if (checkSocketError()) {
            closeSocket(1);
            return;
        } else {
            ssize_t readCount;
            NativeByteBuffer *buffer = ConnectionsManager::getInstance().networkBuffer;
            while (true) {
                buffer->rewind();
                readCount = recv(socketFd, buffer->bytes(), READ_BUFFER_SIZE, 0);
                if (readCount < 0) {
                    closeSocket(1);
                    DEBUG_E("connection(%p) recv failed", this);
                    return;
                }
                if (readCount > 0) {
                    buffer->limit((uint32_t) readCount);
                    lastEventTime = ConnectionsManager::getInstance().getCurrentTimeMonotonicMillis();
                    if (proxyAuthState == 2) {
                        if (readCount == 2) {
                            uint8_t auth_method = buffer->bytes()[1];
                            if (auth_method == 0xff) {
                                closeSocket(1);
                                DEBUG_E("connection(%p) unsupported proxy auth method", this);
                            } else if (auth_method == 0x02) {
                                DEBUG_D("connection(%p) proxy auth required", this);
                                proxyAuthState = 3;
                            } else if (auth_method == 0x00) {
                                proxyAuthState = 5;
                            }
                            adjustWriteOp();
                        } else {
                            closeSocket(1);
                            DEBUG_E("connection(%p) invalid proxy response on state 2", this);
                        }
                    } else if (proxyAuthState == 4) {
                        if (readCount == 2) {
                            uint8_t auth_method = buffer->bytes()[1];
                            if (auth_method != 0x00) {
                                closeSocket(1);
                                DEBUG_E("connection(%p) auth invalid", this);
                            } else {
                                proxyAuthState = 5;
                            }
                            adjustWriteOp();
                        } else {
                            closeSocket(1);
                            DEBUG_E("connection(%p) invalid proxy response on state 4", this);
                        }
                    } else if (proxyAuthState == 6) {
                        if (readCount > 2) {
                            uint8_t status = buffer->bytes()[1];
                            if (status == 0x00) {
                                DEBUG_D("connection(%p) connected via proxy", this);
                                proxyAuthState = 0;
                                adjustWriteOp();
                            } else {
                                closeSocket(1);
                                DEBUG_E("connection(%p) invalid proxy status on state 6, 0x%x", this, status);
                            }
                        } else {
                            closeSocket(1);
                            DEBUG_E("connection(%p) invalid proxy response on state 6", this);
                        }
                    } else if (proxyAuthState == 0) {
                        if (ConnectionsManager::getInstance().delegate != nullptr) {
                            ConnectionsManager::getInstance().delegate->onBytesReceived(readCount, currentNetworkType);
                        }
                        onReceivedData(buffer);
                    }
                }
                if (readCount != READ_BUFFER_SIZE) {
                    break;
                }
            }
        }
    }
    if (events & EPOLLOUT) {
        if (checkSocketError() != 0) {
            closeSocket(1);
            return;
        } else {
            if (proxyAuthState != 0) {
                static uint8_t buffer[1024];
                if (proxyAuthState == 1) {
                    lastEventTime = ConnectionsManager::getInstance().getCurrentTimeMonotonicMillis();
                    proxyAuthState = 2;
                    buffer[0] = 0x05;
                    buffer[1] = 0x02;
                    buffer[2] = 0x00;
                    buffer[3] = 0x02;
                    if (send(socketFd, buffer, 4, 0) < 0) {
                        DEBUG_E("connection(%p) send failed", this);
                        closeSocket(1);
                        return;
                    }
                    adjustWriteOp();
                } else if (proxyAuthState == 3) {
                    buffer[0] = 0x01;
                    uint8_t len1 = (uint8_t) ConnectionsManager::getInstance().proxyUser.length();
                    uint8_t len2 = (uint8_t) ConnectionsManager::getInstance().proxyPassword.length();
                    buffer[1] = len1;
                    memcpy(&buffer[2], ConnectionsManager::getInstance().proxyUser.c_str(), len1);
                    buffer[2 + len1] = len2;
                    memcpy(&buffer[3 + len1], ConnectionsManager::getInstance().proxyPassword.c_str(), len2);
                    proxyAuthState = 4;
                    if (send(socketFd, buffer, 3 + len1 + len2, 0) < 0) {
                        DEBUG_E("connection(%p) send failed", this);
                        closeSocket(1);
                        return;
                    }
                    adjustWriteOp();
                } else if (proxyAuthState == 5) {
                    buffer[0] = 0x05;
                    buffer[1] = 0x01;
                    buffer[2] = 0x00;
                    buffer[3] = (uint8_t) (isIpv6 ? 0x04 : 0x01);
                    uint16_t networkPort = ntohs(currentPort);
                    inet_pton(isIpv6 ? AF_INET6 : AF_INET, currentAddress.c_str(), &buffer[4]);
                    memcpy(&buffer[4 + (isIpv6 ? 16 : 4)], &networkPort, sizeof(uint16_t));
                    proxyAuthState = 6;
                    if (send(socketFd, buffer, 4 + (isIpv6 ? 16 : 4) + 2, 0) < 0) {
                        DEBUG_E("connection(%p) send failed", this);
                        closeSocket(1);
                        return;
                    }
                    adjustWriteOp();
                }
            } else {
                if (!onConnectedSent) {
                    lastEventTime = ConnectionsManager::getInstance().getCurrentTimeMonotonicMillis();
                    onConnected();
                    onConnectedSent = true;
                }
                NativeByteBuffer *buffer = ConnectionsManager::getInstance().networkBuffer;
                buffer->clear();
                outgoingByteStream->get(buffer);
                buffer->flip();

                uint32_t remaining = buffer->remaining();
                if (remaining) {
                    ssize_t sentLength;
                    if ((sentLength = send(socketFd, buffer->bytes(), remaining, 0)) < 0) {
                        DEBUG_E("connection(%p) send failed", this);
                        closeSocket(1);
                        return;
                    } else {
                        if (ConnectionsManager::getInstance().delegate != nullptr) {
                            ConnectionsManager::getInstance().delegate->onBytesSent(sentLength, currentNetworkType);
                        }
                        outgoingByteStream->discard((uint32_t) sentLength);
                        adjustWriteOp();
                    }
                }
            }
        }
    }
    if ((events & EPOLLRDHUP) || (events & EPOLLHUP)) {
        DEBUG_E("socket event has EPOLLHUP");
        closeSocket(1);
        return;
    }
    if (events & EPOLLERR) {
        DEBUG_E("connection(%p) epoll error", this);
        return;
    }
}