#include #include #include #include #include #include #include #include #include #include "screenbuffer.h" #define UDP_PORT 5005 // the port users will be connecting to #define DISCOVER_PORT 5006 // the port on which the server will broadcast its details #define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)) #define MAX_PIXELS 140 static void die(char *msg) { fprintf(stderr, "%s\n", msg); exit(EXIT_FAILURE); } static int get_udp_socket(int port) { int sock_fd; struct sockaddr_in6 addr; memset(&addr, 0, sizeof(addr)); if ( ( sock_fd = socket(PF_INET6, SOCK_DGRAM,0) ) < 0 ) die("could not create socket"); addr.sin6_port = htons(port); addr.sin6_family = AF_INET6; addr.sin6_addr = in6addr_any; if ( bind(sock_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0 ) die("could not bind to UDP"); return sock_fd; } static ssize_t get_udp_packet(int sock_fd, char *packet, size_t size) { ssize_t n_read; while ((n_read = recv(sock_fd, packet, size, 0)) < 0 ); return n_read; } // PixelVloed C version // application entry point int main(int argc, char* argv[]) { uint8_t packet[ (8 * MAX_PIXELS) + 32]; // 8 bytes per pixel is the maximum size ssize_t packet_size; uint16_t x, y; uint8_t r, g, b, a; uint8_t pixellength = 0, protocol, version; uint16_t i; uint8_t offset = 2; int sock_fd = get_udp_socket(UDP_PORT); // Init screen buffer if (!init_frame_buffer()) { die("Failed to init screen buffer.\n"); } // draw each udp packet while( (packet_size = get_udp_packet(sock_fd, (char*)&packet, sizeof(packet))) >= 0 ) { // Get protocol from the packet version = packet[1]; // the version protocol = packet[0]; // protocol switch //printf("V:%d, P:%d\n", version, protocol); switch (protocol) { // Protocol 0: xyrgb 16:16:8:8:8 specified for each pixel case 0: pixellength = 7; packet_size = MIN(offset + (pixellength * MAX_PIXELS), packet_size); for (i=offset; i