fix server
This commit is contained in:
parent
c24007efc3
commit
c6e7f7669e
@ -31,6 +31,14 @@
|
||||
-define(PACKET_PUBLISH, 16#03).
|
||||
-define(PACKET_PUBLISH_RESPONSE, 16#04).
|
||||
|
||||
%% 消息体类型
|
||||
-define(PACKET_REQUEST, 16#01).
|
||||
-define(PACKET_RESPONSE, 16#02).
|
||||
|
||||
%% 服务器端推送消息
|
||||
-define(PACKET_PUBLISH, 16#03).
|
||||
-define(PACKET_PUBLISH_RESPONSE, 16#04).
|
||||
|
||||
%% 主机端上报数据类型标识
|
||||
%% 建立到websocket的register关系
|
||||
-define(METHOD_AUTH, 16#00).
|
||||
@ -42,8 +50,6 @@
|
||||
-define(METHOD_FEEDBACK_STEP, 16#05).
|
||||
|
||||
-define(METHOD_EVENT, 16#07).
|
||||
%% ai识别的事件上报
|
||||
-define(METHOD_AI_EVENT, 16#08).
|
||||
|
||||
-define(METHOD_PHASE, 16#09).
|
||||
|
||||
|
||||
40
apps/iot/src/http/http_server.erl
Normal file
40
apps/iot/src/http/http_server.erl
Normal file
@ -0,0 +1,40 @@
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @author anlicheng
|
||||
%%% @copyright (C) 2025, <COMPANY>
|
||||
%%% @doc
|
||||
%%%
|
||||
%%% @end
|
||||
%%% Created : 08. 5月 2025 13:00
|
||||
%%%-------------------------------------------------------------------
|
||||
-module(http_server).
|
||||
-author("anlicheng").
|
||||
|
||||
%% API
|
||||
-export([start/0]).
|
||||
|
||||
%% 启动http服务
|
||||
start() ->
|
||||
{ok, Props} = application:get_env(iot, http_server),
|
||||
Acceptors = proplists:get_value(acceptors, Props, 50),
|
||||
MaxConnections = proplists:get_value(max_connections, Props, 10240),
|
||||
Backlog = proplists:get_value(backlog, Props, 1024),
|
||||
Port = proplists:get_value(port, Props),
|
||||
|
||||
Dispatcher = cowboy_router:compile([
|
||||
{'_', [
|
||||
{"/host/[...]", http_protocol, [host_handler]},
|
||||
{"/device/[...]", http_protocol, [device_handler]},
|
||||
{"/service/[...]", http_protocol, [service_handler]}
|
||||
]}
|
||||
]),
|
||||
|
||||
TransOpts = [
|
||||
{port, Port},
|
||||
{num_acceptors, Acceptors},
|
||||
{backlog, Backlog},
|
||||
{max_connections, MaxConnections}
|
||||
],
|
||||
|
||||
{ok, Pid} = cowboy:start_clear(http_listener, TransOpts, #{env => #{dispatch => Dispatcher}}),
|
||||
|
||||
lager:debug("[iot_app] the http server start at: ~p, pid is: ~p", [Port, Pid]).
|
||||
@ -17,9 +17,11 @@ start(_StartType, _StartArgs) ->
|
||||
%% 启动mnesia数据库
|
||||
start_mnesia(),
|
||||
|
||||
start_http_server(),
|
||||
%% 启动http服务
|
||||
http_server:start(),
|
||||
|
||||
start_tcp_server(),
|
||||
%% 启动tcp服务
|
||||
tcp_server:start(),
|
||||
|
||||
iot_sup:start_link().
|
||||
|
||||
@ -28,57 +30,6 @@ stop(_State) ->
|
||||
|
||||
%% internal functions
|
||||
|
||||
%% 启动http服务
|
||||
start_http_server() ->
|
||||
{ok, Props} = application:get_env(iot, http_server),
|
||||
Acceptors = proplists:get_value(acceptors, Props, 50),
|
||||
MaxConnections = proplists:get_value(max_connections, Props, 10240),
|
||||
Backlog = proplists:get_value(backlog, Props, 1024),
|
||||
Port = proplists:get_value(port, Props),
|
||||
|
||||
Dispatcher = cowboy_router:compile([
|
||||
{'_', [
|
||||
{"/host/[...]", http_protocol, [host_handler]},
|
||||
{"/device/[...]", http_protocol, [device_handler]},
|
||||
{"/service/[...]", http_protocol, [service_handler]}
|
||||
]}
|
||||
]),
|
||||
|
||||
TransOpts = [
|
||||
{port, Port},
|
||||
{num_acceptors, Acceptors},
|
||||
{backlog, Backlog},
|
||||
{max_connections, MaxConnections}
|
||||
],
|
||||
|
||||
{ok, Pid} = cowboy:start_clear(http_listener, TransOpts, #{env => #{dispatch => Dispatcher}}),
|
||||
|
||||
lager:debug("[iot_app] the http server start at: ~p, pid is: ~p", [Port, Pid]).
|
||||
|
||||
%% 启动tcp服务
|
||||
start_tcp_server() ->
|
||||
{ok, Props} = application:get_env(iot, tcp_server),
|
||||
Acceptors = proplists:get_value(acceptors, Props, 50),
|
||||
MaxConnections = proplists:get_value(max_connections, Props, 10240),
|
||||
Backlog = proplists:get_value(backlog, Props, 1024),
|
||||
Port = proplists:get_value(port, Props),
|
||||
|
||||
TransOpts = [
|
||||
{tcp_options, [
|
||||
binary,
|
||||
{reuseaddr, true},
|
||||
{active, false},
|
||||
{packet, 4},
|
||||
{nodelay, false},
|
||||
{backlog, Backlog}
|
||||
]},
|
||||
{acceptors, Acceptors},
|
||||
{max_connections, MaxConnections}
|
||||
],
|
||||
{ok, _} = esockd:open('iot/tcp_server', Port, TransOpts, {tcp_channel, start_link, []}),
|
||||
|
||||
lager:debug("[iot_app] the tcp server start at: ~p", [Port]).
|
||||
|
||||
%% 启动内存数据库
|
||||
start_mnesia() ->
|
||||
%% 启动数据库
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
%%% @end
|
||||
%%% Created : 11. 1月 2021 上午12:17
|
||||
%%%-------------------------------------------------------------------
|
||||
-module(iot_tcp_channel).
|
||||
-module(tcp_channel).
|
||||
-author("licheng5").
|
||||
-include("iot.hrl").
|
||||
-include("message_pb.hrl").
|
||||
@ -150,11 +150,6 @@ handle_info({tcp, Socket, <<?PACKET_REQUEST, 0:32, ?METHOD_EVENT:8, EventData/bi
|
||||
iot_host:handle(HostPid, {event, Event}),
|
||||
{noreply, State};
|
||||
|
||||
handle_info({tcp, Socket, <<?PACKET_REQUEST, 0:32, ?METHOD_AI_EVENT:8, AIEventData/binary>>}, State = #state{socket = Socket, host_pid = HostPid}) when is_pid(HostPid) ->
|
||||
AIEvent = message_pb:decode_msg(AIEventData, ai_event),
|
||||
iot_host:handle(HostPid, {ai_event, AIEvent}),
|
||||
{noreply, State};
|
||||
|
||||
%% 主机端的消息响应
|
||||
handle_info({tcp, Socket, <<?PACKET_PUBLISH_RESPONSE, PacketId:32, Body/binary>>}, State = #state{socket = Socket, uuid = UUID, inflight = Inflight}) when PacketId > 0 ->
|
||||
lager:debug("[ws_channel] uuid: ~p, get publish response message: ~p, packet_id: ~p", [UUID, Body, PacketId]),
|
||||
37
apps/iot/src/tcp/tcp_server.erl
Normal file
37
apps/iot/src/tcp/tcp_server.erl
Normal file
@ -0,0 +1,37 @@
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @author anlicheng
|
||||
%%% @copyright (C) 2025, <COMPANY>
|
||||
%%% @doc
|
||||
%%%
|
||||
%%% @end
|
||||
%%% Created : 08. 5月 2025 12:58
|
||||
%%%-------------------------------------------------------------------
|
||||
-module(tcp_server).
|
||||
-author("anlicheng").
|
||||
|
||||
%% API
|
||||
-export([start/0]).
|
||||
|
||||
%% 启动tcp服务
|
||||
start() ->
|
||||
{ok, Props} = application:get_env(iot, tcp_server),
|
||||
Acceptors = proplists:get_value(acceptors, Props, 50),
|
||||
MaxConnections = proplists:get_value(max_connections, Props, 10240),
|
||||
Backlog = proplists:get_value(backlog, Props, 1024),
|
||||
Port = proplists:get_value(port, Props),
|
||||
|
||||
TransOpts = [
|
||||
{tcp_options, [
|
||||
binary,
|
||||
{reuseaddr, true},
|
||||
{active, false},
|
||||
{packet, 4},
|
||||
{nodelay, false},
|
||||
{backlog, Backlog}
|
||||
]},
|
||||
{acceptors, Acceptors},
|
||||
{max_connections, MaxConnections}
|
||||
],
|
||||
{ok, _} = esockd:open('iot/tcp_server', Port, TransOpts, {tcp_channel, start_link, []}),
|
||||
|
||||
lager:debug("[iot_app] the tcp server start at: ~p", [Port]).
|
||||
Loading…
x
Reference in New Issue
Block a user