2014-04-02 21:02:54 +08:00
|
|
|
#include "private-libwebsockets.h"
|
|
|
|
|
|
|
|
unsigned long long
|
2014-03-31 11:01:32 +08:00
|
|
|
time_in_microseconds()
|
|
|
|
{
|
|
|
|
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
|
|
|
|
FILETIME filetime;
|
|
|
|
ULARGE_INTEGER datetime;
|
|
|
|
|
|
|
|
#ifdef _WIN32_WCE
|
|
|
|
GetCurrentFT(&filetime);
|
|
|
|
#else
|
|
|
|
GetSystemTimeAsFileTime(&filetime);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* As per Windows documentation for FILETIME, copy the resulting FILETIME structure to a
|
|
|
|
* ULARGE_INTEGER structure using memcpy (using memcpy instead of direct assignment can
|
|
|
|
* prevent alignment faults on 64-bit Windows).
|
|
|
|
*/
|
|
|
|
memcpy(&datetime, &filetime, sizeof(datetime));
|
|
|
|
|
|
|
|
/* Windows file times are in 100s of nanoseconds. */
|
|
|
|
return (datetime.QuadPart - DELTA_EPOCH_IN_MICROSECS) / 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32_WCE
|
2014-04-02 21:02:54 +08:00
|
|
|
time_t time(time_t *t)
|
2014-03-31 11:01:32 +08:00
|
|
|
{
|
|
|
|
time_t ret = time_in_microseconds() / 1000000;
|
|
|
|
*t = ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-01-30 10:13:01 +08:00
|
|
|
/* file descriptor hash management */
|
|
|
|
|
2015-12-04 11:08:32 +08:00
|
|
|
struct lws *
|
2015-12-14 11:34:00 +08:00
|
|
|
wsi_from_fd(const struct lws_context *context, lws_sockfd_type fd)
|
2015-01-30 10:13:01 +08:00
|
|
|
{
|
|
|
|
int h = LWS_FD_HASH(fd);
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
for (n = 0; n < context->fd_hashtable[h].length; n++)
|
|
|
|
if (context->fd_hashtable[h].wsi[n]->sock == fd)
|
|
|
|
return context->fd_hashtable[h].wsi[n];
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-12-04 11:08:32 +08:00
|
|
|
insert_wsi(struct lws_context *context, struct lws *wsi)
|
2015-01-30 10:13:01 +08:00
|
|
|
{
|
|
|
|
int h = LWS_FD_HASH(wsi->sock);
|
|
|
|
|
|
|
|
if (context->fd_hashtable[h].length == (getdtablesize() - 1)) {
|
|
|
|
lwsl_err("hash table overflow\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
context->fd_hashtable[h].wsi[context->fd_hashtable[h].length++] = wsi;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-12-06 08:00:03 +08:00
|
|
|
delete_from_fd(struct lws_context *context, lws_sockfd_type fd)
|
2015-01-30 10:13:01 +08:00
|
|
|
{
|
|
|
|
int h = LWS_FD_HASH(fd);
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
for (n = 0; n < context->fd_hashtable[h].length; n++)
|
|
|
|
if (context->fd_hashtable[h].wsi[n]->sock == fd) {
|
|
|
|
while (n < context->fd_hashtable[h].length) {
|
|
|
|
context->fd_hashtable[h].wsi[n] =
|
|
|
|
context->fd_hashtable[h].wsi[n + 1];
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
context->fd_hashtable[h].length--;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
lwsl_err("Failed to find fd %d requested for "
|
|
|
|
"delete in hashtable\n", fd);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-12-04 11:08:32 +08:00
|
|
|
LWS_VISIBLE int lws_get_random(struct lws_context *context,
|
2014-03-31 11:01:32 +08:00
|
|
|
void *buf, int len)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
char *p = (char *)buf;
|
|
|
|
|
|
|
|
for (n = 0; n < len; n++)
|
|
|
|
p[n] = (unsigned char)rand();
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2015-12-04 11:08:32 +08:00
|
|
|
LWS_VISIBLE int lws_send_pipe_choked(struct lws *wsi)
|
2014-03-31 11:01:32 +08:00
|
|
|
{
|
|
|
|
return wsi->sock_send_blocking;
|
|
|
|
}
|
|
|
|
|
2015-12-04 11:08:32 +08:00
|
|
|
LWS_VISIBLE int lws_poll_listen_fd(struct lws_pollfd *fd)
|
2014-03-31 11:01:32 +08:00
|
|
|
{
|
|
|
|
fd_set readfds;
|
|
|
|
struct timeval tv = { 0, 0 };
|
|
|
|
|
|
|
|
assert(fd->events == LWS_POLLIN);
|
|
|
|
|
|
|
|
FD_ZERO(&readfds);
|
|
|
|
FD_SET(fd->fd, &readfds);
|
|
|
|
|
|
|
|
return select(fd->fd + 1, &readfds, NULL, NULL, &tv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-12-04 08:43:54 +08:00
|
|
|
* lws_cancel_service() - Cancel servicing of pending websocket activity
|
2014-03-31 11:01:32 +08:00
|
|
|
* @context: Websocket context
|
|
|
|
*
|
2015-12-04 08:43:54 +08:00
|
|
|
* This function let a call to lws_service() waiting for a timeout
|
2014-03-31 11:01:32 +08:00
|
|
|
* immediately return.
|
|
|
|
*/
|
|
|
|
LWS_VISIBLE void
|
2015-12-04 11:08:32 +08:00
|
|
|
lws_cancel_service(struct lws_context *context)
|
2014-03-31 11:01:32 +08:00
|
|
|
{
|
|
|
|
WSASetEvent(context->events[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
|
|
|
|
{
|
|
|
|
lwsl_emit_stderr(level, line);
|
|
|
|
}
|
2014-04-02 14:25:10 +08:00
|
|
|
|
|
|
|
LWS_VISIBLE int
|
2015-12-04 11:08:32 +08:00
|
|
|
lws_plat_service(struct lws_context *context, int timeout_ms)
|
2014-04-02 14:25:10 +08:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
int i;
|
|
|
|
DWORD ev;
|
|
|
|
WSANETWORKEVENTS networkevents;
|
2015-12-04 11:08:32 +08:00
|
|
|
struct lws_pollfd *pfd;
|
|
|
|
struct lws *wsi;
|
2014-04-02 14:25:10 +08:00
|
|
|
|
|
|
|
/* stay dead once we are dead */
|
|
|
|
|
|
|
|
if (context == NULL)
|
|
|
|
return 1;
|
|
|
|
|
2015-12-17 07:54:44 +08:00
|
|
|
context->service_tid = context->protocols[0].callback(NULL,
|
2014-04-02 14:25:10 +08:00
|
|
|
LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
|
|
|
|
|
|
|
|
for (i = 0; i < context->fds_count; ++i) {
|
|
|
|
pfd = &context->fds[i];
|
2015-12-17 17:03:59 +08:00
|
|
|
if (pfd->fd == context->lserv_fd)
|
2014-04-02 14:25:10 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (pfd->events & LWS_POLLOUT) {
|
2015-11-13 10:14:50 +08:00
|
|
|
wsi = wsi_from_fd(context, pfd->fd);
|
|
|
|
if (!wsi || wsi->sock_send_blocking)
|
2014-04-02 14:25:10 +08:00
|
|
|
continue;
|
|
|
|
pfd->revents = LWS_POLLOUT;
|
2015-12-04 08:43:54 +08:00
|
|
|
n = lws_service_fd(context, pfd);
|
2014-04-02 14:25:10 +08:00
|
|
|
if (n < 0)
|
2015-12-05 09:38:50 +08:00
|
|
|
return -1;
|
|
|
|
/* if something closed, retry this slot */
|
|
|
|
if (n)
|
|
|
|
i--;
|
2014-04-02 14:25:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ev = WSAWaitForMultipleEvents(context->fds_count + 1,
|
|
|
|
context->events, FALSE, timeout_ms, FALSE);
|
|
|
|
context->service_tid = 0;
|
|
|
|
|
|
|
|
if (ev == WSA_WAIT_TIMEOUT) {
|
2015-12-04 08:43:54 +08:00
|
|
|
lws_service_fd(context, NULL);
|
2014-04-02 14:25:10 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ev == WSA_WAIT_EVENT_0) {
|
|
|
|
WSAResetEvent(context->events[0]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ev < WSA_WAIT_EVENT_0 || ev > WSA_WAIT_EVENT_0 + context->fds_count)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
pfd = &context->fds[ev - WSA_WAIT_EVENT_0 - 1];
|
|
|
|
|
|
|
|
if (WSAEnumNetworkEvents(pfd->fd,
|
|
|
|
context->events[ev - WSA_WAIT_EVENT_0],
|
|
|
|
&networkevents) == SOCKET_ERROR) {
|
|
|
|
lwsl_err("WSAEnumNetworkEvents() failed with error %d\n",
|
|
|
|
LWS_ERRNO);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-12-06 08:00:03 +08:00
|
|
|
pfd->revents = (short)networkevents.lNetworkEvents;
|
2014-04-02 14:25:10 +08:00
|
|
|
|
2015-11-13 11:43:53 +08:00
|
|
|
if (pfd->revents & LWS_POLLOUT) {
|
|
|
|
wsi = wsi_from_fd(context, pfd->fd);
|
2015-11-14 07:02:38 +08:00
|
|
|
if (wsi)
|
|
|
|
wsi->sock_send_blocking = FALSE;
|
2015-11-13 11:43:53 +08:00
|
|
|
}
|
2014-04-02 14:25:10 +08:00
|
|
|
|
2015-12-04 08:43:54 +08:00
|
|
|
return lws_service_fd(context, pfd);
|
2014-04-02 14:25:10 +08:00
|
|
|
}
|
|
|
|
|
2014-04-02 21:02:54 +08:00
|
|
|
LWS_VISIBLE int
|
2015-12-06 08:00:03 +08:00
|
|
|
lws_plat_set_socket_options(struct lws_context *context, lws_sockfd_type fd)
|
2014-04-02 14:25:10 +08:00
|
|
|
{
|
|
|
|
int optval = 1;
|
2014-04-15 18:41:38 +02:00
|
|
|
int optlen = sizeof(optval);
|
2014-04-02 14:25:10 +08:00
|
|
|
u_long optl = 1;
|
|
|
|
DWORD dwBytesRet;
|
|
|
|
struct tcp_keepalive alive;
|
2014-04-03 22:38:49 +08:00
|
|
|
struct protoent *tcp_proto;
|
2015-12-14 08:52:03 +08:00
|
|
|
|
2014-04-02 14:25:10 +08:00
|
|
|
if (context->ka_time) {
|
|
|
|
/* enable keepalive on this socket */
|
|
|
|
optval = 1;
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
|
2014-04-15 18:41:38 +02:00
|
|
|
(const char *)&optval, optlen) < 0)
|
2014-04-02 14:25:10 +08:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
alive.onoff = TRUE;
|
|
|
|
alive.keepalivetime = context->ka_time;
|
|
|
|
alive.keepaliveinterval = context->ka_interval;
|
|
|
|
|
2015-12-14 08:52:03 +08:00
|
|
|
if (WSAIoctl(fd, SIO_KEEPALIVE_VALS, &alive, sizeof(alive),
|
2014-04-02 14:25:10 +08:00
|
|
|
NULL, 0, &dwBytesRet, NULL, NULL))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Disable Nagle */
|
|
|
|
optval = 1;
|
|
|
|
tcp_proto = getprotobyname("TCP");
|
2015-04-16 04:24:38 +08:00
|
|
|
if (!tcp_proto) {
|
|
|
|
lwsl_err("getprotobyname() failed with error %d\n", LWS_ERRNO);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-04-15 18:41:38 +02:00
|
|
|
setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, (const char *)&optval, optlen);
|
2014-04-02 14:25:10 +08:00
|
|
|
|
|
|
|
/* We are nonblocking... */
|
|
|
|
ioctlsocket(fd, FIONBIO, &optl);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-02 21:02:54 +08:00
|
|
|
LWS_VISIBLE void
|
|
|
|
lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
|
2014-04-02 14:25:10 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-02 21:02:54 +08:00
|
|
|
LWS_VISIBLE int
|
|
|
|
lws_plat_context_early_init(void)
|
2014-04-02 14:25:10 +08:00
|
|
|
{
|
|
|
|
WORD wVersionRequested;
|
|
|
|
WSADATA wsaData;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
/* Use the MAKEWORD(lowbyte, highbyte) macro from Windef.h */
|
|
|
|
wVersionRequested = MAKEWORD(2, 2);
|
|
|
|
|
|
|
|
err = WSAStartup(wVersionRequested, &wsaData);
|
|
|
|
if (!err)
|
|
|
|
return 0;
|
|
|
|
/*
|
|
|
|
* Tell the user that we could not find a usable
|
|
|
|
* Winsock DLL
|
|
|
|
*/
|
|
|
|
lwsl_err("WSAStartup failed with error: %d\n", err);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-04-02 21:02:54 +08:00
|
|
|
LWS_VISIBLE void
|
2015-12-04 11:08:32 +08:00
|
|
|
lws_plat_context_early_destroy(struct lws_context *context)
|
2014-04-02 14:25:10 +08:00
|
|
|
{
|
|
|
|
if (context->events) {
|
|
|
|
WSACloseEvent(context->events[0]);
|
2014-12-04 23:59:35 +01:00
|
|
|
lws_free(context->events);
|
2014-04-02 14:25:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-02 21:02:54 +08:00
|
|
|
LWS_VISIBLE void
|
2015-12-04 11:08:32 +08:00
|
|
|
lws_plat_context_late_destroy(struct lws_context *context)
|
2014-04-02 14:25:10 +08:00
|
|
|
{
|
2015-06-25 17:51:07 +02:00
|
|
|
int n;
|
|
|
|
|
|
|
|
for (n = 0; n < FD_HASHTABLE_MODULUS; n++) {
|
|
|
|
if (context->fd_hashtable[n].wsi)
|
|
|
|
lws_free(context->fd_hashtable[n].wsi);
|
|
|
|
}
|
|
|
|
|
2014-04-02 14:25:10 +08:00
|
|
|
WSACleanup();
|
|
|
|
}
|
|
|
|
|
2014-04-02 21:02:54 +08:00
|
|
|
LWS_VISIBLE int
|
2015-12-04 11:08:32 +08:00
|
|
|
interface_to_sa(struct lws_context *context,
|
2014-04-02 14:25:10 +08:00
|
|
|
const char *ifname, struct sockaddr_in *addr, size_t addrlen)
|
|
|
|
{
|
2014-11-06 23:06:01 +08:00
|
|
|
long long address = inet_addr(ifname);
|
|
|
|
|
|
|
|
if (address == INADDR_NONE) {
|
|
|
|
struct hostent *entry = gethostbyname(ifname);
|
|
|
|
if (entry)
|
|
|
|
address = ((struct in_addr *)entry->h_addr_list[0])->s_addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (address == INADDR_NONE)
|
|
|
|
return -1;
|
|
|
|
|
2015-12-06 08:00:03 +08:00
|
|
|
addr->sin_addr.s_addr = (unsigned long)address;
|
2014-11-06 23:06:01 +08:00
|
|
|
|
|
|
|
return 0;
|
2014-04-02 14:25:10 +08:00
|
|
|
}
|
|
|
|
|
2014-04-02 21:02:54 +08:00
|
|
|
LWS_VISIBLE void
|
2015-12-04 11:08:32 +08:00
|
|
|
lws_plat_insert_socket_into_fds(struct lws_context *context,
|
|
|
|
struct lws *wsi)
|
2014-04-02 14:25:10 +08:00
|
|
|
{
|
|
|
|
context->fds[context->fds_count++].revents = 0;
|
|
|
|
context->events[context->fds_count] = WSACreateEvent();
|
|
|
|
WSAEventSelect(wsi->sock, context->events[context->fds_count], LWS_POLLIN);
|
|
|
|
}
|
|
|
|
|
2014-04-02 21:02:54 +08:00
|
|
|
LWS_VISIBLE void
|
2015-12-04 11:08:32 +08:00
|
|
|
lws_plat_delete_socket_from_fds(struct lws_context *context,
|
|
|
|
struct lws *wsi, int m)
|
2014-04-02 14:25:10 +08:00
|
|
|
{
|
|
|
|
WSACloseEvent(context->events[m + 1]);
|
|
|
|
context->events[m + 1] = context->events[context->fds_count + 1];
|
|
|
|
}
|
|
|
|
|
2014-04-02 21:02:54 +08:00
|
|
|
LWS_VISIBLE void
|
2015-12-04 11:08:32 +08:00
|
|
|
lws_plat_service_periodic(struct lws_context *context)
|
2014-04-02 14:25:10 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-02 21:02:54 +08:00
|
|
|
LWS_VISIBLE int
|
2015-12-04 11:08:32 +08:00
|
|
|
lws_plat_change_pollfd(struct lws_context *context,
|
|
|
|
struct lws *wsi, struct lws_pollfd *pfd)
|
2014-04-02 14:25:10 +08:00
|
|
|
{
|
2015-11-18 09:51:07 +08:00
|
|
|
long networkevents = LWS_POLLHUP;
|
2015-12-14 08:52:03 +08:00
|
|
|
|
2014-04-02 14:25:10 +08:00
|
|
|
if ((pfd->events & LWS_POLLIN))
|
|
|
|
networkevents |= LWS_POLLIN;
|
2015-12-14 08:52:03 +08:00
|
|
|
|
2015-11-18 09:51:07 +08:00
|
|
|
if ((pfd->events & LWS_POLLOUT))
|
|
|
|
networkevents |= LWS_POLLOUT;
|
2014-04-02 14:25:10 +08:00
|
|
|
|
|
|
|
if (WSAEventSelect(wsi->sock,
|
|
|
|
context->events[wsi->position_in_fds_table + 1],
|
|
|
|
networkevents) != SOCKET_ERROR)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
lwsl_err("WSAEventSelect() failed with error %d\n", LWS_ERRNO);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-04-02 23:03:23 +08:00
|
|
|
LWS_VISIBLE const char *
|
|
|
|
lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
|
2015-12-14 08:52:03 +08:00
|
|
|
{
|
2014-04-15 18:45:48 +02:00
|
|
|
WCHAR *buffer;
|
|
|
|
DWORD bufferlen = cnt;
|
|
|
|
BOOL ok = FALSE;
|
|
|
|
|
2014-12-04 23:59:35 +01:00
|
|
|
buffer = lws_malloc(bufferlen);
|
2014-04-15 18:45:48 +02:00
|
|
|
if (!buffer) {
|
|
|
|
lwsl_err("Out of memory\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-04-02 21:31:07 +08:00
|
|
|
|
2014-04-15 18:45:48 +02:00
|
|
|
if (af == AF_INET) {
|
|
|
|
struct sockaddr_in srcaddr;
|
|
|
|
bzero(&srcaddr, sizeof(srcaddr));
|
|
|
|
srcaddr.sin_family = AF_INET;
|
|
|
|
memcpy(&(srcaddr.sin_addr), src, sizeof(srcaddr.sin_addr));
|
2014-04-02 21:31:07 +08:00
|
|
|
|
2014-04-15 18:45:48 +02:00
|
|
|
if (!WSAAddressToStringW((struct sockaddr*)&srcaddr, sizeof(srcaddr), 0, buffer, &bufferlen))
|
|
|
|
ok = TRUE;
|
|
|
|
#ifdef LWS_USE_IPV6
|
|
|
|
} else if (af == AF_INET6) {
|
|
|
|
struct sockaddr_in6 srcaddr;
|
|
|
|
bzero(&srcaddr, sizeof(srcaddr));
|
|
|
|
srcaddr.sin6_family = AF_INET6;
|
|
|
|
memcpy(&(srcaddr.sin6_addr), src, sizeof(srcaddr.sin6_addr));
|
|
|
|
|
|
|
|
if (!WSAAddressToStringW((struct sockaddr*)&srcaddr, sizeof(srcaddr), 0, buffer, &bufferlen))
|
|
|
|
ok = TRUE;
|
|
|
|
#endif
|
|
|
|
} else
|
|
|
|
lwsl_err("Unsupported type\n");
|
|
|
|
|
|
|
|
if (!ok) {
|
|
|
|
int rv = WSAGetLastError();
|
|
|
|
lwsl_err("WSAAddressToString() : %d\n", rv);
|
|
|
|
} else {
|
|
|
|
if (WideCharToMultiByte(CP_ACP, 0, buffer, bufferlen, dst, cnt, 0, NULL) <= 0)
|
|
|
|
ok = FALSE;
|
|
|
|
}
|
2014-04-02 21:31:07 +08:00
|
|
|
|
2014-12-04 23:59:35 +01:00
|
|
|
lws_free(buffer);
|
2014-04-15 18:45:48 +02:00
|
|
|
return ok ? dst : NULL;
|
2014-04-02 23:03:23 +08:00
|
|
|
}
|
lws_plat_fd implement platform default handlers
This is a rewrite of the patch from Soapyman here
https://github.com/warmcat/libwebsockets/pull/363
The main changes compared to Soapyman's original patch are
- There's no new stuff in the info struct user code does any overrides
it may want to do explicitly after lws_context_create returns
- User overrides for file ops can call through (subclass) to the original
platform implementation using lws_get_fops_plat()
- A typedef is provided for plat-specific fd type
- Public helpers are provided to allow user code to be platform-independent
about file access, using the lws platform file operations underneath:
static inline lws_filefd_type
lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
unsigned long *filelen, int flags)
static inline int
lws_plat_file_close(struct lws_plat_file_ops *fops, lws_filefd_type fd)
static inline unsigned long
lws_plat_file_seek_cur(struct lws_plat_file_ops *fops, lws_filefd_type fd,
long offset_from_cur_pos)
static inline int
lws_plat_file_read(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
static inline int
lws_plat_file_write(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
There's example documentation and implementation in the test server.
Signed-off-by: Andy Green <andy.green@linaro.org>
2015-12-10 07:58:58 +08:00
|
|
|
|
|
|
|
static lws_filefd_type
|
2015-12-11 13:12:58 +08:00
|
|
|
_lws_plat_file_open(struct lws *wsi, const char *filename,
|
|
|
|
unsigned long *filelen, int flags)
|
lws_plat_fd implement platform default handlers
This is a rewrite of the patch from Soapyman here
https://github.com/warmcat/libwebsockets/pull/363
The main changes compared to Soapyman's original patch are
- There's no new stuff in the info struct user code does any overrides
it may want to do explicitly after lws_context_create returns
- User overrides for file ops can call through (subclass) to the original
platform implementation using lws_get_fops_plat()
- A typedef is provided for plat-specific fd type
- Public helpers are provided to allow user code to be platform-independent
about file access, using the lws platform file operations underneath:
static inline lws_filefd_type
lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
unsigned long *filelen, int flags)
static inline int
lws_plat_file_close(struct lws_plat_file_ops *fops, lws_filefd_type fd)
static inline unsigned long
lws_plat_file_seek_cur(struct lws_plat_file_ops *fops, lws_filefd_type fd,
long offset_from_cur_pos)
static inline int
lws_plat_file_read(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
static inline int
lws_plat_file_write(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
There's example documentation and implementation in the test server.
Signed-off-by: Andy Green <andy.green@linaro.org>
2015-12-10 07:58:58 +08:00
|
|
|
{
|
|
|
|
HANDLE ret;
|
|
|
|
WCHAR buf[MAX_PATH];
|
|
|
|
|
2015-12-11 13:12:58 +08:00
|
|
|
(void)wsi;
|
lws_plat_fd implement platform default handlers
This is a rewrite of the patch from Soapyman here
https://github.com/warmcat/libwebsockets/pull/363
The main changes compared to Soapyman's original patch are
- There's no new stuff in the info struct user code does any overrides
it may want to do explicitly after lws_context_create returns
- User overrides for file ops can call through (subclass) to the original
platform implementation using lws_get_fops_plat()
- A typedef is provided for plat-specific fd type
- Public helpers are provided to allow user code to be platform-independent
about file access, using the lws platform file operations underneath:
static inline lws_filefd_type
lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
unsigned long *filelen, int flags)
static inline int
lws_plat_file_close(struct lws_plat_file_ops *fops, lws_filefd_type fd)
static inline unsigned long
lws_plat_file_seek_cur(struct lws_plat_file_ops *fops, lws_filefd_type fd,
long offset_from_cur_pos)
static inline int
lws_plat_file_read(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
static inline int
lws_plat_file_write(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
There's example documentation and implementation in the test server.
Signed-off-by: Andy Green <andy.green@linaro.org>
2015-12-10 07:58:58 +08:00
|
|
|
MultiByteToWideChar(CP_UTF8, 0, filename, -1, buf, ARRAY_SIZE(buf));
|
2015-12-14 06:40:53 +08:00
|
|
|
if ((flags & 7) == _O_RDONLY) {
|
lws_plat_fd implement platform default handlers
This is a rewrite of the patch from Soapyman here
https://github.com/warmcat/libwebsockets/pull/363
The main changes compared to Soapyman's original patch are
- There's no new stuff in the info struct user code does any overrides
it may want to do explicitly after lws_context_create returns
- User overrides for file ops can call through (subclass) to the original
platform implementation using lws_get_fops_plat()
- A typedef is provided for plat-specific fd type
- Public helpers are provided to allow user code to be platform-independent
about file access, using the lws platform file operations underneath:
static inline lws_filefd_type
lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
unsigned long *filelen, int flags)
static inline int
lws_plat_file_close(struct lws_plat_file_ops *fops, lws_filefd_type fd)
static inline unsigned long
lws_plat_file_seek_cur(struct lws_plat_file_ops *fops, lws_filefd_type fd,
long offset_from_cur_pos)
static inline int
lws_plat_file_read(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
static inline int
lws_plat_file_write(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
There's example documentation and implementation in the test server.
Signed-off-by: Andy Green <andy.green@linaro.org>
2015-12-10 07:58:58 +08:00
|
|
|
ret = CreateFileW(buf, GENERIC_READ, FILE_SHARE_READ,
|
|
|
|
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
} else {
|
|
|
|
lwsl_err("%s: open for write not implemented\n", __func__);
|
|
|
|
*filelen = 0;
|
|
|
|
return LWS_INVALID_FILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret != LWS_INVALID_FILE)
|
|
|
|
*filelen = GetFileSize(ret, NULL);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-12-11 13:12:58 +08:00
|
|
|
_lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
|
lws_plat_fd implement platform default handlers
This is a rewrite of the patch from Soapyman here
https://github.com/warmcat/libwebsockets/pull/363
The main changes compared to Soapyman's original patch are
- There's no new stuff in the info struct user code does any overrides
it may want to do explicitly after lws_context_create returns
- User overrides for file ops can call through (subclass) to the original
platform implementation using lws_get_fops_plat()
- A typedef is provided for plat-specific fd type
- Public helpers are provided to allow user code to be platform-independent
about file access, using the lws platform file operations underneath:
static inline lws_filefd_type
lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
unsigned long *filelen, int flags)
static inline int
lws_plat_file_close(struct lws_plat_file_ops *fops, lws_filefd_type fd)
static inline unsigned long
lws_plat_file_seek_cur(struct lws_plat_file_ops *fops, lws_filefd_type fd,
long offset_from_cur_pos)
static inline int
lws_plat_file_read(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
static inline int
lws_plat_file_write(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
There's example documentation and implementation in the test server.
Signed-off-by: Andy Green <andy.green@linaro.org>
2015-12-10 07:58:58 +08:00
|
|
|
{
|
2015-12-11 13:12:58 +08:00
|
|
|
(void)wsi;
|
|
|
|
|
lws_plat_fd implement platform default handlers
This is a rewrite of the patch from Soapyman here
https://github.com/warmcat/libwebsockets/pull/363
The main changes compared to Soapyman's original patch are
- There's no new stuff in the info struct user code does any overrides
it may want to do explicitly after lws_context_create returns
- User overrides for file ops can call through (subclass) to the original
platform implementation using lws_get_fops_plat()
- A typedef is provided for plat-specific fd type
- Public helpers are provided to allow user code to be platform-independent
about file access, using the lws platform file operations underneath:
static inline lws_filefd_type
lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
unsigned long *filelen, int flags)
static inline int
lws_plat_file_close(struct lws_plat_file_ops *fops, lws_filefd_type fd)
static inline unsigned long
lws_plat_file_seek_cur(struct lws_plat_file_ops *fops, lws_filefd_type fd,
long offset_from_cur_pos)
static inline int
lws_plat_file_read(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
static inline int
lws_plat_file_write(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
There's example documentation and implementation in the test server.
Signed-off-by: Andy Green <andy.green@linaro.org>
2015-12-10 07:58:58 +08:00
|
|
|
CloseHandle((HANDLE)fd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long
|
2015-12-11 13:12:58 +08:00
|
|
|
_lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
|
lws_plat_fd implement platform default handlers
This is a rewrite of the patch from Soapyman here
https://github.com/warmcat/libwebsockets/pull/363
The main changes compared to Soapyman's original patch are
- There's no new stuff in the info struct user code does any overrides
it may want to do explicitly after lws_context_create returns
- User overrides for file ops can call through (subclass) to the original
platform implementation using lws_get_fops_plat()
- A typedef is provided for plat-specific fd type
- Public helpers are provided to allow user code to be platform-independent
about file access, using the lws platform file operations underneath:
static inline lws_filefd_type
lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
unsigned long *filelen, int flags)
static inline int
lws_plat_file_close(struct lws_plat_file_ops *fops, lws_filefd_type fd)
static inline unsigned long
lws_plat_file_seek_cur(struct lws_plat_file_ops *fops, lws_filefd_type fd,
long offset_from_cur_pos)
static inline int
lws_plat_file_read(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
static inline int
lws_plat_file_write(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
There's example documentation and implementation in the test server.
Signed-off-by: Andy Green <andy.green@linaro.org>
2015-12-10 07:58:58 +08:00
|
|
|
{
|
2015-12-11 13:12:58 +08:00
|
|
|
(void)wsi;
|
|
|
|
|
lws_plat_fd implement platform default handlers
This is a rewrite of the patch from Soapyman here
https://github.com/warmcat/libwebsockets/pull/363
The main changes compared to Soapyman's original patch are
- There's no new stuff in the info struct user code does any overrides
it may want to do explicitly after lws_context_create returns
- User overrides for file ops can call through (subclass) to the original
platform implementation using lws_get_fops_plat()
- A typedef is provided for plat-specific fd type
- Public helpers are provided to allow user code to be platform-independent
about file access, using the lws platform file operations underneath:
static inline lws_filefd_type
lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
unsigned long *filelen, int flags)
static inline int
lws_plat_file_close(struct lws_plat_file_ops *fops, lws_filefd_type fd)
static inline unsigned long
lws_plat_file_seek_cur(struct lws_plat_file_ops *fops, lws_filefd_type fd,
long offset_from_cur_pos)
static inline int
lws_plat_file_read(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
static inline int
lws_plat_file_write(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
There's example documentation and implementation in the test server.
Signed-off-by: Andy Green <andy.green@linaro.org>
2015-12-10 07:58:58 +08:00
|
|
|
return SetFilePointer((HANDLE)fd, offset, NULL, FILE_CURRENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-12-11 13:12:58 +08:00
|
|
|
_lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
|
|
|
|
unsigned char* buf, unsigned long len)
|
lws_plat_fd implement platform default handlers
This is a rewrite of the patch from Soapyman here
https://github.com/warmcat/libwebsockets/pull/363
The main changes compared to Soapyman's original patch are
- There's no new stuff in the info struct user code does any overrides
it may want to do explicitly after lws_context_create returns
- User overrides for file ops can call through (subclass) to the original
platform implementation using lws_get_fops_plat()
- A typedef is provided for plat-specific fd type
- Public helpers are provided to allow user code to be platform-independent
about file access, using the lws platform file operations underneath:
static inline lws_filefd_type
lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
unsigned long *filelen, int flags)
static inline int
lws_plat_file_close(struct lws_plat_file_ops *fops, lws_filefd_type fd)
static inline unsigned long
lws_plat_file_seek_cur(struct lws_plat_file_ops *fops, lws_filefd_type fd,
long offset_from_cur_pos)
static inline int
lws_plat_file_read(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
static inline int
lws_plat_file_write(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
There's example documentation and implementation in the test server.
Signed-off-by: Andy Green <andy.green@linaro.org>
2015-12-10 07:58:58 +08:00
|
|
|
{
|
|
|
|
DWORD _amount;
|
|
|
|
|
2015-12-11 13:12:58 +08:00
|
|
|
(void *)wsi;
|
lws_plat_fd implement platform default handlers
This is a rewrite of the patch from Soapyman here
https://github.com/warmcat/libwebsockets/pull/363
The main changes compared to Soapyman's original patch are
- There's no new stuff in the info struct user code does any overrides
it may want to do explicitly after lws_context_create returns
- User overrides for file ops can call through (subclass) to the original
platform implementation using lws_get_fops_plat()
- A typedef is provided for plat-specific fd type
- Public helpers are provided to allow user code to be platform-independent
about file access, using the lws platform file operations underneath:
static inline lws_filefd_type
lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
unsigned long *filelen, int flags)
static inline int
lws_plat_file_close(struct lws_plat_file_ops *fops, lws_filefd_type fd)
static inline unsigned long
lws_plat_file_seek_cur(struct lws_plat_file_ops *fops, lws_filefd_type fd,
long offset_from_cur_pos)
static inline int
lws_plat_file_read(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
static inline int
lws_plat_file_write(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
There's example documentation and implementation in the test server.
Signed-off-by: Andy Green <andy.green@linaro.org>
2015-12-10 07:58:58 +08:00
|
|
|
if (!ReadFile((HANDLE)fd, buf, (DWORD)len, &_amount, NULL)) {
|
|
|
|
*amount = 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*amount = (unsigned long)_amount;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-12-11 13:12:58 +08:00
|
|
|
_lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
|
lws_plat_fd implement platform default handlers
This is a rewrite of the patch from Soapyman here
https://github.com/warmcat/libwebsockets/pull/363
The main changes compared to Soapyman's original patch are
- There's no new stuff in the info struct user code does any overrides
it may want to do explicitly after lws_context_create returns
- User overrides for file ops can call through (subclass) to the original
platform implementation using lws_get_fops_plat()
- A typedef is provided for plat-specific fd type
- Public helpers are provided to allow user code to be platform-independent
about file access, using the lws platform file operations underneath:
static inline lws_filefd_type
lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
unsigned long *filelen, int flags)
static inline int
lws_plat_file_close(struct lws_plat_file_ops *fops, lws_filefd_type fd)
static inline unsigned long
lws_plat_file_seek_cur(struct lws_plat_file_ops *fops, lws_filefd_type fd,
long offset_from_cur_pos)
static inline int
lws_plat_file_read(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
static inline int
lws_plat_file_write(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
There's example documentation and implementation in the test server.
Signed-off-by: Andy Green <andy.green@linaro.org>
2015-12-10 07:58:58 +08:00
|
|
|
unsigned char* buf, unsigned long len)
|
|
|
|
{
|
2015-12-11 13:12:58 +08:00
|
|
|
(void)wsi;
|
|
|
|
(void)fd;
|
|
|
|
(void)amount;
|
|
|
|
(void)buf;
|
|
|
|
(void)len;
|
|
|
|
|
|
|
|
lwsl_err("%s: not implemented yet on this platform\n", __func__);
|
2015-12-14 08:52:03 +08:00
|
|
|
|
lws_plat_fd implement platform default handlers
This is a rewrite of the patch from Soapyman here
https://github.com/warmcat/libwebsockets/pull/363
The main changes compared to Soapyman's original patch are
- There's no new stuff in the info struct user code does any overrides
it may want to do explicitly after lws_context_create returns
- User overrides for file ops can call through (subclass) to the original
platform implementation using lws_get_fops_plat()
- A typedef is provided for plat-specific fd type
- Public helpers are provided to allow user code to be platform-independent
about file access, using the lws platform file operations underneath:
static inline lws_filefd_type
lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
unsigned long *filelen, int flags)
static inline int
lws_plat_file_close(struct lws_plat_file_ops *fops, lws_filefd_type fd)
static inline unsigned long
lws_plat_file_seek_cur(struct lws_plat_file_ops *fops, lws_filefd_type fd,
long offset_from_cur_pos)
static inline int
lws_plat_file_read(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
static inline int
lws_plat_file_write(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
There's example documentation and implementation in the test server.
Signed-off-by: Andy Green <andy.green@linaro.org>
2015-12-10 07:58:58 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
LWS_VISIBLE int
|
|
|
|
lws_plat_init(struct lws_context *context,
|
|
|
|
struct lws_context_creation_info *info)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < FD_HASHTABLE_MODULUS; i++) {
|
|
|
|
context->fd_hashtable[i].wsi =
|
|
|
|
lws_zalloc(sizeof(struct lws*) * context->max_fds);
|
|
|
|
|
|
|
|
if (!context->fd_hashtable[i].wsi)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
context->events = lws_malloc(sizeof(WSAEVENT) * (context->max_fds + 1));
|
|
|
|
if (context->events == NULL) {
|
|
|
|
lwsl_err("Unable to allocate events array for %d connections\n",
|
|
|
|
context->max_fds);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
context->fds_count = 0;
|
|
|
|
context->events[0] = WSACreateEvent();
|
|
|
|
|
|
|
|
context->fd_random = 0;
|
|
|
|
|
|
|
|
context->fops.open = _lws_plat_file_open;
|
|
|
|
context->fops.close = _lws_plat_file_close;
|
|
|
|
context->fops.seek_cur = _lws_plat_file_seek_cur;
|
|
|
|
context->fops.read = _lws_plat_file_read;
|
|
|
|
context->fops.write = _lws_plat_file_write;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|