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); }
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); } } }); }
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); }
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); }; }); }