1
0
Fork 0
mirror of https://github.com/warmcat/libwebsockets.git synced 2025-03-16 00:00:07 +01:00

use lws_plat_file_* instead of compatible_file_*

fix linux and mbed
This commit is contained in:
SoapyMan 2015-12-10 09:08:52 +06:00
parent 419548ba73
commit 436ecb8bec
8 changed files with 147 additions and 132 deletions

View file

@ -21,6 +21,39 @@
#include "private-libwebsockets.h"
/**
* initializes file callbacks for context
*/
LWS_VISIBLE void
lws_context_init_file_callbacks(struct lws_context_creation_info *info,
struct libwebsocket_context *context)
{
struct libwebsocket_file_callbacks* cb;
cb = info->file_callbacks;
if(cb && cb->pfn_open)
context->file_callbacks.pfn_open = cb->pfn_open;
else
context->file_callbacks.pfn_open = lws_plat_file_open;
if(cb && cb->pfn_close)
context->file_callbacks.pfn_close = cb->pfn_close;
else
context->file_callbacks.pfn_close = lws_plat_file_close;
if(cb && cb->pfn_seek_cur)
context->file_callbacks.pfn_seek_cur = cb->pfn_seek_cur;
else
context->file_callbacks.pfn_seek_cur = lws_plat_file_seek_cur;
if(cb && cb->pfn_read)
context->file_callbacks.pfn_read = cb->pfn_read;
else
context->file_callbacks.pfn_read = lws_plat_file_read;
}
#ifndef LWS_BUILD_HASH
#define LWS_BUILD_HASH "unknown-build-hash"
#endif

View file

@ -1,118 +0,0 @@
/*
* libwebsockets - small server side websockets and web server implementation
*
* Copyright (C) 2010-2015 Andy Green <andy@warmcat.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation:
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#include "private-libwebsockets.h"
#include "libwebsockets.h"
#if defined(WIN32) || defined(_WIN32)
HANDLE compatible_file_open(const char* filename, unsigned long* filelen){
HANDLE ret;
WCHAR buffer[MAX_PATH];
MultiByteToWideChar(CP_UTF8, 0, filename, -1, buffer,
sizeof(buffer) / sizeof(buffer[0]));
ret = CreateFileW(buffer, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (ret != LWS_INVALID_FILE)
*filelen = GetFileSize(ret, NULL);
return ret;
}
void compatible_file_close(void*fd){
CloseHandle((HANDLE)fd);
}
unsigned long compatible_file_seek_cur(void* fd, long offset){
return SetFilePointer((HANDLE)fd, offset, NULL, FILE_CURRENT);
}
void compatible_file_read(unsigned long* amount, void* fd, unsigned char* buf, unsigned long len){
DWORD _amount;
if (!ReadFile((HANDLE)fd, buf, (DWORD)len, &_amount, NULL))
*amount = -1;
else
*amount = (unsigned long)_amount;
}
#else /* not windows --> */
int compatible_file_open(const char* filename, unsigned long* filelen){
struct stat stat_buf;
int ret = open(filename, O_RDONLY);
if (ret < 0)
return LWS_INVALID_FILE;
if (fstat(ret, &stat_buf) < 0) {
close(ret);
return LWS_INVALID_FILE;
}
*filelen = stat_buf.st_size;
return ret;
}
void compatible_file_close(void*fd){
close((int)fd)
}
unsigned long compatible_file_seek_cur(void* fd, long offset){
return lseek((int)fd, offset, SEEK_CUR);
}
void compatible_file_read(unsigned long amount, void* fd, unsigned char* buf, unsigned long* len){
*amount = read((int)fd, buf, len);
}
#endif // WIN32 || _WIN32
LWS_VISIBLE void
lws_context_init_file_callbacks(struct lws_context_creation_info *info,
struct libwebsocket_context *context)
{
struct libwebsocket_file_callbacks* cb;
cb = info->file_callbacks;
if(cb && cb->pfn_open)
context->file_callbacks.pfn_open = cb->pfn_open;
else
context->file_callbacks.pfn_open = compatible_file_open;
if(cb && cb->pfn_close)
context->file_callbacks.pfn_close = cb->pfn_close;
else
context->file_callbacks.pfn_close = compatible_file_close;
if(cb && cb->pfn_seek_cur)
context->file_callbacks.pfn_seek_cur = cb->pfn_seek_cur;
else
context->file_callbacks.pfn_seek_cur = compatible_file_seek_cur;
if(cb && cb->pfn_read)
context->file_callbacks.pfn_read = cb->pfn_read;
else
context->file_callbacks.pfn_read = compatible_file_read;
}

View file

@ -73,7 +73,7 @@ lws_close_and_free_session(struct lws_context *context,
if (wsi->mode == LWS_CONNMODE_HTTP_SERVING_ACCEPTED &&
wsi->u.http.fd != LWS_INVALID_FILE) {
lwsl_debug("closing http file\n");
compatible_file_close(wsi->u.http.fd);
context->file_callbacks.pfn_close(wsi->u.http.fd);
wsi->u.http.fd = LWS_INVALID_FILE;
context->protocols[0].callback(context, wsi,
LWS_CALLBACK_CLOSED_HTTP, wsi->user_space, NULL, 0);

View file

@ -145,13 +145,6 @@ lws_plat_service_periodic(struct lws_context *context)
(void)context;
}
LWS_VISIBLE int
lws_plat_open_file(const char* filename, unsigned long* filelen)
{
(void)filename;
(void)filelen;
return LWS_INVALID_FILE;
}
LWS_VISIBLE const char *
lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
@ -179,4 +172,39 @@ delete_from_fd(struct lws_context *context, lws_sockfd_type fd)
(void)fd;
return 1;
}
/*
* Default file callbacks
*/
LWS_VISIBLE int
lws_plat_file_open(const char* filename, unsigned long* filelen)
{
(void)filename;
(void)filelen;
return LWS_INVALID_FILE;
}
LWS_VISIBLE void
lws_plat_file_close(void*fd)
{
(void)fd;
}
LWS_VISIBLE unsigned long
lws_plat_file_seek_cur(void* fd, long offset)
{
(void)fd;
(void)offset;
return 0;
}
LWS_VISIBLE void
lws_plat_file_read(unsigned long* amount, void* fd, unsigned char* buf, unsigned long len)
{
(void)amount;
(void)fd;
(void)buf;
(void)len;
}

View file

@ -451,8 +451,18 @@ lws_plat_change_pollfd(struct lws_context *context,
return 0;
}
LWS_VISIBLE const char *
lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
{
return inet_ntop(af, src, dst, cnt);
}
/*
* Default file callbacks
*/
LWS_VISIBLE int
lws_plat_open_file(const char* filename, unsigned long* filelen)
lws_plat_file_open(const char* filename, unsigned long* filelen)
{
struct stat stat_buf;
int ret = open(filename, O_RDONLY);
@ -468,8 +478,20 @@ lws_plat_open_file(const char* filename, unsigned long* filelen)
return ret;
}
LWS_VISIBLE const char *
lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
LWS_VISIBLE void
lws_plat_file_close(void*fd)
{
return inet_ntop(af, src, dst, cnt);
close((int)fd)
}
LWS_VISIBLE unsigned long
lws_plat_file_seek_cur(void* fd, long offset)
{
return lseek((int)fd, offset, SEEK_CUR);
}
LWS_VISIBLE void
lws_plat_file_read(unsigned long* amount, void* fd, unsigned char* buf, unsigned long len)
{
*amount = read((int)fd, buf, len);
}

View file

@ -443,3 +443,46 @@ lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
lws_free(buffer);
return ok ? dst : NULL;
}
/*
* Default file callbacks
*/
LWS_VISIBLE HANDLE
lws_plat_file_open(const char* filename, unsigned long* filelen)
{
HANDLE ret;
WCHAR buffer[MAX_PATH];
MultiByteToWideChar(CP_UTF8, 0, filename, -1, buffer,
sizeof(buffer) / sizeof(buffer[0]));
ret = CreateFileW(buffer, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (ret != LWS_INVALID_FILE)
*filelen = GetFileSize(ret, NULL);
return ret;
}
LWS_VISIBLE void
lws_plat_file_close(void*fd)
{
CloseHandle((HANDLE)fd);
}
LWS_VISIBLE unsigned long
lws_plat_file_seek_cur(void* fd, long offset)
{
return SetFilePointer((HANDLE)fd, offset, NULL, FILE_CURRENT);
}
LWS_VISIBLE void
lws_plat_file_read(unsigned long* amount, void* fd, unsigned char* buf, unsigned long len)
{
DWORD _amount;
if (!ReadFile((HANDLE)fd, buf, (DWORD)len, &_amount, NULL))
*amount = -1;
else
*amount = (unsigned long)_amount;
}

View file

@ -1287,6 +1287,15 @@ time_in_microseconds(void);
LWS_EXTERN const char *
lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt);
LWS_EXTERN HANDLE
lws_plat_file_open(const char* filename, unsigned long* filelen);
LWS_EXTERN void
lws_plat_file_close(void*fd);
LWS_EXTERN unsigned long
lws_plat_file_seek_cur(void* fd, long offset);
LWS_EXTERN void
lws_plat_file_read(unsigned long* amount, void* fd, unsigned char* buf, unsigned long len);
#ifdef __cplusplus
};
#endif

View file

@ -296,8 +296,6 @@ int main(int argc, char **argv)
* which also includes libwebsocket sockets
*/
pollfds->events = (0x0100 | 0x0200);
n = poll(pollfds, count_pollfds, 50);
if (n < 0)
continue;