/*
 * Called when a packet with the opcode XPT_OPC_S_SHARE_ACK is received
 */
bool xptClient_processPacket_shareAck(xptClient_t* xptClient)
{
	xptPacketbuffer_t* cpb = xptClient->recvBuffer;
	// read data from the packet
	xptPacketbuffer_beginReadPacket(cpb);
	// start parsing
	bool readError = false;
	// read error code field
	uint32 shareErrorCode = xptPacketbuffer_readU32(cpb, &readError);
	if( readError )
		return false;
	// read reject reason
	char rejectReason[512];
	xptPacketbuffer_readString(cpb, rejectReason, 512, &readError);
	rejectReason[511] = '\0';
	float shareValue = xptPacketbuffer_readFloat(cpb, &readError);
	if( readError )
		return false;
	if( shareErrorCode == 0 )
	{
		time_t now = time(0);
		char* dt = ctime(&now);
		//printf("Share accepted by server\n");
		//printf(" [ %d / %d val: %.6f] %s", valid_shares, total_shares, shareValue, dt);
		//primeStats.fShareValue += shareValue;
	}
	else
	{
		// share not accepted by server
		printf("Invalid share\n");
		if( rejectReason[0] != '\0' )
			printf("Reason: %s\n", rejectReason);
	}
	return true;
}
/*
 * Called when a packet with the opcode XPT_OPC_S_SHARE_ACK is received
 */
bool xptClient_processPacket_shareAck(xptClient_t* xptClient)
{
    xptPacketbuffer_t* cpb = xptClient->recvBuffer;
    // read data from the packet
    xptPacketbuffer_beginReadPacket(cpb);
    // start parsing
    bool readError = false;
    // read error code field
    uint32 shareErrorCode = xptPacketbuffer_readU32(cpb, &readError);
    if( readError )
        return false;
    // read reject reason
    char rejectReason[512];
    xptPacketbuffer_readString(cpb, rejectReason, 512, &readError);
    rejectReason[511] = '\0';
    float shareValue = xptPacketbuffer_readFloat(cpb, &readError);
    if( readError )
        return false;
    if( shareErrorCode == 0 )
    {
        total_shares++;
        valid_shares++;
        time_t now = time(0);
        char* dt = ctime(&now);
        std::cout << "ACCEPTED [ " << valid_shares << " / " << total_shares << " val: " << shareValue << "] " << dt << std::endl;
        primeStats.fShareValue += shareValue;
        primeStats.fBlockShareValue += shareValue;
        primeStats.fTotalSubmittedShareValue += shareValue;
    }
    else
    {
        // error logging in -> disconnect
        total_shares++;
        std::cout << "Invalid share" << std::endl;
        if( rejectReason[0] != '\0' )
            std::cout << "Reason: " << rejectReason << std::endl;
    }
    return true;
}