Commit graph

376 commits

Author SHA1 Message Date
Andy Green
93f98d748d valgrind client go through context destroy on connection error
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 14:05:02 +08:00
Andy Green
a7109e6ebf valgrind introduce protocol init and destroy user callbacks
To get a clean bill of health from valgrind, we have to have a way to
inform the user code that we're going down and it should free everything
it is holding that was malloc'd.

This patch introduces LWS_CALLBACK_PROTOCOL_DESTROY which each protocol
gets when the context is being destroyed and no more activity will come
after that call.  They can get rid of everything there.

To match it, LWS_CALLBACK_PROTOCOL_INIT is introduced which would allow
one-time init per protocol too.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 12:05:54 +08:00
Andy Green
16ab3185c4 replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.

It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure.  It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).

The 3-level thing is all in one struct

 - array indexed by the header enum, pointing to first "fragment" index
	(ie, header type to fragment lookup, or 0 for none)

 - array of fragments indexes, enough for 2 x the number of known headers
	(fragment array... note that fragments can point to a "next"
	fragment if the same header is spread across multiple entries)

 - linear char array where the known header payload gets written
	(fragments point into null-terminated strings stored in here,
	only the known header content is stored)

http headers can legally be split over multiple headers of the same
name which should be concatenated.  This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them.  There are apis to get the total length and copy out a
linear, concatenated version to a buffer.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 11:10:57 +08:00
Andy Green
8e0c98484e correct test client to close synchronously with last send
Noticed previously the test client lifetime is decoupled from
the actual send activity.  With SSL if the connection starts but
there is a period of SSL-layer "blocking" (actually fail-and-retry)
the mirror lifetime could be exhausted before the connection really
completed, making it stall after it was then closed.

This corrects that so connection lifetime management is done in the
send callback.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-09 14:27:24 +08:00
Andy Green
e7c97e8429 align max frame for mirror protocol to what the code does
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-09 14:27:15 +08:00
Andy Green
1b26527e72 change context creation params to struct
*** This patch changes an API all apps use ***

Context creation parameters are getting a bit out of control, this
patch creates a struct to contain them.

All the test apps are updated accordingly.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-09 14:01:09 +08:00
Andy Green
5449511d3e remove fixed rx buffer allow definition per protocol
A new protocol member is defined that controls the size of rx
buffer allocation per connection.  For compatibility 0 size
allocates 4096, but you should adapt your protocol definition
array in the user code to declare an appropriate value.

See the changelog for more detail.

The advantage is the rx frame buffer size is now tailored to
what is expected from the protocol, rather than being fixed
to a default of 4096.  If your protocol only sends frames of
a dozen bytes this allows you to only allocate an rx frame
buffer of the same size.

For example the per-connection allocation (excluding headers)
for the test server fell from ~4500 to < 750 bytes with this.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-08 13:16:07 +08:00
Andy Green
b8b247d3e1 remove need for filepath buffer on http file serve
This gets rid of the stack buffer while serving files, and the
PATH_MAX char array that used to hold the filepath in the wsi.

It holds an extra file descriptor open while serving the file,
however it attempts to stuff the socket with as much of the
file as it can take.  For files of a few KB, that typically
completes (without blocking) in the call to
libwebsockets_serve_http_file() and then closes the file
descriptor before returning.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-08 12:19:01 +08:00
Peter Pentchev
c74964ec44 Fix two typos. 2013-02-07 23:23:10 +08:00
Joakim Soderberg
d2edfec5fa Fixed linux compilation and added more compile options.
- Tested and works on Linux now also, including SSL support.
- Look for ZLIB not zlib.
- Added CMake options for setting all LWS_ defines.
2013-02-06 15:49:12 +09:00
Joakim Soderberg
4c53123677 CMake support + fixed windows build.
- Finalized CMake support (tested on windows only so far).
  - Uses a generated lws_config.h that is included in
  private-libwebsocket to pass defines, only used if CMAKE_BUILD is set.
  - Support for SSL on Windows.
  - Initial support for CyaSSL replacement of OpenSSL (This has been added
    to my older CMake-fork but haven't been tested on this version yet).
- Fixed windows build (see below for details).
- Fixed at least the 32-bit Debug build for the existing Visual Studio
  Project. (Not to keen fixing all the others when we have CMake support
  anyway (which can generate much better project files)...)
- BUGFIXES:
  - handshake.c
    - used C99 definition of handshake_0405 function
  - libwebsocket.c
    - syslog not available on windows, put in ifdefs.
    - Fixed previous known crash bug on Windows where WSAPoll in
      Ws2_32.dll would not be present, causing the poll function pointer
      being set to NULL.
    - Uninitialized variable context->listen_service_extraseen would
      result in stack overflow because of infinite recursion. Fixed by
      initializing in libwebsocket_create_context
    - SO_REUSADDR means something different on Windows compared to Unix.
    - Setting a socket to nonblocking is done differently on Windows.
      (This should probably broken out into a helper function instead)
    - lwsl_emit_syslog -> lwsl_emit_stderr on Windows.
  - private-libwebsocket.h
    - PATH_MAX is not available on Windows, define as MAX_PATH
    - Always define LWS_NO_DAEMONIZE on windows.
    - Don't define lws_latency as inline that does nothing. inline is not
      support by the Microsoft compiler, replaced with an empty define
      instead. (It's __inline in MSVC)
  - server.c
    - Fixed nonblock call on windows
  - test-ping.c
    - Don't use C99 features (Microsoft compiler does not support it).
    - Move non-win32 headers into ifdefs.
    - Skip use of sighandler on Windows.
  - test-server.c
    - ifdef syslog parts on Windows.
2013-02-06 15:49:12 +09:00
Andy Green
0d49c8d1a0 improve test server poll loop docs
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-02 23:02:56 +08:00
Andy Green
36eb70d7a9 additional casts allow test server build as cpp
With these explicit casts that are not needed in C, it's possible to build
the test server using g++ like this, after building and installing the
library.

g++ -DINSTALL_DATADIR=\"/usr/share\" -ocpptest test.cpp -lwebsockets

Add a small documentation to README.coding

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-01 08:42:15 +08:00
Andy Green
c51823a418 renovate test html
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-31 19:50:57 +08:00
Andy Green
cbb3122ab4 fixes for without server and without client
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-31 09:57:05 +08:00
Andy Green
769153ec5d introduce test echo
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-30 12:28:44 +08:00
Andy Green
aaf0b9f514 change get_peer_addresses to use context wsi latency
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-30 08:49:13 +08:00
Andy Green
08f2c017e8 test server terminate cleanly on ctrl c
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-30 08:02:26 +08:00
Andy Green
6f520a5195 evict all broadcast support
Libwebsockets is fundamentally singlethreaded... the existence of the
fork and broadcast support, especially in the sample server is
giving the wrong idea about how to use it.

This replaces broadcast in the sample server with
libwebsocket_callback_on_writable_all_protocol().  The whole idea of
'broadcast' is removed.

All of the broadcast proxy stuff is removed: data must now be sent
from the callback only.  Doing othherwise is not reliable since the
service loop may close the socket and free the wsi at any time,
invalidating a wsi pointer held by another thread (don't do that!)

Likewise the confirm_legit_wsi api added recently does not help the
other thread case, since if the wsi has been freed dereferencing the
wsi to study if it is legit or not will segfault in that case.  So
this is removed too.

The overall effect is to push user code to only operate inside the
protocol callbacks or external poll loops, ie, single thread context.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-29 22:13:55 +08:00
Andy Green
52f28ce67a autocreate foreign broadcast sockets on broadcast
Also introduce libwebsockets_broadcast_foreign() separate from libwebsockets_broadcast()

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-25 17:52:16 +08:00
Andy Green
5c54d622ff use correct LWS_NO_DAEMONIZE on test server
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-21 12:58:04 +08:00
Andy Green
5738c0e838 remove all support for pre v13 protocols
Since v13 was defined as the released ietf version the older versions
are deprecated.  This patch strips out everything to do with the older
versions and gets rid of the option to send stuff unmasked.

The in-tree md5 implementation is then also deleted as nothing needs
it any more, 1280 loc are shed in all

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-21 09:53:35 +08:00
Andy Green
f5bc1306ef disable private broadcast sockets if enable no fork config option
The whole thing about count_protocols + 1 broadcast sockets and
associated dummy wsis is a workaround for getting a broadcast from
a different process context, if we are running with --enable-no-fork
then we don't need any of it in.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-21 09:09:52 +08:00
Edwin van den Oetelaar
596b220c40 align test server extpoll with library dynamic approach
Signed-off-by: Edwin van den Oetelaar <oetelaar.automatisering@gmail.com>
2013-01-20 20:51:14 +08:00
Andy Green
13ba5bbc63 zlib not needed if no extensions
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-20 18:26:20 +08:00
Andy Green
3182ece3a4 introduce without extensions
The new --without-extensions config flag completely removes all code
and data related to extensions from the build throughout the library
when given.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-20 17:08:31 +08:00
Andy Green
24cba92c7e make use of lock file
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-19 14:39:24 +08:00
Andy Green
fd6764a1fb test server add daemonization flag
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-19 13:13:48 +08:00
Andy Green
058ba81017 test server use syslog logging
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-19 13:11:26 +08:00
Andy Green
a1ce6be947 refactor and introduce without server configure option
Move server-only stuff into their own files and make building
that depend on not having --without-server on the configure

Make fragments in other places conditional as well

Remove client-related members from struct libwebscket when
building LWS_NO_CLIENT

Apps:

normal: build test server, client, fraggle, ping
--without-client: build test server
--without-server: build test client, ping

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-18 11:43:21 +08:00
Andy Green
706961dbb5 solve flowcontrol problems
Problems with rx flow control implementation were the underlying cause
of the connection stalling issue that was covered up with the udelay()
patch that was removed recently.

This get rx flow control working properly and corrects problems with
fifo management in the test server mirror protocol code too.

The rxfow control api has been changed to just set a flag, so it's very cheap
to call from user code.  After the callbacks that might use the rxflow control
api the flag is checked and any pending actions done.

rx flow control now stops any rx packet coming immediately, with compessed
connections "just what was left in the pipe" might be hundreds of KBytes.  To
implement that the current packet being decoded is copied into a malloc'd buffer
by the rx processing code now.

When rxflow is allows to come again, the buffer is drained and freed before any
new packet content is accepted.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-17 17:18:55 +08:00
Edwin van der Oetelaar
6c720c4440 use simple lookup table for extpoll
Hash stuff is overkill since Edwin found a max connection limit of 30000 on his
box anyway.  Just use a simple preallocated lookup table and fds array.

AG Modified for style and removed debugging bits

Signed-off-by: Edwin van der Oetelaar <oetelaar.automatisering@gmail.com>
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-17 15:50:09 +08:00
Andy Green
443ea54c5c move array bounds gcc workaround outside function
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-16 17:46:00 +08:00
Andy Green
f7248f8dfa update ping test client and stop exposing payload to extensions
Ping and Pong payload in control messages need to be
above the fray of extension payload munging

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-16 15:12:33 +08:00
Andy Green
03674a655d configure without client
This leverages the refactor patches to introduce the ability to
disable building any client side code in the library or the client
side test apps.

This will be a considerable size saving for embedded server-only
case.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-16 12:35:46 +08:00
Andy Green
5378b143c3 test client remove usleep
Whatever caused the need for this has gone away

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-16 12:34:58 +08:00
Andy Green
3278872824 workaround for some gcc array bounds false positive
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-16 10:21:34 +08:00
Andy Green
fab033dd4f add logo to test file
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-16 10:11:34 +08:00
Andy Green
bb2dc8aeb1 update test server html serving callback to use aepd whitelist approach
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-16 10:06:28 +08:00
Andy Green
56d4697463 add libwebsockets.org logo to share
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-16 10:00:39 +08:00
Andy Green
5fc460cd41 extpoll use hashtable for fd tracking
This implements a much faster, hashtable-based tracking scheme for
external poll fds.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-15 20:44:46 +08:00
Edwin van der Oetelaar
8db451f363 extpoll break out of loop when set or clear finds fd
Signed-off-by: Edwin van der Oetelaar <oetelaar.automatisering@gmail.com>
2013-01-15 16:22:34 +08:00
Andy Green
d280b6ecb3 http service break into outer loop states
Previously we sat and looped to dump a file over http protocol.

Actually that's a source of blocking to the other sockets being serviced.

This patch breaks up the file service into a roundtrip around the poll()
loop for each 512-byte packet.  It doesn't make much difference if the
server is idle, but if it's busy it makes sure everyone else is getting
service while the file is sent.

It doesn't try to optimize multiple users of the file or to keep the
descriptor open, the point of this patch is to establish the breaking up
of the file send action into the poll loop.

On the user side, there are two differences:

 - context is now needed in the first argument to libwebsockets_serve_http_file()
that's not too bad since we provide context in the callback.

 - file send is now asynchronous to the user code, you get a new callback coming
in protocol 0 when it's done, LWS_CALLBACK_HTTP_FILE_COMPLETION

libwebsockets-test-server is updated accordingly.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-15 16:00:36 +08:00
Andy Green
a50dd1af40 merge test server extpoll into test server
the -extpoll version of the test server was starting to rot compared to
the test-server.c it was originally based on.

This patch deletes the -extpoll.c version and instead has the test-server.c
source built two different ways in the makefile, once with the define
EXTERNAL_POLL which forces non-fork mode and enables the "by hand"
pollfd array handling.  The resulting binary of that is still called
libwebsockets-test-server-extpoll.

Another problem was that the pollfd array length needs to match MAX_CLIENTS, that
now happens during the build.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-15 15:50:31 +08:00
Edwin van der Oetelaar
5e59bf15e1 optimize extpoll fd delete
Previous method of shifting back array by one to cover the deleted
item could be expensive when the list was large and the deleted item
appeared early in it.

This scheme swaps the last guy into the vacant space and reduces the
list size by one.

(AG adapted for style and not to care if n is end guy)

Signed-off-by: Edwin van der Oetelaar <oetelaar.automatisering@gmail.com>
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-15 15:50:28 +08:00
Andy Green
43063dd250 add longlived option to test client
Needed to confirm pending timeouts won't kill the connection, by default
it spams the server with connections that live less than 5s

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-15 15:43:17 +08:00
Andy Green
f7609e9ada logging ensure everyone has a newline
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-15 15:41:15 +08:00
Andy Green
24b588b6d9 absorb README.rst into main README and code
Some of the advice in README.rst became deprecated with recent patches,
the (good) advice about http connection close is better demonstrated
in the code and API docs, and the remainder can go in the main README,
which will have to be refactored itself at some point.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-13 09:53:18 +08:00
Andy Green
de8f27a80b logging extend level set api to allow setting emission function
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-12 09:17:42 +08:00
Andy Green
46ef0cf3c5 allow enabling debug contexts from test apps
Adds a -d switch to everything so you can set the log level bitfeld.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-10 23:34:33 +08:00