Exemple #1
0
ssize_t CwxSocket::recv (CWX_HANDLE handle, CwxMsgHead& head, CwxMsgBlock*& msg, CwxTimeouter  *timeout) 
{
    char szHead[CwxMsgHead::MSG_HEAD_LEN];
    ssize_t  ret = read_n(handle, szHead, CwxMsgHead::MSG_HEAD_LEN, timeout, NULL);
    if (ret != CwxMsgHead::MSG_HEAD_LEN)
        return ret;
    if (!head.fromNet(szHead))
    {
        errno = EBADMSG;
        return -1;
    }
    msg = CwxMsgBlockAlloc::malloc(head.getDataLen());
    if (!msg)
    {
        errno = ENOMEM;
        return -1;
    }
    ret = recv_n(handle, msg->wr_ptr(), head.getDataLen(), timeout, NULL);
    if (ret != (ssize_t)head.getDataLen())
    {
        CwxMsgBlockAlloc::free(msg);
        msg = NULL;
        return ret;
    }
    msg->wr_ptr(head.getDataLen());
    return CwxMsgHead::MSG_HEAD_LEN + head.getDataLen();

}
int CwxAppHandler4Channel::recvPackage(CWX_HANDLE handle,
                                       CWX_UINT32& uiRecvHeadLen,
                                       CWX_UINT32& uiRecvDataLen,
                                       char*      szRecvHead,
                                       CwxMsgHead& header,
                                       CwxMsgBlock*& msg)
{
    ssize_t recv_size = 0;
    ssize_t need_size = 0;
    need_size = CwxMsgHead::MSG_HEAD_LEN - uiRecvHeadLen;
    if (need_size > 0 )
    {//not get complete head
        recv_size = CwxSocket::recv(handle, szRecvHead + uiRecvHeadLen, need_size);
        if (recv_size <=0 )
        { //error or signal
            if ((0==recv_size) || ((errno != EWOULDBLOCK) && (errno != EINTR)))
            {
                return -1; //error
            }
            else
            {//signal or no data
                return 0;
            }
        }
        uiRecvHeadLen += recv_size;
        if (recv_size < need_size)
        {
            return 0;
        }
        szRecvHead[uiRecvHeadLen] = 0x00;
        if (!header.fromNet(szRecvHead))
        {
            CWX_ERROR(("Msg header is error."));
            return -1;
        }
        msg = CwxMsgBlockAlloc::malloc(header.getDataLen());
        uiRecvDataLen = 0;
    }//end  if (need_size > 0)
    //recv data
    need_size = header.getDataLen() - uiRecvDataLen;
    if (need_size > 0)
    {//not get complete data
        recv_size = CwxSocket::recv(handle, msg->wr_ptr(), need_size);
        if (recv_size <=0 )
        { //error or signal
            if ((errno != EWOULDBLOCK)&&(errno != EINTR))
            {
                return -1; //error
            }
            else
            {//signal or no data
                return 0;
            }
        }
        //move write pointer
        msg->wr_ptr(recv_size);
        uiRecvDataLen += recv_size;
        if (recv_size < need_size)
        {
            return 0;
        }
    }
    return 1;
}
Exemple #3
0
int main(int argc ,char** argv)
{
    int iRet = parseArg(argc, argv);

    if (0 == iRet) return 0;
    if (-1 == iRet) return 1;

    CwxSockStream  stream;
    CwxINetAddr  addr(g_unPort, g_strHost.c_str());
    CwxSockConnector conn;
    if (0 != conn.connect(stream, addr))
    {
        printf("Failure to connect ip:port: %s:%u, errno=%d\n", g_strHost.c_str(), g_unPort, errno);
        return 1;
    }
    CwxPackageWriterEx writer;
    CwxPackageWriterEx writer1;
    CwxPackageReaderEx reader;
    CwxMsgHead head;
    CwxMsgBlock* block=NULL;
    char szErr2K[2048];
	CWX_UINT32  output_buf_len = UNISTOR_MAX_KVS_SIZE;
	char* output_buf = (char*)malloc(output_buf_len);
	bool bGets=g_key.size()>1?true:false;
	do {
		string strField;
        list<string>::iterator iter = g_field.begin();
        while(iter != g_field.end()){
			if (!strField.length()){
				strField = *iter;
			}else{
				strField += "\n";
				strField += *iter;
			}
			iter++;
		}
        CwxKeyValueItemEx  field;
        field.m_szData = strField.c_str();
        field.m_uiDataLen = strField.length();
        field.m_bKeyValue = false;
        CwxKeyValueItemEx  extra;
        extra.m_szData = g_extra.c_str();
        extra.m_uiDataLen = g_extra.length();
        extra.m_bKeyValue = false;
        if (!bGets){
            CwxKeyValueItemEx  key;
            key.m_szData = g_key.begin()->c_str();
            key.m_uiDataLen = g_key.begin()->length();
            key.m_bKeyValue = false;
            if (UNISTOR_ERR_SUCCESS != UnistorPoco::packGetKey(&writer,
                block,
                100,
                key,
                strField.length()?&field:NULL,
                g_extra.length()?&extra:NULL,
                g_bVer,
                g_user.c_str(),
                g_passwd.c_str(),
                g_bMaster,
                g_ucKeyInfo,
                szErr2K))
            {
                printf("failure to pack get key package, err=%s\n", szErr2K);
                iRet = 1;
                break;
            }
        }else{
            list<pair<char const*, CWX_UINT16> > keys;
            list<string>::iterator iter = g_key.begin();
            while(iter != g_key.end()){
                keys.push_back(pair<char const*, CWX_UINT16>(iter->c_str(), iter->length()));
                iter++;
            }
            if (UNISTOR_ERR_SUCCESS != UnistorPoco::packGetKeys(&writer,
                &writer1,
                block,
                100,
                keys,
                strField.length()?&field:NULL,
                g_extra.length()?&extra:NULL,
                g_user.c_str(),
                g_passwd.c_str(),
                g_bMaster,
                g_ucKeyInfo,
                szErr2K))
            {
                printf("failure to pack gets key package, err=%s\n", szErr2K);
                iRet = 1;
                break;
            }
        }
		//send
		if (block->length() != (CWX_UINT32)CwxSocket::write_n(stream.getHandle(),
            block->rd_ptr(),
            block->length()))
        {
            printf("failure to send message, errno=%d\n", errno);
            iRet = 1;
            break;
        }
        CwxMsgBlockAlloc::free(block);
        block = NULL;
        //recv msg
        if (0 >= CwxSocket::read(stream.getHandle(), head, block))
        {
            printf("failure to read the reply, errno=%d\n", errno);
            iRet = 1;
            break;
        }

		if ((bGets?UnistorPoco::MSG_TYPE_RECV_GETS_REPLY:UnistorPoco::MSG_TYPE_RECV_GET_REPLY) != head.getMsgType())
        {
            printf("recv a unknow msg type, task_id=%u, msg_type=%u\n", head.getTaskId(), head.getMsgType());
            iRet = 1;
            break;
        }
		printf("query result, task_id=%u, len=%u, msg_len=%u\n", head.getTaskId(), head.getDataLen(), block->length());
		CwxPackageEx::dump(block->rd_ptr(),
			block->length(),
			output_buf,
			output_buf_len,
			"  ");
		printf("dump len:%u\n", output_buf_len);
		output_buf[output_buf_len] = 0x00;
        for (CWX_UINT32 i=0; i<output_buf_len; i++){
            if (0 == output_buf[i]) output_buf[i]=' ';
        }
		printf("%s\n", output_buf);
    } while(0);
    if (block) CwxMsgBlockAlloc::free(block);
	if (output_buf) free(output_buf);
    stream.close();
    return iRet;
}