Esempio n. 1
0
/* For the most basic example of Fudge-C use, see the "simple.c" file that
   should be in the same directory as this one.

   This is a basic "pretty printer" for Fudge-C encoded message files. It
   takes the filename of the message to display and dumps the contents, in
   human readable form, to the standard output.

   Rather than assume a Unicode compatible console, all strings are
   converted in to 7bit ASCII before being output.
*/
int main ( int argc, char * argv [ ] )
{
    FudgeStatus status;
    FudgeMsgEnvelope envelope;
    const char * filename;
    fudge_byte * bytes;
    fudge_i32 numbytes;

    /* Get the filename from the command-line arguments */
    if ( argc != 2 )
        displayUsage ( );
    filename = argv [ 1 ];

    /* Initialise the Fudge library */
    if ( ( status = Fudge_init ( ) ) )
        fatalFudgeError ( status, "Failed to initialise Fudge library" );

    /* Load in the file and attempt to decode it */
    loadFile ( &bytes, &numbytes, filename );
    if ( ( status = FudgeCodec_decodeMsg ( &envelope, bytes, numbytes ) ) )
        fatalFudgeError ( status, "Failed to decode file" );
    free ( bytes );

    /* Output the file contents */
    outputEnvelope ( envelope );
    FudgeMsgEnvelope_release ( envelope );
    return 0;
}
Esempio n. 2
0
/// Creates a Fudge message from a binary encoding.
///
/// @param[in] pData the encoded message data
/// @param[in] cbData the length of the encoded data in bytes
/// @return the message, or NULL if there is a problem
static FudgeMsg _DecodeFudgeMsg (const void *pData, size_t cbData) {
	FudgeMsgEnvelope env;
	FudgeMsg msg = NULL;
	if (FudgeCodec_decodeMsg (&env, (fudge_byte*)pData, cbData) == FUDGE_OK) {
		msg = FudgeMsgEnvelope_getMessage (env);
		FudgeMsg_retain (msg);
		FudgeMsgEnvelope_release (env);
	} else {
		LOGWARN (TEXT ("Couldn't decode Fudge message"));
	}
	return msg;
}
Esempio n. 3
0
// This must only be called from the thread that creates and connects the pipes. This then
// doesn't need to acquire the pipe semaphore as the object won't be modified concurrently.
// Another thread might be sending, but that's it.
FudgeMsgEnvelope CClientService::Recv (unsigned long lTimeout) {
	FudgeStatus status;
	FudgeMsgHeader header;
	fudge_byte *ptr = (fudge_byte*)m_poPipes->PeekInput (8, lTimeout); // Fudge headers are 8-bytes long
	if (!ptr) {
		int ec = GetLastError ();
		if (ec == ETIMEDOUT) {
			LOGDEBUG (TEXT ("Timeout reading envelope header"));
		} else {
			LOGWARN (TEXT ("Couldn't read Fudge envelope header, error ") << ec);
		}
		SetLastError (ec);
		return NULL;
	}
	if ((status = FudgeHeader_decodeMsgHeader (&header, ptr, 8)) != FUDGE_OK) {
		LOGERROR (TEXT ("Couldn't decode Fudge envelope header, status ") << status);
		SetLastError (EIO_READ);
		return NULL;
	}
	ptr = (fudge_byte*)m_poPipes->PeekInput (header.numbytes, lTimeout);
	if (!ptr) {
		int ec = GetLastError ();
		LOGWARN (TEXT ("Couldn't read full Fudge message (") << header.numbytes << TEXT (" bytes, error ") << ec);
		SetLastError (ec);
		return NULL;
	}
	FudgeMsgEnvelope env;
	status = FudgeCodec_decodeMsg (&env, ptr, header.numbytes);
	m_poPipes->DiscardInput (header.numbytes);
	if (status == FUDGE_OK) {
		return env;
	} else {
		LOGERROR (TEXT ("Couldn't decode Fudge message, status ") << status);
		SetLastError (EIO_READ);
		return NULL;
	}
}
Esempio n. 4
0
int main ( int argc, char * argv [ ] )
{
    FudgeStatus status;
    FudgeMsg message;
    FudgeMsgEnvelope envelope;
    AddressDetails * details [ 2 ];
    fudge_i16 ordinal;
    fudge_byte * encoded;
    fudge_i32 encodedsize;

    /* Initialise the Fudge library */
    if ( ( status = Fudge_init ( ) ) )
        fatalFudgeError ( status, "Failed to initialise Fudge library" );

    /* Register the AddressDetails type */
    if ( ( status = FudgeRegistry_registerType ( FUDGE_TYPE_ADDRESSDETAILS,
                    FUDGE_TYPE_PAYLOAD_BYTES,
                    FudgeCodec_decodeFieldAddressDetails,
                    FudgeCodec_encodeFieldAddressDetails,
                    FudgeType_coerceAddressDetails ) ) )
        fatalFudgeError ( status, "Failed to register AddressDetails type" );

    /* Construct and encode two address details */
    details [ 0 ] = constructAddressDetails ( Status_Past, 123, "Fake St.", "Some City", "P05 T4L" );
    details [ 1 ] = constructAddressDetails ( Status_Active, 45, "Faux Road", "Some Town", "FUD 63C" );

    /* Create a message and add the two details */
    if ( ( status = FudgeMsg_create ( &message ) ) )
        fatalFudgeError ( status, "Failed to create Fudge message" );

    for ( ordinal = 0; ordinal < 2; ++ordinal )
        FudgeMsg_addFieldAddressDetails ( message, 0, &ordinal, details [ ordinal ] );

    /* Encode the message */
    if ( ( status = FudgeMsgEnvelope_create ( &envelope, 0, 0, 0, message ) ) )
        fatalFudgeError ( status, "Failed to create Fudge messag envelope" );

    if ( ( status = FudgeCodec_encodeMsg ( envelope, &encoded, &encodedsize ) ) )
        fatalFudgeError ( status, "Failed to encode Fudge message" );

    /* Clean up source details and messge */
    free ( details [ 0 ] );
    free ( details [ 1 ] );
    FudgeMsgEnvelope_release ( envelope );
    FudgeMsg_release ( message );

    /* Decode the message and release the encoded bytes array */
    if ( ( status = FudgeCodec_decodeMsg ( &envelope, encoded, encodedsize ) ) )
        fatalFudgeError ( status, "Failed to decode Fudge message" );
    free ( encoded );

    /* Retrieve, convert and display the fields */
    for ( ordinal = 0; ordinal < 2; ++ordinal )
    {
        FudgeField field;
        FudgeFieldData data;
        FudgeTypePayload payload;
        fudge_i32 datasize;
        char * ascii;

        if ( ( status = FudgeMsg_getFieldByOrdinal ( &field, FudgeMsgEnvelope_getMessage ( envelope ), ordinal ) ) )
            fatalFudgeError ( status, "Failed to find field" );

        /* Convert the field in to a string */
        if ( ( status = FudgeMsg_getFieldAs ( &field, FUDGE_TYPE_STRING, &data, &payload, &datasize ) ) )
            fatalFudgeError ( status, "Failed to convert field to string" );

        /* This is a bit paranoid, but it's checking that the string
           conversion actually resulted in string payload */
        if ( payload != FUDGE_TYPE_PAYLOAD_STRING )
        {
            fprintf ( stderr, "FATAL ERROR: Retrieving field %d as a string returned a non-string!\n", ordinal );
            exit ( 1 );
        }

        FudgeString_convertToASCIIZ ( &ascii, data.string );
        printf ( "Field %d: %s\n", ordinal, ascii );
        free ( ascii );
        FudgeString_release ( data.string );
    }

    /* Clean up */
    FudgeMsgEnvelope_release ( envelope );
    return 0;
}