Ejemplo 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);
        }
Ejemplo n.º 2
0
 public override void DisConnect(IPEndPoint iPEndPoint)
 {
     lock (ClientInstance)
     {
         string key = iPEndPoint.ToString();
         if (ClientInstance.TryGetValue(key, out Instance instance))
         {
             RUdpInstance rUdpInstance = (RUdpInstance)instance;
             rUdpInstance.Dispose();
             ClientInstance.Remove(key);
         }
     }
 }
Ejemplo 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);
                    }
                }
            });
        }
Ejemplo n.º 4
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);
                };
            });
        }