Git Product home page Git Product logo

libwebsock's People

Contributors

payden avatar shawnw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

libwebsock's Issues

Instructions for compiling are outdated

Hey,

I tried to follow the instructions in INSTALL, however they appear to be out of date. It says to run ./configure & make & make install, and for more information to refer to README (which is empty). After some playing around I worked out that I need to use autogen.sh, however the makefile it generates doesn't work. In the end I wrote my own makefile to compile the library

config.h in libwebsock causes conflict with config.h in project using libwebsock

config.h in libwebsock causes conflict with config.h in project using libwebsock (freeciv).

I'm currently looking into using libwebsock in freeciv ( https://github.com/andreasrosdal/freeciv-web/ )

Would it be possible for you to rename config.h in libwebsock to something else?

These are the compilation errors I get:

In file included from ../common/name_translation.h:22:0,
from ../common/city.h:27,
from ../common/player.h:25,
from ../common/game.h:34,
from sernet.c:80:
../fc_config.h:485:0: warning: "PACKAGE" redefined [enabled by default]
In file included from /usr/local/include/websock/websock.h:20:0,
from sernet.c:62:
/usr/local/include/websock/config.h:65:0: note: this is the location of the previous definition
In file included from ../common/name_translation.h:22:0,
from ../common/city.h:27,
from ../common/player.h:25,
from ../common/game.h:34,
from sernet.c:80:
../fc_config.h:488:0: warning: "PACKAGE_BUGREPORT" redefined [enabled by default]
In file included from /usr/local/include/websock/websock.h:20:0,
from sernet.c:62:
/usr/local/include/websock/config.h:68:0: note: this is the location of the previous definition
In file included from ../common/name_translation.h:22:0,
from ../common/city.h:27,
from ../common/player.h:25,
from ../common/game.h:34,
from sernet.c:80:
../fc_config.h:491:0: warning: "PACKAGE_NAME" redefined [enabled by default]
In file included from /usr/local/include/websock/websock.h:20:0,
from sernet.c:62:
/usr/local/include/websock/config.h:71:0: note: this is the location of the previous definition
In file included from ../common/name_translation.h:22:0,
from ../common/city.h:27,
from ../common/player.h:25,
from ../common/game.h:34,
from sernet.c:80:
../fc_config.h:497:0: warning: "PACKAGE_STRING" redefined [enabled by default]
In file included from /usr/local/include/websock/websock.h:20:0,
from sernet.c:62:
/usr/local/include/websock/config.h:74:0: note: this is the location of the previous definition
In file included from ../common/name_translation.h:22:0,
from ../common/city.h:27,
from ../common/player.h:25,
from ../common/game.h:34,
from sernet.c:80:
../fc_config.h:500:0: warning: "PACKAGE_TARNAME" redefined [enabled by default]
In file included from /usr/local/include/websock/websock.h:20:0,
from sernet.c:62:
/usr/local/include/websock/config.h:77:0: note: this is the location of the previous definition
In file included from ../common/name_translation.h:22:0,
from ../common/city.h:27,
from ../common/player.h:25,
from ../common/game.h:34,
from sernet.c:80:
../fc_config.h:506:0: warning: "PACKAGE_VERSION" redefined [enabled by default]
In file included from /usr/local/include/websock/websock.h:20:0,
from sernet.c:62:
/usr/local/include/websock/config.h:83:0: note: this is the location of the previous definition
In file included from ../common/name_translation.h:22:0,
from ../common/city.h:27,
from ../common/player.h:25,
from ../common/game.h:34,
from sernet.c:80:
../fc_config.h:552:0: warning: "VERSION" redefined [enabled by default]
In file included from /usr/local/include/websock/websock.h:20:0,
from sernet.c:62:

SSL specific SIGSEGV

While running the autobahn test suite with an SSL enabled libwebsock server, I get a SIGSEGV for case 2.6. This case involves sending a binary PING and expecting to receive binary PONG back. I'm not sure whether the case has something to do with it or whether something previous is causing some memory corruption.

Segmentation fault when receiving high-speed data

Description

Segmentation fault occurs when messages are received at a high frequency.

Steps to reproduce:

  • Run the code available in example/echo.c
  • Connect with a simple node js client (probably works with any other client implementation)
  • Send messages periodically at increasing frequency, the server will SEGFAULT at around 40 Hz.

Code for the client:

const WebSocket = require('ws');

var frequency = 30;

const socket = new WebSocket('ws://localhost:8081');

socket.onopen = () => {
    console.log('WebSocket connection established');
    
    setInterval(() => {
        const message = 'Hello, WebSocket!';
        socket.send(message);
    }, 1000 / frequency);
};

socket.onclose = () => {
    console.log('WebSocket connection closed');
};

socket.onerror = (error) => {
    console.error(`WebSocket error: ${error.message}`);
}

Details

echo.c is running on a raspberry pi 4B.
gdb output for bt and info thread :

(gdb) handle SIGUSR2 nostop
Signal        Stop      Print   Pass to program Description
SIGUSR2       No        Yes     Yes             User defined signal 2
(gdb) run
Starting program: [...]/build/test 8081
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/aarch64-linux-gnu/libthread_db.so.1".
libwebsock listening on port 8081
[New Thread 0x7ff777f180 (LWP 33915)]
onopen: 8
[Thread 0x7ff777f180 (LWP 33915) exited]
[New Thread 0x7ff777f180 (LWP 33916)]
Received message from client: 8
Message opcode: 1
Payload Length: 17
Payload: Hello, WebSocket!

Thread 3 "test" received signal SIGUSR2, User defined signal 2.
[New Thread 0x7ff6f6f180 (LWP 33917)]
[Thread 0x7ff777f180 (LWP 33916) exited]
Received message from client: 8
Message opcode: 1
Payload Length: 17
Payload: Hello, WebSocket!

Thread 4 "test" received signal SIGUSR2, User defined signal 2.
[Thread 0x7ff6f6f180 (LWP 33917) exited]
[New Thread 0x7ff675f180 (LWP 33918)]
Received message from client: 8
Message opcode: 1
Payload Length: 17
Payload: Hello, WebSocket!

Thread 5 "test" received signal SIGUSR2, User defined signal 2.
[Thread 0x7ff675f180 (LWP 33918) exited]

Thread 1 "test" received signal SIGSEGV, Segmentation fault.
libwebsock_cleanup_thread_list (sock=<optimized out>, what=<optimized out>, arg=0x7ff00010a0) at websock.c:202
202                     if (pthread_equal(current_thread, wrapper->thread)) {
(gdb) bt
#0  libwebsock_cleanup_thread_list (sock=<optimized out>, what=<optimized out>, arg=0x7ff00010a0) at websock.c:202
#1  0x0000007ff7c9188c in ?? () from /lib/aarch64-linux-gnu/libevent-2.1.so.7
#2  0x0000007ff7c91fb0 in event_base_loop () from /lib/aarch64-linux-gnu/libevent-2.1.so.7
#3  0x0000007ff7f98580 in libwebsock_wait (ctx=0x5555571a00) at api.c:141
#4  0x0000005555550bf8 in main ()
(gdb) info thread
  Id   Target Id                              Frame 
* 1    Thread 0x7ff7fba040 (LWP 33869) "test" libwebsock_cleanup_thread_list (sock=<optimized out>, what=<optimized out>, arg=0x7ff00010a0) at websock.c:202
(gdb) 

Odd Question - Sending to specific FD's

I'm still brushing back up on C knowledge, and perhaps there are better suited places to ask this question. Though, you guys seem extremely friendly and helpful, as you've been quite responsive to issues with the code in odd circumstances (i.e. compiling on mac).

I'm a little behind here and still working on grasping working with variables, structs etc.

I realize that *state is a pointer that also contains the sockfd and data (opcode) variables that can be assigned. But if I wanted to send data to a specific socketfd, say, 7... What would I have to do in order to ensure that it makes it there? Would I actually have to define *state->sockfd = 7; - or could I get away with something like:

int sockfd = 7;
libwebsock_send_text(sockfd, data);

I apologize if this seems silly, but also thank you in advance for responding.

libevent_pthreads required, failing

I've seen this on google but none of the fixes seem to work for me.
"libevent_pthreads required, failing" from ./configure
Tried "LIBS=-levent ./configure" but then get

checking for gcc... gcc
checking whether the C compiler works... no
(didn't happen in first case without LIBS=, but got the libevent error

This on Jesse on raspberry pi btw

Crashing if websocket session closes before data send...

If a session closes unexpectedly and data is being sent, it results in a segfault.

  • thread #1: tid = 0xc15ece, 0x000000010036c73a libevent-2.0.5.dylibevent_base_loop + 749, queue = 'com.apple.main-thread', stop reason = signal SIGUSR2 frame #0: 0x000000010036c73a libevent-2.0.5.dylibevent_base_loop + 749
    libevent-2.0.5.dylib`event_base_loop + 749:
    -> 0x10036c73a: jmp 0x10036c745 ; event_base_loop + 760
    0x10036c73c: jle 0x10036c779 ; event_base_loop + 812
    0x10036c73e: addq $0x70, %r14
    0x10036c742: movl -0x58(%rbp), %edx
    (lldb)

I produced the error by clicking refresh twice back to back on the browser. The JS code simply connects the websocket and listens for data, the onconnect function simply sends a hello message.

Add pthreads support for asynchronous callbacks.

Right now, even though libwebsock is event driven, the callbacks themselves are synchronous. What this means is that callbacks are currently very limited in what they can do. If you block in a callback, the whole server's execution is blocked. This is horrible and limits libwebsock tremendously. This project started out as a personal learning experience regarding the WebSocket protocol, but implementing this will actually make it feasible for someone to put to good use in practical applications.

error while loading shared libraries

I have installed the lib following your instructions, and compiled the echo.c in your examples.
When I run it, the following errors emitted.

./echo: error while loading shared libraries: libwebsock.so.1: cannot open shared object file: No such file or directory

Add close API method

libwebsock_close(libwebsock_client_state *state, short code, char *reason);

As an aside, I probably also need to be more informative when we receive close frames from the client. If they specify a code and reason, we should populate some sort of close event structure to be passed to to the onclose callback.

New onclose method might look like this:

int onclose(libwebsock_client_state *state, libwebsock_close_event *e) {
if(e->clean) {
fprintf(stderr, "client cleanly shutdown with code and reason: (%d) %s\n", e->code, e->reason);
}
return 0;
}

Need to do more noodling on this.

`libtoolize` does not exist on Mac OS X

There are two fixes:

  1. Change autogen.sh:

    -libtoolize
    +case `uname` in Darwin*) glibtoolize ;;
    +      *) libtoolize ;; esac
    
  2. Just use autoreconf which is part of the autoconf package and essentially replaces the contents of the autogen.sh script.

error when I compile echo.c

when I compile echo.c, it show this error

root@ubuntu:/home/orange/libwebsock/examples# gcc -g -O2 -o echo echo.c -lwebsock
In file included from /usr/local/include/websock/websock.h:73:0,
from echo.c:17:
/usr/local/include/websock/utf.h:25:17: warning: inline function ‘decode’ declared but never defined
uint32_t inline decode(uint32_t *state, uint32_t *codep, uint32_t byte);
^
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libwebsock.so: undefined reference to `decode'
collect2: error: ld returned 1 exit status

libwsclient_send

Hi,
I have your library in my project.. I have developed the client program where it is able to connect to the server which is developed in java.. Also able to send my message to server over the webscoket.

The problem is when ethernet cable is removed from the server side, their the onerror method is called & their he is closing the webscoket & recreating it.
But from the client side it is getting blocked at libwsclient_send() ;

How can I come over from this blocking call like what we use select() & poll() for linux socket, instead of blocking the whole application.

glibc malloc memory corruption

This is a weird one. I don't know if this has to do with my older glibc environment in which I happened to be working at the time. Anything over 65k, thus using 8 bytes (long long int) ends up causing a crash malloc error for me. Needs further inspection. Just making a note of it for now.

possible incorrect int to pointer conversion or incorrect libwebsock_context definition

libwebsock_context is defined as:
typedef struct _libwebsock_context { int running; int ssl_init; int flags; int owns_base; ev_uintptr_t *socketfd; struct event_base *base; int (*onmessage)(libwebsock_client_state *, libwebsock_message *); int (*control_callback)(libwebsock_client_state *, libwebsock_frame *); int (*onopen)(libwebsock_client_state *); int (*onclose)(libwebsock_client_state *); int (*onpong)(libwebsock_client_state *); int (*onframetoolarge)(libwebsock_client_state *, libwebsock_frame *); libwebsock_client_state *clients_HEAD; void *user_data; unsigned int max_frame_payload_size; } libwebsock_context;

However, on line 202 in api.c we have :
ctx->socketfd = sockfd; // Set the socketfd on the context so user can thread the context

which generates a compiler warning:
api.c:202:19: warning: assignment makes pointer from integer without a cast [enabled by default]

Shouldn't ctx->socketfd be defined as evutil_socket_t socketfd
rather than ev_uintptr_t *socketfd?

This is the only place where ctx-socketfd is use AFAIK.

continuous crash seen on ubuntu Raspberry pi

#0 0xb6ed8462 in libwebsock_cleanup_thread_list (sock=,
what=, arg=0xb6001bf8) at /usr/include/pthread.h:1167
wrapper = 0xb6001bf8
tinfo = 0x1647008
current = 0x1647008
current_thread = <error reading variable current_thread (Cannot access memory
at address 0x50000)>
state = 0x164de58
#1 0xb6b63fd0 in ?? () from /lib/arm-linux-gnueabihf/libevent-2.1.so.7
No symbol table info available.
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

Deadlock on libwebsock_post_shutdown_cleanup

Hi,

first of all, great work! The API is super-easy to use, and it was quite trivial integrating it in my project to handle concurrent websocket connections.

Anyway, every time a websocket client closes a connection in my project, I get a deadlock related to the libwebsock stuff. That is, everything else works, but libwebsock won't handle new connections, and won't send/receive any more data from connections that were already being handled before.

Debugging with gdb, it looks like the issue is in libwebsock_post_shutdown_cleanup:

    thread_return=thread_return@entry=0x0) at pthread_join.c:92
#1  0x00007fb60d2735dd in libwebsock_post_shutdown_cleanup (

fd=, what=, arg=0x80e490) at websock.c:251

#2  0x00000035c74100e2 in event_base_loop () from /lib64/libevent-2.0.so.5
#3  0x00007fb60d2757eb in libwebsock_wait (ctx=0x80dee0) at api.c:141
#4  0x0000000000413947 in main (argc=1, argv=0x7fffa6cb4598) at janus.c:2525```

that is, in the cleanup code a pthread_join that never returns. What could be the issue? Is there anything I can provide you with to understand what may be the cause? Can it be some libevent issue rather than in libwebsock?

I should clarify that, for each new client, I spawn a new thread that is solely responsible for sending data to it: that is, there's a single thread that writes on a specific connection, while I leave the reading part to the callbacks defined by the library itself.

Thanks!

./configure fails due to missing pthread

When I try to compile this library on Debian Jessie (testing) I get the following error while configure:

checking for pthread_create in -lpthread... no
pthreads required, failing

I have pthread installed, and compiling multithreaded applications with -lpthread works like expected.
Could you have a look at your configure scripts to figure out how to fix this?

Kind regards,

Tino

crashing...

libwebsock listening on port 8085
onopen: 7
dyld: lazy symbol binding failed: Symbol not found: _decode
Referenced from: /usr/local/lib/libwebsock.1.dylib
Expected in: flat namespace

dyld: Symbol not found: _decode
Referenced from: /usr/local/lib/libwebsock.1.dylib
Expected in: flat namespace

Trace/BPT trap: 5
sh-3.2#

Any ideas?

without-ssl does not work

On OSX 10.11 (El Capitan), when running "./configure --without-ssl" as instructed to build without openssl, the source code still tries to:
#include <openssl/ssl.h>
because the generated file websock_config.h contains:
`
/* src/websock_config.h. Generated from websock_config.h.in by configure. */
#ifndef WEBSOCK_CONFIG_H
#define WEBSOCK_CONFIG_H 1

#define WEBSOCK_PACKAGE_STRING "libwebsock 1.0.7"
#define WEBSOCK_PACKAGE_VERSION "1.0.7"
#define WEBSOCK_PACKAGE_NAME "libwebsock"
#define WEBSOCK_HAVE_SSL @WEBSOCK_HAVE_SSL@
#endif
`
This results in build failure because openssl is not found. Changing
#define WEBSOCK_HAVE_SSL @WEBSOCK_HAVE_SSL@
to
#undef WEBSOCK_HAVE_SSL
results in a successful build.

However, without-ssl should have worked, unless I am doing something wrong.

Add streaming mechanism

Need to provide API for sending fragmented messages. Useful when we don't know size of data to be sent. Need to think about how to implement this. Trickiest part will be holding some sort of send lock so as not to attempt to send other messages while we're sending a fragmented message.

Compilation failing on v1.0.7

Hi,
I'm consistently finding ./configure to fail while testing for evthread_use_pthreads:

checking for evthread_use_pthreads in -levent_pthreads... no
libevent_pthreads required, failing

However, it works if I run it like this:

LIBS=-levent ./configure

I'm guessing "-levent" should be added somewhere in the configure generation chain, as libevent generates libevent.so with the core contents and libevent_pthreads.so with the pthread related code.

I'm running Fedora 18, with libevent 2.0.21-stable.
Also tried it on Fedora 20 with the same results.
Hope this helps!

Payden Sutherland's Death

The lack of correspondence from Payden regarding this project had me very curious, so I began digging to see if I could find out where he mysteriously disappeared to. I figured at worst case, he was tied up working and just overall busy. However, I found out today that he actually passed away on September 24, 2014. It's very sad, to say the least, as he leaves behind some very innovative and creative work. As such, don't expect him to be answering any questions on here moving forward (obviously).

I'm going to pick up where he left off and begin working on the library. His implementation of libwebsock is far superior to any out there right now in terms of ease of use and functionality. It makes sense to keep its development alive and going. With it, he'll technically live forever. Anyone interested in joining in, please contact me via [email protected] - I'll be bringing up a new Git repository shortly today.

Thank you, Payden, for your exemplary work and incredible creativity. RIP.

A error due to the unreleased lock &global_alloc_free_lock before program exit

First, thank you for your checking! The lock &global_alloc_free_lock is not released if the !alloc is satisfied. The fix is to insert pthread_mutex_unlock(&global_alloc_free_lock); before exit(1).

Note that it is not a very harmful bug( e.g., resource leak, deadlock) since the program exits and all of the program resources will be cleaned up by some common OS. However, other OS systems (e.g., embedded systems) do not automatically free some resources at the exit. It is a good manner for resource management. Adding the unlock statement adds symmetry, so the code looks better. Also, the debugger would not warn this case : )

pthread_mutex_lock(&global_alloc_free_lock);

void *
lws_calloc(size_t size)
{
   pthread_mutex_lock(&global_alloc_free_lock);
        ...;
  if (!alloc) {
    fprintf(stderr, "Failed calloc!  Exiting.\n");
    exit(-1); //exit without releasing
  }
  pthread_mutex_unlock(&global_alloc_free_lock);
  ...;
  return alloc;
}

Best,

missing include <stdlib.h> in util.c

When building util.c, compiler generates many warnings:
util.c: In function ‘lws_calloc’:
util.c:84:5: warning: implicit declaration of function ‘calloc’ [-Wimplicit-function-declaration]
util.c:84:19: warning: incompatible implicit declaration of built-in function ‘calloc’ [enabled by default]
util.c:87:9: warning: implicit declaration of function ‘exit’ [-Wimplicit-function-declaration]
util.c:87:9: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
util.c: In function ‘lws_malloc’:
util.c:103:5: warning: implicit declaration of function ‘malloc’ [-Wimplicit-function-declaration]
util.c:103:19: warning: incompatible implicit declaration of built-in function ‘malloc’ [enabled by default]
util.c:106:9: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
util.c: In function ‘lws_free’:
util.c:122:2: warning: implicit declaration of function ‘free’ [-Wimplicit-function-declaration]
util.c:122:2: warning: incompatible implicit declaration of built-in function ‘free’ [enabled by default]
util.c: In function ‘lws_realloc’:
util.c:136:5: warning: implicit declaration of function ‘realloc’ [-Wimplicit-function-declaration]
util.c:136:17: warning: incompatible implicit declaration of built-in function ‘realloc’ [enabled by default]
util.c:139:9: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]

Adding #include <stdlib.h> to beginning of util.c eliminates these warning.

Sample with libevent

Is there a sample that lets libwebsock coexist with a libevent http server? And shares the same base etc? Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.