Ported pixelflut to c

This commit is contained in:
Andreas Völker 2018-12-31 13:47:21 +01:00
parent e270cc32d0
commit 9c0995a8b2
2 changed files with 27 additions and 22 deletions

View File

@ -13,6 +13,6 @@ target_link_libraries(fbcp m)
add_executable(flicker flicker.c) add_executable(flicker flicker.c)
add_executable(pixelflut pixelflut.cpp) add_executable(pixelflut pixelflut.c)
find_package (Threads) find_package (Threads)
target_link_libraries (pixelflut ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (pixelflut ${CMAKE_THREAD_LIBS_INIT})

View File

@ -1,16 +1,19 @@
#include <string.h> #include <string.h>
#include <ctype.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <unistd.h> #include <unistd.h>
#include <assert.h> #include <assert.h>
#include <thread> #include <ctype.h>
#include <stdio.h>
#include <threads.h>
#include <stdlib.h>
int Nx, Ny; int Nx, Ny;
int port;
uint8_t *buf; uint8_t *buf;
void SetPixel(int x, int y, uint8_t r, uint8_t g, uint8_t b){ void SetPixelRGB(int x, int y, uint8_t r, uint8_t g, uint8_t b){
if (x < 0 || x >= Nx || y < 0 || y >= Ny) return; if (x < 0 || x >= Nx || y < 0 || y >= Ny) return;
buf[3*(x+Nx*y)+0] = r; buf[3*(x+Nx*y)+0] = r;
buf[3*(x+Nx*y)+1] = g; buf[3*(x+Nx*y)+1] = g;
@ -18,7 +21,7 @@ void SetPixel(int x, int y, uint8_t r, uint8_t g, uint8_t b){
} }
void SetPixel(int x, int y, uint32_t c){ void SetPixel(int x, int y, uint32_t c){
SetPixel(x, y, (c >> 16)&0xff, (c >> 8)&0xff, c&0xff); SetPixelRGB(x, y, (c >> 16)&0xff, (c >> 8)&0xff, c&0xff);
} }
uint32_t GetPixel(int x, int y){ uint32_t GetPixel(int x, int y){
@ -33,7 +36,7 @@ uint32_t GetPixel(int x, int y){
static const int PFT_CONNECTION_TIMEOUT = 1; static const int PFT_CONNECTION_TIMEOUT = 1;
volatile int PFT_ClientThreadCount = 0; volatile int PFT_ClientThreadCount = 0;
bool PFT_ReadIdent(char** begin, char* end, char* out, int size){ int PFT_ReadIdent(char** begin, char* end, char* out, int size){
int i = 0; int i = 0;
while (*begin != end && isalpha(**begin) && i < size-1){ while (*begin != end && isalpha(**begin) && i < size-1){
out[i++] = **begin; out[i++] = **begin;
@ -43,18 +46,18 @@ bool PFT_ReadIdent(char** begin, char* end, char* out, int size){
return i != 0; return i != 0;
} }
bool PRF_ReadInt(char** begin, char* end, int *out){ int PRF_ReadInt(char** begin, char* end, int *out){
if (*begin == end || !isdigit(**begin)) if (*begin == end || !isdigit(**begin))
return false; return 0;
*out = 0; *out = 0;
while(*begin != end && isdigit(**begin)){ while(*begin != end && isdigit(**begin)){
*out = (*out)*10+(**begin-'0'); *out = (*out)*10+(**begin-'0');
(*begin)++; (*begin)++;
} }
return true; return 1;
} }
bool PFT_IsHex(char c){ int PFT_IsHex(char c){
return isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); return isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
} }
@ -65,19 +68,18 @@ int PFT_HexCharToInt(char c){
return c-'a'+10; return c-'a'+10;
if (c >= 'A' && c <= 'F') if (c >= 'A' && c <= 'F')
return c-'A'+10; return c-'A'+10;
assert(false);
return 0; return 0;
} }
bool PFT_ReadColorHex(char** begin, char* end, uint32_t *out){ int PFT_ReadColorHex(char** begin, char* end, uint32_t *out){
char* oldbegin = *begin; char* oldbegin = *begin;
*out = 0; *out = 0;
if (end-*begin < 6) if (end-*begin < 6)
return false; return 0;
for(int i = 0; i != 6; i++){ for(int i = 0; i != 6; i++){
if (!PFT_IsHex(**begin)){ if (!PFT_IsHex(**begin)){
*begin = oldbegin; *begin = oldbegin;
return false; return 0;
} }
*out = (*out)*16+PFT_HexCharToInt(**begin); *out = (*out)*16+PFT_HexCharToInt(**begin);
(*begin)++; (*begin)++;
@ -90,7 +92,7 @@ bool PFT_ReadColorHex(char** begin, char* end, uint32_t *out){
}else{ }else{
*out *= 256; *out *= 256;
} }
return true; return 1;
} }
void PFT_SkipSpace(char **begin, char *end){ void PFT_SkipSpace(char **begin, char *end){
@ -154,7 +156,8 @@ int PFT_HandlePixelflutMessage(uint8_t *buffer, int size, int sock){
return begin-(char*)buffer; return begin-(char*)buffer;
} }
void * PFT_HandleClient(int sock){ int PFT_HandleClient(void* sock_){
int sock = (int)(size_t)sock_;
PFT_ClientThreadCount++; PFT_ClientThreadCount++;
uint8_t buf[1024]; uint8_t buf[1024];
uint8_t offset = 0; uint8_t offset = 0;
@ -169,7 +172,7 @@ void * PFT_HandleClient(int sock){
} }
void * PFT_RunServer(unsigned long port){ int PFT_RunServer(void* _){
int client_sock; int client_sock;
socklen_t addr_len; socklen_t addr_len;
struct sockaddr_in addr; struct sockaddr_in addr;
@ -214,11 +217,12 @@ void * PFT_RunServer(unsigned long port){
setsockopt(server_sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv,sizeof(struct timeval)); setsockopt(server_sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv,sizeof(struct timeval));
setsockopt(server_sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval)); setsockopt(server_sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval));
while(true){ while(1){
client_sock = accept(server_sock, (struct sockaddr*)&addr, &addr_len); client_sock = accept(server_sock, (struct sockaddr*)&addr, &addr_len);
if(client_sock > 0){ if(client_sock > 0){
std::thread client_thread(PFT_HandleClient, client_sock); thrd_t thread;
client_thread.detach(); thrd_create(&thread, PFT_HandleClient, (void*)(size_t)client_sock);
thrd_detach(thread);
} }
} }
close(server_sock); close(server_sock);
@ -229,10 +233,11 @@ int main(int argc, char **argv) {
Nx = atoi(argv[1]); Nx = atoi(argv[1]);
Ny = atoi(argv[2]); Ny = atoi(argv[2]);
buf = (uint8_t*)malloc(Nx*Ny*3); buf = (uint8_t*)malloc(Nx*Ny*3);
int port = atoi(argv[3]); port = atoi(argv[3]);
if (port == 0) port = 4444; if (port == 0) port = 4444;
thrd_t thread;
thrd_create(&thread, PFT_RunServer, NULL);
std::thread pixelflut_tcp_thread(&PFT_RunServer,(unsigned long)port);
while(1) { while(1) {