Add simple HTTP server
This commit is contained in:
parent
c8fb6b0780
commit
317fb65508
2
Makefile
2
Makefile
@ -1,2 +1,2 @@
|
||||
camera_stream: *.c *.h
|
||||
gcc -g -o camera_stream *.c
|
||||
gcc -g -lpthread -o camera_stream *.c
|
86
http.c
Normal file
86
http.c
Normal file
@ -0,0 +1,86 @@
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/ip.h>
|
||||
|
||||
#include "http.h"
|
||||
|
||||
#define BUFSIZE 256
|
||||
|
||||
int http_listen(int listen_port, int maxcons)
|
||||
{
|
||||
struct sockaddr_in server = {0};
|
||||
int listenfd = -1;
|
||||
|
||||
// getaddrinfo for host
|
||||
server.sin_family = AF_INET;
|
||||
server.sin_addr.s_addr = INADDR_ANY;
|
||||
server.sin_port = htons(listen_port);
|
||||
|
||||
listenfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (listenfd < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (bind(listenfd, (struct sockaddr *)&server, sizeof(server)) < 0) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (listen(listenfd, maxcons) < 0) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
return listenfd;
|
||||
error:
|
||||
if (listenfd >= 0)
|
||||
close(listenfd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
void http_process(int fd, struct sockaddr_in *addr)
|
||||
{
|
||||
FILE *stream = fdopen(fd, "r+");
|
||||
if (!stream) {
|
||||
return;
|
||||
}
|
||||
|
||||
char line[BUFSIZE];
|
||||
fgets(line, BUFSIZE, stream);
|
||||
|
||||
fprintf(stream, "HTTP/1.1 200 OK\n");
|
||||
fprintf(stream, "Content-Type: text/plain\n");
|
||||
fprintf(stream, "\r\n");
|
||||
fprintf(stream, "Text.\n");
|
||||
fflush(stream);
|
||||
fclose(stream);
|
||||
}
|
||||
|
||||
int http_worker(int listenfd)
|
||||
{
|
||||
while (1) {
|
||||
struct sockaddr_in addr;
|
||||
int addrlen = sizeof(addr);
|
||||
int ret = accept(listenfd, (struct sockaddr *)&addr, &addrlen);
|
||||
if (ret < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
http_process(ret, &addr);
|
||||
close(ret);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int http_worker_threads(int listenfd, int nthreads)
|
||||
{
|
||||
while (nthreads-- > 0) {
|
||||
pthread_t thread;
|
||||
pthread_create(&thread, NULL, (void *(*)(void*))http_worker, (void*)listenfd);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
7
http.h
Normal file
7
http.h
Normal file
@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "v4l2.h"
|
||||
|
||||
int http_listen(int listen_port, int maxcons);
|
||||
int http_worker(int listenfd);
|
||||
int http_worker_threads(int listenfd, int nthreads);
|
6
main.c
6
main.c
@ -3,6 +3,7 @@
|
||||
#include "device.h"
|
||||
#include "links.h"
|
||||
#include "v4l2.h"
|
||||
#include "http.h"
|
||||
|
||||
int camera_width = 1920;
|
||||
int camera_height = 1080;
|
||||
@ -103,6 +104,11 @@ int main(int argc, char *argv[])
|
||||
// return -1;
|
||||
// }
|
||||
|
||||
int httpfd = http_listen(9090, 5);
|
||||
if (httpfd >= 0) {
|
||||
http_worker_threads(httpfd, 8);
|
||||
}
|
||||
|
||||
bool running = false;
|
||||
links_loop(links, &running);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user