PDA

View Full Version : Unofficial UDP Streaming C/C++ Development Kit (Windows/Linux)



TrevorP
23-12-2015, 04:30
Hi,

I was going through the UDP streaming posts and found the current state of the API fairly confusing to deal with, as it is a cross between the shared memory interface and the new UDP data streaming structures. So, I decided to create a simplified C/C++ header and example program for dealing with the UDP stream data.

Version 01:


- Unified C header containing all data structures that are sent from the UDP broadcast server (255.255.255.255:5606).
- Unpacking macros to extracting bitfield data members from the various data packets.
- Consolidated enum lists (including the DPad and Joypad enums).
- Windows (and hopefully linux soon) implementation of a UDP client for reading and then interpreting telemetry data.

I have tested this using gcc (MinGW) using a dummy UDP server to send packets (I don't have my XBOX with me right now), so it is possible it may not work exactly the same with Project Cars.

To compile with gcc on Windows you can use:

gcc pcars_udp.c -o pcars_udp -lws2_32

You can also use this with Visual C/C++ if you want, but might need something like the following in your source code:

#pragma comment(lib,"ws2_32.lib") // Visual C++ Winsock Library Include

Version 02:


- Cleaned up client example code and added a makefile. To run type 'make pcars_client_example', and then 'pcars_client_example.exe'.
- Created packet dispatcher API where by packet "handlers" can be registered, and will then be called when valid UDP packet data is received.
- Dispatcher will automatically check packet version, and data size. Eventually it will also allow for automatic "unpacking" so bit-field compressed entries will be expanded into individual data members.

NOTE: I don't have the correct UDP build version number in there right now, so you may need to adjust that in pcars_udp_types.h for now.


Version 03:


- Refactored socket connection code into easy-to-use PCARS_UDP_Stream functions, and updated the client example accordingly.

To open the UDP stream all you have to do is:


sPCARSUDPStream pcars_stream;
char buffer[PCARS_UDP_STREAM_BUFSIZE];

// Create and open PCARS UDP Stream
//
// This is essentially a wrapper on the required socket functions to receive
// the UDP packets
PCARS_UDP_Stream_Init(&pcars_stream, PORT);
result = PCARS_UDP_Stream_Open(&pcars_stream);
if (!result) {
printf("Failed to open PCARS UDP stream.\n");
exit(EXIT_FAILURE);
}

Packets can then be received with:


size = PCARS_UDP_Stream_ReceivePacket(&pcars_stream, buffer);
if (!size) {
break;
}

NOTE: I still don't have the correct UDP build version number in there right now, so you may need to adjust that in pcars_udp_types.h for now.


Near future features:

- As there are a number of bit-packed entries I may end up creating a "unpacked" variant of the packet structures, and wrapper functions to extract them.

Also, from what I can see there could probably be some improvements to reduce the amount of data sent in the telemetry and participant packets. It might make sense to have a "WorldData" type which sends things like track length, weather, name, variant, etc. As this stuff only changes intermittently and doesn't need to be sent in every sample.

Let me know if you have any issues or questions. You guys are free to use this as you wish, I don't care about getting credit or anything (as it is mostly SMS stuff I've just compiled together and formatted to my liking). If the mods have any issue with this you can delete this post, and I make no guarantees about future updates or support.

Cheers,
Trevor

TrevorP
23-12-2015, 17:03
Update: Added version 2, which includes a packet dispatcher for automatically identifying and verifying received UDP packets. Makes it simpler to handle packets.

TrevorP
23-12-2015, 18:41
Update 2: Added version 3, this introduces easy-to-use UDP stream open, receive, and close functions. These will handle all socket setup stuff that may be needed. Data can then be passed to the packet dispatcher if desired.

AtomicSphincter
24-12-2015, 05:28
Thanks for posting this info! It's way above my head right now, but it's something I can come back to once I'm somewhat familiar with it. I got so much to learn!