您当前的位置: 首页 >  c++

qianbo_insist

暂无认证

  • 0浏览

    0关注

    399博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

mongoose c++封装

qianbo_insist 发布时间:2021-07-08 09:53:11 ,浏览量:0

mongoose c++封装

封装以后暴露一个回调函数,让main函数调用即可

#pragma once

#include "mongoose.h"
//#include "../CorePhone/TThreadRunable.h"


typedef void(*websocket_callback)(int flag, const char *p);
static websocket_callback g_callback = NULL;
class Service_HTTP //:public TThreadRunable
{
public:
	char *s_http_port = "8000";
	struct mg_serve_http_opts s_http_server_opts;
	struct mg_mgr mgr;
	struct mg_connection * _nc = NULL;
	//websocket_callback  _callback = NULL;
public:
	void setcallback(websocket_callback callback)
	{
		g_callback = callback;
	}

	static int is_websocket(const struct mg_connection *nc) {
		return nc->flags & MG_F_IS_WEBSOCKET;
	}

	static void broadcast(struct mg_connection *nc, const struct mg_str msg) {
		char buf[16];
		char bufname[128];
		const char * pmsg = msg.p;
		int len = msg.len;	
		int namelen = 0;
		int flag = -1;
		if (len > 7)
		{
			memcpy(buf, pmsg, 6);
			buf[6] = '\0';
			if (strcmp(buf, "RECORD") == 0)
				flag = 1;
			else if (strcmp(buf, "FINISH") == 0)
				flag = 2;
			else
				flag = 3;
		}
		if (msg.len > 8)
		{
			char c = *(pmsg + 6);
			if (c == ':')
			{
				namelen = len - 7;
				memcpy(bufname, pmsg + 7, namelen);
				bufname[len - 7] = '\0';
			}
		}
	
        
		//memcpy(buf, msg.p, msg.len);
		//buf[msg.len] = '\0';

		struct mg_connection *c;
		
	
		//以下是广播信息
		//snprintf(buf, sizeof(buf), "%s %.*s", (int)msg.len, msg.p);
		//printf("%s\n", buf); /* Local echo. */
		for (c = mg_next(nc->mgr, NULL); c != NULL; c = mg_next(nc->mgr, c)) {
			//if (c == nc) continue; /* Don't send to the sender. */
			mg_send_websocket_frame(c, WEBSOCKET_OP_TEXT, buf, strlen(buf));
		}
		if (namelen != 0)
		{
			if (g_callback != NULL)
				g_callback(flag, &bufname[0]);
		}
	}

	static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
		switch (ev) {
		case MG_EV_WEBSOCKET_HANDSHAKE_DONE: {
			/* New websocket connection. Tell everybody. */
			//broadcast(nc, mg_mk_str("++ joined"));
			break;
		}
		case MG_EV_WEBSOCKET_FRAME: {
			struct websocket_message *wm = (struct websocket_message *) ev_data;
			/* New websocket message. Tell everybody. */
			struct mg_str d = { (char *)wm->data, wm->size };
			broadcast(nc, d);
			break;
		}
		case MG_EV_HTTP_REQUEST: {
			Service_HTTP *service = (Service_HTTP *)nc->user_data;
			mg_serve_http(nc, (struct http_message *) ev_data, service->s_http_server_opts);
			break;
		}
		case MG_EV_CLOSE: {
			/* Disconnect. Tell everybody. */
			if (is_websocket(nc)) {
				//broadcast(nc, mg_mk_str("-- left"));
			}
			break;
		}
		}
	}
	//广播该视频数据
	void WebSocket_Broadcast(uint8_t * data, int len)
	{
		struct mg_connection *c;
		if (_nc != NULL) {
			for (c = mg_next(_nc->mgr, NULL); c != NULL; c = mg_next(_nc->mgr, c)) {
				if (c == _nc) continue; /* Don't send to the sender. */
				mg_send_websocket_frame(c, WEBSOCKET_OP_BINARY, data, len);
			}
		}
	}
	
public:

	void Stop()
	{
		_stop = 1;
		//TThreadRunable::Stop();
		//Join();
	}
	void Run()
	{
		mg_mgr_init(&mgr, NULL);
		_nc = mg_bind(&mgr, s_http_port, ev_handler);
		_nc->user_data = this;
		mg_set_protocol_http_websocket(_nc);
		s_http_server_opts.document_root = "./public";  // Serve current directory
		s_http_server_opts.enable_directory_listing = "yes";
		//printf("Started on port %s\n", s_http_port);
		while (!_stop) {
			mg_mgr_poll(&mgr, 200);
		}
		mg_mgr_free(&mgr);

	}
private:
	int _stop = 0;
};
使用

其中读配置文件等等就不贴出代码了,这个我们把配置写进代码就行,可以去掉,Mp4WrapperNet.h这个也可以去掉,关注回调函数就好

#include 
#include "Mp4WrapperNet.h"
#include "time.h"
#include 
#include 
#include 
#include "service_http.h"
#include 
#include 
#include "rrconfig.h"
using namespace std;
//int gStopProcess = 0;

//Mp4Wrapper g_wrapper;
Service_HTTP HTTPServer;

static string g_IP;

static string gettime_now()
{
	char buff[64];
	//char buff2[256];
	time_t t = time(NULL);
	strftime(buff, sizeof(buff), "%Y_%m_%d_%H_%M_%S", localtime(&t));
	//printf("%s\n", buff);
	return buff;
}
static void callback(int flag, const char * name)
{
	switch (flag)
	{
	case 1:
	{
		if (g_IP.empty() || g_IP == "" )
		{
			cout             
关注
打赏
1663161521
查看更多评论
0.0433s