private.h: rename to contain dir
[platform/upstream/libwebsockets.git] / lib / plat / windows / windows-fds.c
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2018 Andy Green <andy@warmcat.com>
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public
8  *  License as published by the Free Software Foundation:
9  *  version 2.1 of the License.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  *  MA  02110-1301  USA
20  */
21
22 #ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
23 #define _WINSOCK_DEPRECATED_NO_WARNINGS
24 #endif
25 #include "private-lib-core.h"
26
27 struct lws *
28 wsi_from_fd(const struct lws_context *context, lws_sockfd_type fd)
29 {
30         int h = LWS_FD_HASH(fd);
31         int n = 0;
32
33         for (n = 0; n < context->fd_hashtable[h].length; n++)
34                 if (context->fd_hashtable[h].wsi[n]->desc.sockfd == fd)
35                         return context->fd_hashtable[h].wsi[n];
36
37         return NULL;
38 }
39
40 int
41 insert_wsi(struct lws_context *context, struct lws *wsi)
42 {
43         int h = LWS_FD_HASH(wsi->desc.sockfd);
44
45         if (context->fd_hashtable[h].length == (getdtablesize() - 1)) {
46                 lwsl_err("hash table overflow\n");
47                 return 1;
48         }
49
50         context->fd_hashtable[h].wsi[context->fd_hashtable[h].length++] = wsi;
51
52         return 0;
53 }
54
55 int
56 delete_from_fd(struct lws_context *context, lws_sockfd_type fd)
57 {
58         int h = LWS_FD_HASH(fd);
59         int n = 0;
60
61         for (n = 0; n < context->fd_hashtable[h].length; n++)
62                 if (context->fd_hashtable[h].wsi[n]->desc.sockfd == fd) {
63                         while (n < context->fd_hashtable[h].length) {
64                                 context->fd_hashtable[h].wsi[n] =
65                                         context->fd_hashtable[h].wsi[n + 1];
66                                 n++;
67                         }
68                         context->fd_hashtable[h].length--;
69
70                         return 0;
71                 }
72
73         lwsl_err("Failed to find fd %d requested for "
74                  "delete in hashtable\n", fd);
75         return 1;
76 }