OpenHarmony源码分析之分布式软总线:trans_service/tcp_session.c

一、概述

tcp_session.c文件提供的接口功能主要是创建一个TCP会话并返回其地址,在tcp_session.h文件中还提供了会话的属性结构体,本文将对这两个文件进行详细分析。

二、源码分析

  1. tcp_session.h
arduino 复制代码
/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef TCP_SESSION_H
#define TCP_SESSION_H
#include "comm_defs.h"
#include "common_info_manager.h"
#define NAME_LENGTH         64      //名称长度
#define SESSION_KEY_LENGTH  32      //会话密钥长度
/*会话数据包序列号节点*/
typedef struct {
    List head;
    int seqNum;
} SessionSeqNumNode;
/*tcp会话相关属性结构体*/
typedef struct {
    char sessionName[NAME_LENGTH];//会话名称
    char deviceId[MAX_DEV_ID_LEN];//设备id
    char groupId[NAME_LENGTH];//组id
    char sessionKey[SESSION_KEY_LENGTH];//会话密钥
    long seqNum;//节点序号
    int fd;//通信套接字fd
    int busVersion;//总线版本
    int routeType;//路由类型
    bool isAccepted;
    List *seqNumList;//数据包序号链表,用于记录数据包的序列号
} TcpSession;
TcpSession* CreateTcpSession(void);
#endif // TCP_SESSION_H

DD一下: 欢迎大家关注公众号<程序猿百晓生>,可以了解到一下知识点。

erlang 复制代码
1.OpenHarmony开发基础
2.OpenHarmony北向开发环境搭建
3.鸿蒙南向开发环境的搭建
4.鸿蒙生态应用开发白皮书V2.0 & V3.0
5.鸿蒙开发面试真题(含参考答案) 
6.TypeScript入门学习手册
7.OpenHarmony 经典面试题(含参考答案)
8.OpenHarmony设备开发入门【最新版】
9.沉浸式剖析OpenHarmony源代码
10.系统定制指南
11.【OpenHarmony】Uboot 驱动加载流程
12.OpenHarmony构建系统--GN与子系统、部件、模块详解
13.ohos开机init启动流程
14.鸿蒙版性能优化指南
.......
  1. tcp_session.c
c 复制代码
/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "tcp_session.h"
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/socket.h>
#include "os_adapter.h"
/*
函数功能:创建一个新的tcp会话并进行相关属性初始化
函数参数:无
函数返回值:
    成功:返回tcp会话结构体地址
    失败:返回NULL
详细:
*/
TcpSession *CreateTcpSession(void)
{
    TcpSession *tcpSession = (TcpSession *)malloc(sizeof(TcpSession));//申请会话地址空间
    if (tcpSession == NULL) {
        return NULL;
    }
    if (strcpy_s(tcpSession->sessionName, NAME_LENGTH, "softbus_Lite_unknown") != 0) {//为新会话初始化名字
        SOFTBUS_PRINT("[TRANS] CreateTcpSession cpy busname fail\n");
        free(tcpSession);
        return NULL;
    }
    //初始化TCP会话结构体的相关属性:
    (void)memset_s(tcpSession->deviceId, MAX_DEV_ID_LEN, 0, MAX_DEV_ID_LEN);//清空设备id空间
    (void)memset_s(tcpSession->groupId, NAME_LENGTH, 0, NAME_LENGTH);//清空组id空间
    (void)memset_s(tcpSession->sessionKey, SESSION_KEY_LENGTH, 0, SESSION_KEY_LENGTH);//清空会话密钥空间
    tcpSession->seqNum = 0;//初始化节点序号为0
    tcpSession->fd = -1;//初始化套接字fd为-1
    tcpSession->busVersion = 0;
    tcpSession->routeType = 0;
    tcpSession->isAccepted = false;
    tcpSession->seqNumList = malloc(sizeof(List));//申请链表空间
    if (tcpSession->seqNumList == NULL) {
        free(tcpSession);
        return NULL;
    }
    ListInitHead(tcpSession->seqNumList);//初始化链表头指针
    return tcpSession;
}
相关推荐
li理28 分钟前
鸿蒙Image Kit深度解析:从图片解码到高级特效处理
harmonyos
li理31 分钟前
鸿蒙相机开发中篇:自定义 UI 与拍摄控制
harmonyos
virtuousOne4 小时前
Kafka基础
分布式·kafka
虫小宝4 小时前
Java分布式架构下的电商返利APP技术选型与架构设计实践
java·分布式·架构
007php0075 小时前
百度面试题解析:Zookeeper、ArrayList、生产者消费者模型及多线程(二)
java·分布式·zookeeper·云原生·职场和发展·eureka·java-zookeeper
waving-black5 小时前
windows系统下安装测试kafka
windows·分布式·kafka
深思慎考5 小时前
RabbitMQ 入门:基于 AMQP-CPP 的 C++ 实践指南与二次封装
开发语言·c++·分布式·rabbitmq·api
Andya_net5 小时前
Java | 基于redis实现分布式批量设置各个数据中心的服务器配置方案设计和代码实践
java·服务器·分布式
鸿蒙小白龙5 小时前
OpenHarmony 与 HarmonyOS 的 NAPI 开发实战对比:自上而下与自下而上的差异解析
harmonyos·鸿蒙·鸿蒙系统·open harmony
喵手7 小时前
【参赛心得】从“碰一碰”到“服务流转”:HarmonyOS创新赛金奖作品“智游文博”全流程复盘!
华为·harmonyos·鸿蒙应用开发·1024征文