Beispiel #1
0
        void InitTcpServer()
        {
            remoteTcpPeerMap = new Dictionary <long, IRemoteTcpPeer>();
            ipEndPointIDMap  = new Dictionary <IPEndPoint, long>();
            Debug.Log("ManagedThreadId:" + Thread.CurrentThread.ManagedThreadId);
            server = new AsyncNetTcpServer(12100);
            server.ConnectionClosed += (sender, e) => //当特定客户端/对等端连接关闭时触发
            {
            };
            server.ConnectionEstablished += (sender, e) => //当新客户机/对等点连接到服务器时触发
            {
                IRemoteTcpPeer peer = e.RemoteTcpPeer;
                Debug.Log($"New connection from [{peer.IPEndPoint}]");
                Debug.Log("ManagedThreadId:" + Thread.CurrentThread.ManagedThreadId);
                long id = IdGenerater.GenerateId();
                remoteTcpPeerMap[id]             = peer;
                ipEndPointIDMap[peer.IPEndPoint] = id;
            };

            server.FrameArrived += (sender, e) =>//TCP帧从特定客户端/对等端到达时触发
            {
                Console.WriteLine($"Server received: {System.Text.Encoding.UTF8.GetString(e.FrameData)}");
                var    peer      = e.RemoteTcpPeer;
                byte[] framedata = e.FrameData;
                int    offser    = 0;
                int    remain    = framedata.Length;
                int    len       = 0;
                int    cmd       = 0;
                while (remain > offser)
                {
                    offser += Utils.Decode32(framedata, offser, ref len);
                    offser += Utils.Decode32(framedata, offser, ref cmd);
                    //offser += Utils.Decode32(framedata, offser, ref cmd);
                    dispatcher[cmd]?.Execute(framedata, offser, len, ipEndPointIDMap[e.RemoteTcpPeer.IPEndPoint]);
                    offser += len;
                }
            };
            server.RemoteTcpPeerExceptionOccured += (sender, e) =>//当处理特定客户机/对等点时发生错误时触发
            {
            };
            server.ServerExceptionOccured += (sender, e) =>//当服务器出现问题时触发
            {
            };
            server.ServerStarted += (sender, e) => //当服务器开始运行时触发
            {
                Console.WriteLine($"Server started on port: " + $"{e.ServerPort}");
            };
            server.UnhandledExceptionOccured += (sender, e) => //当未处理的错误发生时触发——例如,当事件订阅程序抛出异常时
            {
            };

            server.StartAsync(CancellationToken.None);
        }
        public async void StartListening()
        {
            var server = new AsyncNetTcpServer(CableCloudConfig.Port);

            server.ServerStarted += (s, e) => Console.WriteLine($"Server started on port: " +
                                                                $"{e.ServerPort}");
            server.ConnectionEstablished += (s, e) =>
            {
            };
            server.FrameArrived += (s, e) =>
            {
                var message = _objectSerializerService.Deserialize(e.FrameData);

                switch (message)
                {
                case string init:
                {
                    var parts = init.Split(' ');
                    _logService.LogInfo($"Connected with {parts[1]}");

                    if (parts[1].StartsWith("CCRC"))
                    {
                        _ccrcList.Add(parts[1]);
                    }
                    AddToTranslationDictionary(e.RemoteTcpPeer, parts);
                    break;
                }

                case ISignalingMessage signalingMessage:
                {
                    ProcessMessage(e, signalingMessage);
                    break;
                }

                case EonPacket eonPacket:
                {
                    ProcessPackage(e.RemoteTcpPeer, eonPacket);
                    break;
                }
                }
            };
            await server.StartAsync(CancellationToken.None);
        }
Beispiel #3
0
        public async void StartListening(string ip, int port)
        {
            var server = new AsyncNetTcpServer(port);

            server.ServerStarted += (s, e) => Console.WriteLine($"Server started on port: " +
                                                                $"{e.ServerPort}");
            server.ConnectionEstablished += (s, e) =>
            {
            };
            server.FrameArrived += (s, e) =>
            {
                var message = _objectSerializerService.Deserialize(e.FrameData);

                switch (message)
                {
                case string init:
                {
                    var parts = init.Split(' ');
                    _logService.LogInfo($"Connected with {parts[1]}");

                    if (parts[1].StartsWith("CCRC"))
                    {
                        _domainCcrcName = parts[1];
                    }
                    else if (parts[1].StartsWith("NCC"))
                    {
                        _secondNccName = parts[1];
                    }
                    AddToTranslationDictionary(e.RemoteTcpPeer, parts);
                    break;
                }

                case ISignalingMessage signalingMessage:
                {
                    ProcessMessage(signalingMessage);
                    break;
                }
                }
            };
            await server.StartAsync(CancellationToken.None);
        }