static int ws_service_callback(struct lws *wsi,
		enum lws_callback_reasons reason, void *user,
		void *in, size_t len)
{

	switch (reason) {

		case LWS_CALLBACK_ESTABLISHED:
			printf(KYEL"[Main Service] Connection established\n"RESET);
			break;

		/* If receive a data from client*/
		case LWS_CALLBACK_RECEIVE:
			printf(KCYN_L"[Main Service] Server recvived:%s\n"RESET,(char *)in);

			/* echo back to client*/
			websocket_write_back(wsi ,(char *)in, -1);

			break;
		case LWS_CALLBACK_CLOSED:
			printf(KYEL"[Main Service] Client close.\n"RESET);
			break;

		default:
			break;
	}

	return 0;
}
Beispiel #2
0
int main(int argc, char *argv[])
{
    //setup signal handler to detect if the mirror
    //wants to terminate this program.
    struct sigaction action;
    memset(&action, 0, sizeof(struct sigaction));

    action.sa_handler = terminate;
    sigaction(SIGTERM, &action, NULL);

    //start of program here
    lws_set_log_level(0, NULL);

    char protoName[1024];
    strcpy(protoName, argv[1]);

    struct lws_context *context = NULL;
    struct lws_context_creation_info info;
    struct lws *wsi = NULL;
    struct lws_protocols protocol;
    struct lws_client_connect_info i;

    memset(&info, 0, sizeof info);
    info.port = CONTEXT_PORT_NO_LISTEN;
    info.protocols = &protocol;
    info.max_http_header_pool = 128;
    info.gid = -1;
    info.uid = -1;

    protocol.name  = protoName;
    protocol.callback = ws_service_callback;
    protocol.per_session_data_size = sizeof(struct session_data);
    protocol.rx_buffer_size = 125;

    printf("Creating context\n");
    context = lws_create_context(&info);
    printf(KRED"[Main] context created.\n"RESET);

    if (context == NULL) {
        printf(KRED"[Main] context is NULL.\n"RESET);
        return -1;
    }

    printf("Connecting...\n");

    const char *prot;
    if (lws_parse_uri(server, &prot, &i.address, &i.port, &i.path)) {
        printf("Client.c: Error parsing uri\n");
        exit(1);
    }


    i.port = 5000;
    i.context = context;
    i.ssl_connection = 0;
    i.host = i.address;
    i.origin = i.address;
    i.ietf_version_or_minus_one = -1;
    i.protocol = protoName;


    //struct lws *wsi = NULL;
    const struct timespec sleepTime = {
            0, NANOSECOND/FPS
    };

    char command[256];
    int left = 0, dir = 1;

    if (!connection_flag || !wsi) {
        wsi = lws_client_connect_via_info(&i);
    }

    while(!destroy_flag) {

        lws_service(context, 0);

        if (!connection_flag || !wsi) continue;
        left += (dir * SPEED);

        if (left > SCREEN_WIDTH) dir = -dir;
        else if (left < 0) dir = -dir;

        sprintf(command, "{\"command\":\"setcss\",\"data\":\"left=%dpx\"}", left);
        websocket_write_back(wsi, command, -1);
        nanosleep(&sleepTime, NULL);
    }

    lws_context_destroy(context);
    return 0;
}
int
callback_lws_mirror(struct lws *wsi, enum lws_callback_reasons reason,
		    void *user, void *in, size_t len)
{
	struct per_session_data__lws_mirror *pss =
			(struct per_session_data__lws_mirror *)user;
	int n, m;
int client_sockfd=lws_get_socket_fd(wsi);
	switch (reason) {

	case LWS_CALLBACK_ESTABLISHED:
		lwsl_notice("%s: LWS_CALLBACK_ESTABLISHED\n", __func__);
		pss->ringbuffer_tail = ringbuffer_head;
		pss->wsi = wsi;
		break;

	case LWS_CALLBACK_PROTOCOL_DESTROY:
		lwsl_notice("%s: mirror protocol cleaning up\n", __func__);
		for (n = 0; n < sizeof ringbuffer / sizeof ringbuffer[0]; n++)
			if (ringbuffer[n].payload)
				free(ringbuffer[n].payload);
		break;

	case LWS_CALLBACK_SERVER_WRITEABLE:
		//if (close_testing)
		//	break;

	printf("服务器开始发送数据\n");
	websocket_write_back(wsi, msg,-1);
	// n = lws_write(wsi, (char*)in + LWS_SEND_BUFFER_PRE_PADDING, len, LWS_WRITE_TEXT);

		// while (pss->ringbuffer_tail != ringbuffer_head) {
		// 	m = ringbuffer[pss->ringbuffer_tail].len;
		// 	n = lws_write(wsi, (unsigned char *)
		// 		   ringbuffer[pss->ringbuffer_tail].payload +
		// 		   LWS_PRE, m, LWS_WRITE_TEXT);
			
			
		// 	printf("the n is %d, the cilent fd is %d\n",n,client_sockfd);
		// 	if (n < 0) {
		// 		lwsl_err("ERROR %d writing to mirror socket\n", n);
		// 		return -1;
		// 	}
		// 	if (n < m)
		// 		lwsl_err("mirror partial write %d vs %d\n", n, m);

		// 	if (pss->ringbuffer_tail == (MAX_MESSAGE_QUEUE - 1))
		// 		pss->ringbuffer_tail = 0;
		// 	else
		// 		pss->ringbuffer_tail++;

		// 	// if (((ringbuffer_head - pss->ringbuffer_tail) &
		// 	//     (MAX_MESSAGE_QUEUE - 1)) == (MAX_MESSAGE_QUEUE - 15))
		// 	// 	lws_rx_flow_allow_all_protocol(lws_get_context(wsi),
		// 	// 		       lws_get_protocol(wsi));

		// 	// if (lws_send_pipe_choked(wsi)) {
		// 	// 	lws_callback_on_writable(wsi);
		// 	// 	break;
		// 	// }
		// }
		break;

	case LWS_CALLBACK_RECEIVE:
	client_sockfd=lws_get_socket_fd(wsi);
	lwsl_notice("接收到客户端 %d 的信息=[%s]\n",client_sockfd, (const char*)in);
	memcpy(msg,in,strlen(in));
		if (((ringbuffer_head - pss->ringbuffer_tail) &
		    (MAX_MESSAGE_QUEUE - 1)) == (MAX_MESSAGE_QUEUE - 1)) {
			lwsl_err("dropping!\n");
			goto choke;
		}

		if (ringbuffer[ringbuffer_head].payload)
			free(ringbuffer[ringbuffer_head].payload);

		ringbuffer[ringbuffer_head].payload = malloc(LWS_PRE + len);
		ringbuffer[ringbuffer_head].len = len;
		memcpy((char *)ringbuffer[ringbuffer_head].payload +
		       LWS_PRE, in, len);
		if (ringbuffer_head == (MAX_MESSAGE_QUEUE - 1))
			ringbuffer_head = 0;
		else
			ringbuffer_head++;

		if (((ringbuffer_head - pss->ringbuffer_tail) &
		    (MAX_MESSAGE_QUEUE - 1)) != (MAX_MESSAGE_QUEUE - 2))
			goto done;

choke:
		lwsl_notice("LWS_CALLBACK_RECEIVE: throttling %p\n", wsi);
		lws_rx_flow_control(wsi, 0);

done:
	
	printf("lws_callback_on_writable_all_protocol, the client fd is %d\n", client_sockfd);
		lws_callback_on_writable_all_protocol(lws_get_context(wsi),
						      lws_get_protocol(wsi));
		break;

	/*
	 * this just demonstrates how to use the protocol filter. If you won't
	 * study and reject connections based on header content, you don't need
	 * to handle this callback
	 */

	case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
		//dump_handshake_info(wsi);
		/* you could return non-zero here and kill the connection */
		break;

	default:
		break;
	}

	return 0;
}