Exemplo n.º 1
0
        private bool CreateInstance(TcpClient tcp)
        {
            try
            {
                IPEndPoint ipEndPoint = tcp.Client.RemoteEndPoint as IPEndPoint;
                if (ipEndPoint == null)
                {
                    return(false);
                }
                string clientIp = ipEndPoint.ToString();
                if (ClientInstance.ContainsKey(clientIp))
                {
                    ClientInstance.Remove(clientIp);
                }
                // 建立玩家peer實體
                ClientNode cNode = _SocketServer.GetPeer(this, ipEndPoint, _SocketServer);
                //註冊到 mListener 中,讓他的 Receive 功能能被叫
                TCPInstance instance = new TCPInstance(this, cNode, tcp);
                //註冊到 mListener 中,讓他的 Receive 功能能被叫
                ClientInstance.Add(clientIp, instance);
                //成功加入後傳送 Connect 事件給 Client
                byte[] packet = new byte[] { 1 };
                tcp.GetStream().Write(packet, 0, packet.Length);
                cNode.Initialize();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(false);
            }

            return(true);
        }
Exemplo n.º 2
0
        public void CreateBasicNetworkTest()
        {
            var localhost = "127.0.0.1";
            var maxAtmCellsInCableCloudMessage = 100;

            var portA = 1;
            var port1 = 2;
            var port2 = 3;
            var port3 = 4;
            var port4 = 5;
            var portB = 6;

            var cableCloud = new CableCloud(10000);

            cableCloud.UpdateState += (sender, state) => Console.WriteLine("CC: " + state);
            cableCloud.StartListening();

            var signallingCloud = new SignallingCloud(20000);

            signallingCloud.UpdateState += (sender, state) => Console.WriteLine("SC: " + state);
            signallingCloud.StartListening();

            var nms = new NetworkManagementSystem(6666);

            nms.UpdateState += (sender, state) => Console.WriteLine("NMS:" + state);
            nms.StartListening();

            Thread.Sleep(1000);

            var nameServer = new NameServer(localhost, 20000);

            nameServer.UpdateState += (sender, state) => Console.WriteLine("NS " + state);
            nameServer.Initialize();

            var sspcs1 = new StepByStepPathComputationServer(
                new NetworkAddress(1),
                localhost,
                20000
                );

            sspcs1.UpdateState += (sender, state) => Console.WriteLine("SSPCS1: " + state);
            sspcs1.Initialize();

            var sspcs2 = new StepByStepPathComputationServer(
                new NetworkAddress(2),
                localhost,
                20000
                );

            sspcs2.UpdateState += (sender, state) => Console.WriteLine("SSPCS2: " + state);
            sspcs2.Initialize();

            var clientNodeA = new ClientNode(new ClientNodeModel {
                NetworkAddress = new NetworkAddress("1.1"),
                MaxAtmCellsNumberInCableCloudMessage = maxAtmCellsInCableCloudMessage,
                ClientName = "A",
                CableCloudListeningPort      = 10000,
                SignallingCloudListeningPort = 20000,
                IpAddress = localhost
            });

            //clientNodeA.OnMessageReceived += (sender, state) => Console.WriteLine(state);
            clientNodeA.UpdateState += (sender, state) => Console.WriteLine("ClientNode A: " + state);
            clientNodeA.Initialize();
            nameServer.UpdateDirectory("A", clientNodeA.NetworkAddress, new SubnetworkPointPool(clientNodeA.NetworkAddress.Append(1)));

            var clientNodeB = new ClientNode(new ClientNodeModel {
                NetworkAddress = new NetworkAddress("2.2"),
                MaxAtmCellsNumberInCableCloudMessage = maxAtmCellsInCableCloudMessage,
                ClientName = "B",
                CableCloudListeningPort      = 10000,
                SignallingCloudListeningPort = 20000,
                IpAddress = localhost
            });

            //clientNodeB.OnMessageReceived += (sender, state) => Console.WriteLine(state);
            clientNodeB.UpdateState += (sender, state) => Console.WriteLine("ClientNode B: " + state);
            clientNodeB.Initialize();
            nameServer.UpdateDirectory("B", clientNodeB.NetworkAddress, new SubnetworkPointPool(clientNodeB.NetworkAddress.Append(1)));

            var networkNode1 = new NetworkNode(new NetworkNodeModel {
                NetworkAddress = new NetworkAddress("1.2"),
                MaxAtmCellsNumberInCableCloudMessage = maxAtmCellsInCableCloudMessage,
                NumberOfPorts           = 8,
                CableCloudListeningPort = 10000,
                IpAddress = localhost,
                NetworkManagmentSystemListeningPort = 6666,
                SignallingCloudListeningPort        = 20000
            });

            networkNode1.UpdateState += (sender, state) => Console.WriteLine("NetworkNode 1.2: " + state);
            networkNode1.Initialize();

            var networkNode2 = new NetworkNode(new NetworkNodeModel {
                NetworkAddress = new NetworkAddress("2.1"),
                MaxAtmCellsNumberInCableCloudMessage = maxAtmCellsInCableCloudMessage,
                NumberOfPorts           = 8,
                CableCloudListeningPort = 10000,
                IpAddress = localhost,
                NetworkManagmentSystemListeningPort = 6666,
                SignallingCloudListeningPort        = 20000
            });

            networkNode2.UpdateState += (sender, state) => Console.WriteLine("NetworkNode 2.1: " + state);
            networkNode2.Initialize();

            Thread.Sleep(1000);

            var socketNodePortPair1 = new NetworkAddressNodePortPair(clientNodeA.NetworkAddress, portA);
            var socketNodePortPair2 = new NetworkAddressNodePortPair(networkNode1.NetworkAddress, port1);
            var socketNodePortPair3 = new NetworkAddressNodePortPair(networkNode1.NetworkAddress, port2);
            var socketNodePortPair4 = new NetworkAddressNodePortPair(networkNode2.NetworkAddress, port3);
            var socketNodePortPair5 = new NetworkAddressNodePortPair(networkNode2.NetworkAddress, port4);
            var socketNodePortPair6 = new NetworkAddressNodePortPair(clientNodeB.NetworkAddress, portB);

            cableCloud.AddLink(socketNodePortPair1, socketNodePortPair2);
            cableCloud.AddLink(socketNodePortPair3, socketNodePortPair4);
            cableCloud.AddLink(socketNodePortPair5, socketNodePortPair6);

            Thread.Sleep(1000);

            //var sb = new StringBuilder();

            //for (var i = 0; i < 5000; i++) sb.Append("0123456789");

            clientNodeA.Connect("B", 10);

            //Thread.Sleep(1000);

            //clientNodeA.SendMessage("Message", "B");

            Thread.Sleep(1000);
        }
Exemplo n.º 3
0
        public RUdp(SocketServer socketServer, IPEndPoint iPEndPoint) : base(socketServer)
        {
            _listener = new EventBasedNetListener();
            _server   = new NetManager(_listener)
            {
                DisconnectTimeout = 60 * 1000
            };
            _mIpEndPoint = iPEndPoint;

            _listener.ConnectionRequestEvent += request =>
            {
                request.AcceptIfKey("SomeConnectionKey");
            };

            _listener.PeerConnectedEvent += peer =>
            {
                //Console.WriteLine("We got connection: {0}", peer.EndPoint); // Show peer ip
                string clientIp = peer.EndPoint.ToString();

                ReceiveResult receiveResult = new ReceiveResult(peer.EndPoint);

                if (ClientInstance.ContainsKey(clientIp))
                {
                    ClientInstance.Remove(clientIp);
                }
                // 建立玩家peer實體
                ClientNode cNode = _SocketServer.GetPeer(this, receiveResult.RemoteEndPoint, _SocketServer);
                try
                {
                    //註冊到 mListener 中,讓他的 Receive 功能能被叫
                    RUdpInstance instance = new RUdpInstance(this, cNode, peer);
                    //註冊到 mListener 中,讓他的 Receive 功能能被叫
                    ClientInstance.Add(clientIp, instance);
                    //成功加入後傳送 Connect 事件給 Client
                    peer.Send(new byte[] { 1 }, DeliveryMethod.ReliableOrdered);
                    cNode.Initialize();
                }
                catch (Exception e)
                {
                    Printer.WriteError($"Accept connection failed : {e.Message}\n{e.StackTrace}");
                }
            };

            _listener.NetworkReceiveEvent += (peer, reader, deliveryMethod) =>
            {
                //reader.GetBytesWithLength
                if (ClientInstance.TryGetValue(peer.EndPoint.ToString(), out Instance instance))
                {
                    RUdpInstance client = (RUdpInstance)instance;
                    byte[]       b      = new byte[reader.AvailableBytes];
                    reader.GetBytes(b, reader.AvailableBytes);
                    //Console.WriteLine("We got something 2 : {0},Length: {1}", peer.EndPoint, b.Length); // Show peer ip
                    client.PassData(b);
                }
                reader.Recycle();
            };

            _listener.PeerDisconnectedEvent += (peer, disconnectInfo) =>
            {
                DisConnect(peer.EndPoint);
            };

            Task.Run(async() =>
            {
                while (true)
                {
                    try
                    {
                        _server.PollEvents();
                        await Task.Delay(1);
                    }
                    catch (Exception e)
                    {
                        Printer.WriteLine(e);
                    }
                }
            });
        }
Exemplo n.º 4
0
        private async Task <ReceiveResult> receiveAsync()
        {
            UdpReceiveResult receiveResult;

            receiveResult = await _udpClient.ReceiveAsync();

            //這個地方可以順便帶 device Id 如果發現有重複就應該擋掉
            var result = new ReceiveResult(receiveResult.RemoteEndPoint);

            if (ReqConnect.Length == receiveResult.Buffer.Length)
            {
                for (int i = 0; i < receiveResult.Buffer.Length; i++)
                {
                    result.IsOk = result.IsOk && receiveResult.Buffer[i].Equals(ReqConnect[i]);
                }
            }
            else
            {
                result.IsOk = false;
            }

            bool b = true;

            if (ReqDisconnect.Length == receiveResult.Buffer.Length)
            {
                for (int i = 0; i < receiveResult.Buffer.Length; i++)
                {
                    b = b && receiveResult.Buffer[i].Equals(ReqConnect[i]);
                }
            }
            if (!b)
            {
                DisConnect(receiveResult.RemoteEndPoint);
            }

            lock (ClientInstance)
            {
                if (ClientInstance.TryGetValue(receiveResult.RemoteEndPoint.ToString(), out Instance instance))
                {
                    UDPInstance udpInstance = (UDPInstance)instance;
                    udpInstance.PassData(receiveResult.Buffer);
                }
            }

            string clientIp = receiveResult.RemoteEndPoint.ToString();

            if (!ClientInstance.ContainsKey(clientIp) && result.IsOk)
            {
                // 建立玩家peer實體
                ClientNode  cNode    = _SocketServer.GetPeer(this, receiveResult.RemoteEndPoint, _SocketServer);
                UDPInstance instance = new UDPInstance(this, _udpClient, receiveResult.RemoteEndPoint, cNode);
                //註冊到 mListener 中,讓他的 Receive 功能能被叫
                ClientInstance.Add(clientIp, instance);
                //成功加入後傳送 Connect 事件給 Client
                await SendAsync(new byte[] { 1 }, cNode.IpEndPoint);

                cNode.Initialize();
            }

            return(result);
        }
Exemplo n.º 5
0
        public WebSocket(SocketServer socketServer, int port) : base(socketServer)
        {
            _server = new WebSocketServer(port)
            {
                WaitTime = TimeSpan.FromSeconds(1)
            };
            _server.Log.KnownFatal = new List <string>()
            {
                "The header of a frame cannot be read from the stream.",
                "Object name: 'System.Net.Sockets.NetworkStream'."
            };
            _server.WebSocketServices.AddService <WSPeer>("/WebSocket", peer =>
            {
                peer._OnOpen += () =>
                {
                    IPEndPoint iPEndPoint = peer.Context.UserEndPoint;
                    string clientIp       = iPEndPoint.ToString();

                    if (ClientInstance.ContainsKey(clientIp))
                    {
                        ClientInstance.Remove(clientIp);
                    }

                    // 建立玩家peer實體
                    ClientNode cNode = _SocketServer.GetPeer(this, iPEndPoint, _SocketServer);
                    try
                    {
                        //註冊到 mListener 中,讓他的 Receive 功能能被叫
                        WSInstance instance = new WSInstance(cNode, peer);
                        //註冊到 mListener 中,讓他的 Receive 功能能被叫
                        ClientInstance.Add(clientIp, instance);
                        //成功加入後傳送 Connect 事件給 Client
                        peer.SendBytes(new byte[] { 1 });
                        cNode.Initialize();
                    }
                    catch (Exception e)
                    {
                        Printer.WriteError($"Accept connection failed : {e.Message}\n{e.StackTrace}");
                    }
                };

                peer._OnMessage += packet =>
                {
                    if (ClientInstance.TryGetValue(peer.Context.UserEndPoint.ToString(), out Instance instance))
                    {
                        WSInstance client = (WSInstance)instance;
                        client.PassData(packet);
                    }
                };

                peer._OnClose += e =>
                {
                    DisConnect(peer.Context.UserEndPoint);
                };

                peer._OnError += e =>
                {
                    DisConnect(peer.Context.UserEndPoint);
                };
            });
        }