public override async Task SendAsync(byte[] data, IPEndPoint endPoint) { if (ClientInstance.TryGetValue(endPoint.ToString(), out Instance instance)) { await((RUdpInstance)instance).Send(data); } }
public override async Task SendAsync(byte[] data, IPEndPoint endPoint) { //修改成 Async //測試中 //不等候直接讓它過去 //測試當量大的時候 //不等後的風險 if (ClientInstance.TryGetValue(endPoint.ToString(), out Instance instance)) { await((UDPInstance)instance).Send(data); } }
public override async Task SendAsync(byte[] data, IPEndPoint endPoint) { if (ClientInstance.TryGetValue(endPoint.ToString(), out Instance instance)) { await((TCPInstance)instance).Send(data); } else { Console.WriteLine("GG"); } }
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); } } }
private async Task StartReceiveAsync(TcpClient tcp) { IPEndPoint ipEndPoint = tcp.Client.RemoteEndPoint as IPEndPoint; if (ClientInstance.TryGetValue(ipEndPoint.ToString(), out Instance instance)) { TCPInstance tcpInstance = (TCPInstance)instance; NetworkStream stream = tcp.GetStream(); await Task.Run(async() => { try { while (tcp.Client.Connected) { byte[] buff = new byte[BufferSize]; int count = await stream.ReadAsync(buff, 0, buff.Length); Array.Resize(ref buff, count); if (count.Equals(0)) { DisConnect(ipEndPoint); break; } // 將接收到的buff data送入client佇列等候處理 tcpInstance.PassData(buff); } } catch (IOException e) { if (!e.Message.Contains(ErrorMsg1)) { Console.WriteLine(e.Message + "," + e.HResult); } else { DisConnect(ipEndPoint); } } }); } }
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); }; }); }