bool NetCenter::ConnectServiceWhitFD(int fd , const char* host_name, int port)
{
    if (!_run_flg)
    {
        return false;
    }

    SocketAddress addr(host_name, port);

    CriticalSectionScoped css(&cs_);
    std::map<SOCKET, struct stSocketObj*>::iterator it = socket_objs.find(fd);
    if (socket_objs.end() == it)
    {
        return false;
    }

    if (it->second->dispatch->Connect(addr) != 0)
    {
        return false;
    }

    if (it->second->dispatch->GetState() == jsbn::AsyncSocket::CS_CONNECTED)
    {
        jmethodID m = GetMethodID(jni(), *j_observer_class_, "StatusReportWithFD", "(II)V");
        jni()->CallVoidMethod(*j_observer_global_, m, it->second->dispatch->GetDescriptor(), (int)ENE_CONNECTED);
    }

    return true;
}
void NetCenter::OnReadEvent(AsyncSocket* socket_)
{
    SocketDispatcher* pSocketDispatcher = static_cast<SocketDispatcher*>(socket_);
    struct stSocketObj* sst = socket_objs[pSocketDispatcher->GetDescriptor()];

    int len = socket_->Recv(sst->inbuf_ + sst->inpos_, sst->insize_ - sst->inpos_);
    if (len < 0)
    {
        if (!socket_->IsBlocking())
        {
            LOGE("Recv() returned error: %d.", socket_->GetError());
        }
        return;
    }

    sst->inpos_ += len;

    while (true)
    {
        if (sst->inpos_ < kPacketLenSize)
        {
            break;
        }

        PacketLength pkt_len = GetBE16(sst->inbuf_);
        if (sst->inpos_ < kPacketLenSize + pkt_len)
        {
            break;
        }

        jbyteArray array = jni()->NewByteArray(pkt_len);//定义数据变量
        jni()->SetByteArrayRegion(array, 0, pkt_len, (const jbyte*)sst->inbuf_+kPacketLenSize);
        jmethodID m = GetMethodID(jni(), *j_observer_class_, "RecvTCPDataWithFD", "(I[BI)V");
        jni()->CallVoidMethod(*j_observer_global_, m, pSocketDispatcher->GetDescriptor(), array, pkt_len);
        jni()->DeleteLocalRef(array);

        sst->inpos_ -= kPacketLenSize + pkt_len;
        if (sst->inpos_ > 0)
        {
            memmove(sst->inbuf_, sst->inbuf_ + kPacketLenSize + pkt_len, sst->inpos_);
        }
    }

    if (sst->inpos_ >= sst->insize_)
    {
        LOGE("input buffer overflow");
        sst->inpos_ = 0;
    }
}
void QAndroidBatteryDataProvider::stop()
{
	if (isJniReady())
	{
		jni()->callVoid("stop");
	}
}
void QAndroidBatteryDataProvider::start()
{
	if (isJniReady())
	{
		jni()->callBool("start");
	}
}
void NetCenter::OnCloseEvent(AsyncSocket* socket, int err)
{
    SocketDispatcher* pSocketDispatcher = static_cast<SocketDispatcher*>(socket);
    jmethodID m = GetMethodID(jni(), *j_observer_class_, "StatusReportWithFD", "(II)V");
    jni()->CallVoidMethod(*j_observer_global_, m, pSocketDispatcher->GetDescriptor(), (int)ENE_CLOSE);
}