测试

59 阅读2分钟

业务简单科普

互联网,物联网

互联网物联网
定义连接计算机和移动设备等人类使用的设备连接任何物品、设备和传感器等物理实体的网络
应用场景电子商务、社交媒体、在线视频、云计算等智能家居,智慧城市,智慧
网络架构中心服务器为核心边缘计算

什么是边缘计算?

监控

能看多远?

www.bilibili.com/video/BV1aL…

清晰度?

www.bilibili.com/video/BV1ni…

4 分钟

视频平台概览

www.bilibili.com/video/BV1go…

回到什么是边缘计算?

门禁

消费

技术架构

简单架构图

协议

  • RTMP/RTSP:传统流媒体协议
  • HTTP/HTTPS
  • 自定义协议
/*
 * Copyright 2012 The Netty Project
 *
 * The Netty Project licenses this file to you 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:
 *
 *   https://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.
 */
package org.example;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public final class Client {

    static final String HOST = System.getProperty("host", "127.0.0.1");
    static final int PORT = Integer.parseInt(System.getProperty("port", "8080"));

    public static void main(String[] args) throws Exception {
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
                    .channel(NioSocketChannel.class)
                    .option(ChannelOption.TCP_NODELAY, true)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline p = ch.pipeline();
                            p.addLast(new ClientHandler());
                        }
                    });

            ChannelFuture f = b.connect(HOST, PORT).sync();
            f.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully();
        }
    }
}
package org.example;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class ClientHandler extends ChannelInboundHandlerAdapter {


    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        ByteBuf buffer = Unpooled.buffer();
        buffer.writeShort(0b0000000000001101); // 2 字节
        buffer.writeByte(0b11111000);          // 1 字节
        buffer.writeShort(0b0110110001101001); // 2 字节
        buffer.writeInt(0b00110001001100100011001100110100); // 4 字节
        buffer.writeInt(0b00110101001101100011011100111000); // 4 字节
        buffer.writeShort(0b1000001001000000); // 2 字节
        ctx.writeAndFlush(buffer);
        ctx.writeAndFlush(buffer);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        ctx.write(msg);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        // Close the connection when an exception is raised.
        cause.printStackTrace();
        ctx.close();
    }
}
package org.example;

import lombok.Data;

@Data
public class CustomPort {

    // length 0
    private String field1;

    // bitmap 1
    private String field2;

    // name 2
    private String field3;

    // cardNum 3
    private String field4;

    // price 4
    private Integer field5;

    // datetime 5
    private String field6;

    // ....
    private String field7;
    private String field8;


}
package org.example;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;


import java.util.Arrays;
import java.util.List;


public class CustomServerHandler extends ByteToMessageDecoder {

    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        byte[] all = new byte[byteBuf.readableBytes()];
        byteBuf.readBytes(all);
        System.out.println(Arrays.toString(all));
//        byte[] length = new byte[2];
//        byteBuf.readBytes(length);
//        System.out.println(Arrays.toString(length));
//        System.out.println("---------------------");
//        byte[] bytes = new byte[byteBuf.readableBytes()];
//        byteBuf.readBytes(bytes);
//        System.out.println(Arrays.toString(bytes));
    }


}
package org.example;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

public class Server {

    public static void main(String[] args) {
        // Configure the server.
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 100)
                    .handler(new LoggingHandler(LogLevel.INFO))
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline p = ch.pipeline();
                            //p.addLast(new LoggingHandler(LogLevel.INFO));
                            p.addLast(new CustomServerHandler());
                        }
                    });

            // Start the server.
            ChannelFuture f = b.bind(8080).sync();

            // Wait until the server socket is closed.
            f.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            // Shut down all event loops to terminate all threads.
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}