mirror of
https://github.com/openshwprojects/OpenBK7231T_App.git
synced 2026-02-11 00:45:45 +00:00
add stubs for compiling on windows (placeholders)
This commit is contained in:
169
src/httpserver/http_tcp_server_nonblocking.c
Normal file
169
src/httpserver/http_tcp_server_nonblocking.c
Normal file
@ -0,0 +1,169 @@
|
||||
#ifdef WINDOWS
|
||||
|
||||
#include "../new_common.h"
|
||||
#include "lwip/sockets.h"
|
||||
#include "lwip/ip_addr.h"
|
||||
#include "lwip/inet.h"
|
||||
#include "../logging/logging.h"
|
||||
#include "new_http.h"
|
||||
|
||||
SOCKET ListenSocket = INVALID_SOCKET;
|
||||
|
||||
#define DEFAULT_PORT "80"
|
||||
|
||||
int HTTPServer_Start() {
|
||||
|
||||
int iResult;
|
||||
int argp;
|
||||
struct addrinfo *result = NULL;
|
||||
struct addrinfo hints;
|
||||
|
||||
ZeroMemory(&hints, sizeof(hints));
|
||||
hints.ai_family = AF_INET;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_protocol = IPPROTO_TCP;
|
||||
hints.ai_flags = AI_PASSIVE;
|
||||
|
||||
// Resolve the server address and port
|
||||
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
|
||||
if ( iResult != 0 ) {
|
||||
printf("getaddrinfo failed with error: %d\n", iResult);
|
||||
WSACleanup();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Create a SOCKET for connecting to server
|
||||
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
|
||||
if (ListenSocket == INVALID_SOCKET) {
|
||||
printf("socket failed with error: %ld\n", WSAGetLastError());
|
||||
freeaddrinfo(result);
|
||||
WSACleanup();
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Setup the TCP listening socket
|
||||
iResult = bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen);
|
||||
if (iResult == SOCKET_ERROR) {
|
||||
printf("bind failed with error: %d\n", WSAGetLastError());
|
||||
freeaddrinfo(result);
|
||||
closesocket(ListenSocket);
|
||||
WSACleanup();
|
||||
return 1;
|
||||
}
|
||||
|
||||
freeaddrinfo(result);
|
||||
|
||||
iResult = listen(ListenSocket, SOMAXCONN);
|
||||
if (iResult == SOCKET_ERROR) {
|
||||
printf("listen failed with error: %d\n", WSAGetLastError());
|
||||
closesocket(ListenSocket);
|
||||
WSACleanup();
|
||||
return 1;
|
||||
}
|
||||
|
||||
argp = 1;
|
||||
if (ioctlsocket(ListenSocket,
|
||||
FIONBIO,
|
||||
&argp) == SOCKET_ERROR)
|
||||
{
|
||||
printf("ioctlsocket() error %d\n", WSAGetLastError());
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
#define DEFAULT_BUFLEN 10000
|
||||
void HTTPServer_RunQuickTick() {
|
||||
int iResult;
|
||||
int err;
|
||||
char recvbuf[DEFAULT_BUFLEN];
|
||||
char outbuf[DEFAULT_BUFLEN];
|
||||
int recvbuflen = DEFAULT_BUFLEN;
|
||||
SOCKET ClientSocket = INVALID_SOCKET;
|
||||
int len, iSendResult;
|
||||
int argp;
|
||||
|
||||
// Accept a client socket
|
||||
ClientSocket = accept(ListenSocket, NULL, NULL);
|
||||
if (ClientSocket == INVALID_SOCKET) {
|
||||
iResult = WSAGetLastError();
|
||||
if(iResult != WSAEWOULDBLOCK) {
|
||||
printf("accept failed with error: %d\n", iResult);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
//argp = 0;
|
||||
//if (ioctlsocket(ClientSocket,
|
||||
// FIONBIO,
|
||||
// &argp) == SOCKET_ERROR)
|
||||
//{
|
||||
// printf("ioctlsocket() error %d\n", WSAGetLastError());
|
||||
// return 1;
|
||||
//}
|
||||
// Receive until the peer shuts down the connection
|
||||
do {
|
||||
|
||||
iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
|
||||
if (iResult > 0) {
|
||||
http_request_t request;
|
||||
memset(&request, 0, sizeof(request));
|
||||
|
||||
recvbuf[iResult] = 0;
|
||||
|
||||
request.fd = ClientSocket;
|
||||
request.received = recvbuf;
|
||||
request.receivedLen = iResult;
|
||||
outbuf[0] = '\0';
|
||||
request.reply = outbuf;
|
||||
request.replylen = 0;
|
||||
|
||||
request.replymaxlen = DEFAULT_BUFLEN;
|
||||
|
||||
printf("Bytes received: %d \n", iResult);
|
||||
len = HTTP_ProcessPacket(&request);
|
||||
|
||||
if(len > 0) {
|
||||
printf("Bytes rremaining tosend %d\n", len);
|
||||
//printf("%s\n",outbuf);
|
||||
// Echo the buffer back to the sender
|
||||
//leen = strlen(request.reply);
|
||||
iSendResult = send( ClientSocket, outbuf,len, 0 );
|
||||
if (iSendResult == SOCKET_ERROR) {
|
||||
printf("send failed with error: %d\n", WSAGetLastError());
|
||||
closesocket(ClientSocket);
|
||||
return 1;
|
||||
}
|
||||
printf("Bytes sent: %d\n", iSendResult);
|
||||
}
|
||||
break;
|
||||
}
|
||||
else if (iResult == 0) {
|
||||
printf("Connection closing...\n");
|
||||
break;
|
||||
}
|
||||
else {
|
||||
err = WSAGetLastError();
|
||||
if (err != WSAEWOULDBLOCK) {
|
||||
printf("recv failed with error: %d\n", err);
|
||||
closesocket(ClientSocket);
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
} while (1);
|
||||
//byte zero = 0;
|
||||
//send(ClientSocket, &zero, 0, 0);
|
||||
//Sleep(50);
|
||||
// shutdown the connection since we're done
|
||||
iResult = shutdown(ClientSocket, SD_SEND);
|
||||
//Sleep(50);
|
||||
//iResult = closesocket(ClientSocket);
|
||||
if (iResult == SOCKET_ERROR) {
|
||||
printf("shutdown failed with error: %d\n", WSAGetLastError());
|
||||
closesocket(ClientSocket);
|
||||
//WSACleanup();
|
||||
//return 1;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user