private void CheckPeers() { List <string> delKeys = new List <string>(); foreach (var item in socketsDic) { HolePunchSocket hps = item.Value; if (socketsDic.ContainsKey(hps.peer)) { HolePunchSocket hpsNeed; socketsDic.TryGetValue(hps.peer, out hpsNeed); IPEndPoint epNeed = (IPEndPoint)hpsNeed.socket.RemoteEndPoint; byte[] sendep = Encoding.ASCII.GetBytes(epNeed.ToString() + "-" + epNeed.ToString()); hps.socket.Send(sendep, sendep.Length, 0); delKeys.Add(item.Key); } } foreach (string item in delKeys) { HolePunchSocket hps; socketsDic.TryGetValue(item, out hps); hps.socket.Close(); socketsDic.Remove(item); } }
public void handle(Socket socket) { while (true) { Console.WriteLine("A new socket request Tcp Hole Punching"); HolePunchSocket holePunchSocket = ParseTcpPunchHeader(socket); socketsDic.Add(holePunchSocket.name, holePunchSocket); CheckPeers(); } }