169570cc8Sopenharmony_ci/*
269570cc8Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
369570cc8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
469570cc8Sopenharmony_ci * you may not use this file except in compliance with the License.
569570cc8Sopenharmony_ci * You may obtain a copy of the License at
669570cc8Sopenharmony_ci *
769570cc8Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
869570cc8Sopenharmony_ci *
969570cc8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1069570cc8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1169570cc8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1269570cc8Sopenharmony_ci * See the License for the specific language governing permissions and
1369570cc8Sopenharmony_ci * limitations under the License.
1469570cc8Sopenharmony_ci */
1569570cc8Sopenharmony_ci
1669570cc8Sopenharmony_ci#ifndef APPSPAWN_SERVICE_H
1769570cc8Sopenharmony_ci#define APPSPAWN_SERVICE_H
1869570cc8Sopenharmony_ci
1969570cc8Sopenharmony_ci#include <limits.h>
2069570cc8Sopenharmony_ci#include <stdbool.h>
2169570cc8Sopenharmony_ci#include <unistd.h>
2269570cc8Sopenharmony_ci
2369570cc8Sopenharmony_ci#include "appspawn.h"
2469570cc8Sopenharmony_ci#include "appspawn_hook.h"
2569570cc8Sopenharmony_ci#include "appspawn_msg.h"
2669570cc8Sopenharmony_ci#include "appspawn_server.h"
2769570cc8Sopenharmony_ci#include "appspawn_utils.h"
2869570cc8Sopenharmony_ci#include "list.h"
2969570cc8Sopenharmony_ci#include "loop_event.h"
3069570cc8Sopenharmony_ci
3169570cc8Sopenharmony_ci#ifdef __cplusplus
3269570cc8Sopenharmony_ciextern "C" {
3369570cc8Sopenharmony_ci#endif
3469570cc8Sopenharmony_ci
3569570cc8Sopenharmony_ci#ifdef APPSPAWN_TEST
3669570cc8Sopenharmony_ci#define MAX_WAIT_MSG_COMPLETE (5 * 100)  // 500ms
3769570cc8Sopenharmony_ci#define COLD_CHILD_RESPONSE_TIMEOUT 5
3869570cc8Sopenharmony_ci#define WAIT_CHILD_RESPONSE_TIMEOUT 3  //3s
3969570cc8Sopenharmony_ci#else
4069570cc8Sopenharmony_ci#define MAX_WAIT_MSG_COMPLETE (5 * 1000)  // 5s
4169570cc8Sopenharmony_ci#define COLD_CHILD_RESPONSE_TIMEOUT 5
4269570cc8Sopenharmony_ci#define WAIT_CHILD_RESPONSE_TIMEOUT 3  //3s
4369570cc8Sopenharmony_ci#endif
4469570cc8Sopenharmony_ci
4569570cc8Sopenharmony_citypedef struct TagAppSpawnMsgNode AppSpawnMsgNode;
4669570cc8Sopenharmony_citypedef struct TagAppSpawnMsgReceiverCtx {
4769570cc8Sopenharmony_ci    uint32_t nextMsgId;              // 校验消息id
4869570cc8Sopenharmony_ci    uint32_t msgRecvLen;             // 已经接收的长度
4969570cc8Sopenharmony_ci    TimerHandle timer;               // 测试消息完整
5069570cc8Sopenharmony_ci    int fdCount;
5169570cc8Sopenharmony_ci    int fds[APP_MAX_FD_COUNT];
5269570cc8Sopenharmony_ci    AppSpawnMsgNode *incompleteMsg;  // 保存不完整的消息,额外保存消息头信息
5369570cc8Sopenharmony_ci} AppSpawnMsgReceiverCtx;
5469570cc8Sopenharmony_ci
5569570cc8Sopenharmony_citypedef struct TagAppSpawnConnection {
5669570cc8Sopenharmony_ci    uint32_t connectionId;
5769570cc8Sopenharmony_ci    TaskHandle stream;
5869570cc8Sopenharmony_ci    AppSpawnMsgReceiverCtx receiverCtx;
5969570cc8Sopenharmony_ci} AppSpawnConnection;
6069570cc8Sopenharmony_ci
6169570cc8Sopenharmony_citypedef struct TagAppSpawnStartArg {
6269570cc8Sopenharmony_ci    RunMode mode;
6369570cc8Sopenharmony_ci    uint32_t moduleType;
6469570cc8Sopenharmony_ci    const char *socketName;
6569570cc8Sopenharmony_ci    const char *serviceName;
6669570cc8Sopenharmony_ci    uint32_t initArg : 1;
6769570cc8Sopenharmony_ci} AppSpawnStartArg;
6869570cc8Sopenharmony_ci
6969570cc8Sopenharmony_citypedef struct {
7069570cc8Sopenharmony_ci    char *serverName;
7169570cc8Sopenharmony_ci    AppSpawnStartArg arg;
7269570cc8Sopenharmony_ci} AppSpawnStartArgTemplate;
7369570cc8Sopenharmony_ci
7469570cc8Sopenharmony_cipid_t NWebSpawnLaunch(void);
7569570cc8Sopenharmony_civoid NWebSpawnInit(void);
7669570cc8Sopenharmony_ciAppSpawnContent *StartSpawnService(const AppSpawnStartArg *arg, uint32_t argvSize, int argc, char *const argv[]);
7769570cc8Sopenharmony_civoid AppSpawnDestroyContent(AppSpawnContent *content);
7869570cc8Sopenharmony_ci
7969570cc8Sopenharmony_ci#ifdef __cplusplus
8069570cc8Sopenharmony_ci}
8169570cc8Sopenharmony_ci#endif
8269570cc8Sopenharmony_ci#endif  // APPSPAWN_SERVICE_H
83