static void ProcessItem(IP2PBase Item, ProtocolType Protocol, IPEndPoint EP = null, TcpClient Client = null) { if (Item.GetType() == typeof(ClientInfo)) { ClientInfo CI = Clients.FirstOrDefault(x => x.ID == ((ClientInfo)Item).ID); if (CI == null) { CI = (ClientInfo)Item; Clients.Add(CI); if (EP != null) { Console.WriteLine("Client Added: UDP EP: {0}:{1}, Name: {2}", EP.Address, EP.Port, CI.Name); } else if (Client != null) { Console.WriteLine("Client Added: TCP EP: {0}:{1}, Name: {2}", ((IPEndPoint)Client.Client.RemoteEndPoint).Address, ((IPEndPoint)Client.Client.RemoteEndPoint).Port, CI.Name); } } else { CI.Update((ClientInfo)Item); if (EP != null) { Console.WriteLine("Client Updated: UDP EP: {0}:{1}, Name: {2}", EP.Address, EP.Port, CI.Name); } else if (Client != null) { Console.WriteLine("Client Updated: TCP EP: {0}:{1}, Name: {2}", ((IPEndPoint)Client.Client.RemoteEndPoint).Address, ((IPEndPoint)Client.Client.RemoteEndPoint).Port, CI.Name); } } if (EP != null) { CI.ExternalEndpoint = EP; } if (Client != null) { CI.Client = Client; } BroadcastTCP(CI); if (!CI.Initialized) { if (CI.ExternalEndpoint != null & Protocol == ProtocolType.Udp) { SendUDP(new Message("Server", CI.Name, "UDP Communication Test"), CI.ExternalEndpoint); } if (CI.Client != null & Protocol == ProtocolType.Tcp) { SendTCP(new Message("Server", CI.Name, "TCP Communication Test"), CI.Client); } if (CI.Client != null & CI.ExternalEndpoint != null) { foreach (ClientInfo ci in Clients) { SendUDP(ci, CI.ExternalEndpoint); } CI.Initialized = true; } } } else if (Item.GetType() == typeof(Message)) { Console.WriteLine("Message from {0}:{1}: {2}", UDPEndPoint.Address, UDPEndPoint.Port, ((Message)Item).Content); } else if (Item.GetType() == typeof(Req)) { Req R = (Req)Item; ClientInfo CI = Clients.FirstOrDefault(x => x.ID == R.RecipientID); if (CI != null) { SendTCP(R, CI.Client); } } }
static void ProcessItem(IP2PBase Item, ProtocolType Protocol, IPEndPoint EP = null, TcpClient Client = null) { if (Item.GetType() == typeof(ClientInfo)) { ClientInfo CI = Clients.FirstOrDefault(x => x.ID == ((ClientInfo)Item).ID); if (CI == null) { CI = (ClientInfo)Item; Clients.Add(CI); if (EP != null) Console.WriteLine("Client Added: UDP EP: {0}:{1}, Name: {2}", EP.Address, EP.Port, CI.Name); else if (Client != null) Console.WriteLine("Client Added: TCP EP: {0}:{1}, Name: {2}", ((IPEndPoint)Client.Client.RemoteEndPoint).Address, ((IPEndPoint)Client.Client.RemoteEndPoint).Port, CI.Name); } else { CI.Update((ClientInfo)Item); if (EP != null) Console.WriteLine("Client Updated: UDP EP: {0}:{1}, Name: {2}", EP.Address, EP.Port, CI.Name); else if (Client != null) Console.WriteLine("Client Updated: TCP EP: {0}:{1}, Name: {2}", ((IPEndPoint)Client.Client.RemoteEndPoint).Address, ((IPEndPoint)Client.Client.RemoteEndPoint).Port, CI.Name); } if (EP != null) CI.ExternalEndpoint = EP; if (Client != null) CI.Client = Client; BroadcastTCP(CI); if (!CI.Initialized) { if (CI.ExternalEndpoint != null & Protocol == ProtocolType.Udp) SendUDP(new Message("Server", CI.Name, "UDP Communication Test"), CI.ExternalEndpoint); if (CI.Client != null & Protocol == ProtocolType.Tcp) SendTCP(new Message("Server", CI.Name, "TCP Communication Test"), CI.Client); if (CI.Client != null & CI.ExternalEndpoint != null) { foreach (ClientInfo ci in Clients) SendUDP(ci, CI.ExternalEndpoint); CI.Initialized = true; } } } else if (Item.GetType() == typeof(Message)) { Console.WriteLine("Message from {0}:{1}: {2}", UDPEndPoint.Address, UDPEndPoint.Port, ((Message)Item).Content); } else if (Item.GetType() == typeof(Req)) { Req R = (Req)Item; ClientInfo CI = Clients.FirstOrDefault(x => x.ID == R.RecipientID); if (CI != null) SendTCP(R, CI.Client); } }
private void ProcessItem(IP2PBase Item, IPEndPoint EP = null) { if (Item.GetType() == typeof(Message)) { Message m = (Message)Item; ClientInfo CI = Clients.FirstOrDefault(x => x.ID == Item.ID); if (m.ID == 0) { if (OnResultsUpdate != null) { OnResultsUpdate.Invoke(this, m.From + ": " + m.Content); } } if (m.ID != 0 & EP != null & CI != null) { if (OnMessageReceived != null) { OnMessageReceived.Invoke(EP, new MessageReceivedEventArgs(CI, m, EP)); } } } else if (Item.GetType() == typeof(ClientInfo)) { ClientInfo CI = Clients.FirstOrDefault(x => x.ID == Item.ID); if (CI == null) { Clients.Add((ClientInfo)Item); if (OnClientAdded != null) { OnClientAdded.Invoke(this, (ClientInfo)Item); } } else { CI.Update((ClientInfo)Item); if (OnClientUpdated != null) { OnClientUpdated.Invoke(this, (ClientInfo)Item); } } } else if (Item.GetType() == typeof(Notification)) { Notification N = (Notification)Item; if (N.Type == NotificationsTypes.Disconnected) { ClientInfo CI = Clients.FirstOrDefault(x => x.ID == long.Parse(N.Tag.ToString())); if (CI != null) { if (OnClientRemoved != null) { OnClientRemoved.Invoke(this, CI); } Clients.Remove(CI); } } else if (N.Type == NotificationsTypes.ServerShutdown) { if (OnResultsUpdate != null) { OnResultsUpdate.Invoke(this, "Server shutting down."); } ConnectOrDisconnect(); } } else if (Item.GetType() == typeof(Req)) { Req R = (Req)Item; ClientInfo CI = Clients.FirstOrDefault(x => x.ID == R.ID); if (CI != null) { if (OnResultsUpdate != null) { OnResultsUpdate.Invoke(this, "Received Connection Request from: " + CI.ToString()); } IPEndPoint ResponsiveEP = FindReachableEndpoint(CI); if (ResponsiveEP != null) { if (OnResultsUpdate != null) { OnResultsUpdate.Invoke(this, "Connection Successfull to: " + ResponsiveEP.ToString()); } if (OnClientConnection != null) { OnClientConnection.Invoke(CI, ResponsiveEP); } if (OnClientUpdated != null) { OnClientUpdated.Invoke(this, CI); } } } } else if (Item.GetType() == typeof(Ack)) { Ack A = (Ack)Item; if (A.Responce) { AckResponces.Add(A); } else { ClientInfo CI = Clients.FirstOrDefault(x => x.ID == A.ID); if (CI.ExternalEndpoint.Address.Equals(EP.Address) & CI.ExternalEndpoint.Port != EP.Port) { if (OnResultsUpdate != null) { OnResultsUpdate.Invoke(this, "Received Ack on Different Port (" + EP.Port + "). Updating ..."); } CI.ExternalEndpoint.Port = EP.Port; if (OnClientUpdated != null) { OnClientUpdated.Invoke(this, CI); } } List <string> IPs = new List <string>(); CI.InternalAddresses.ForEach(new Action <IPAddress>(delegate(IPAddress IP) { IPs.Add(IP.ToString()); })); if (!CI.ExternalEndpoint.Address.Equals(EP.Address) & !IPs.Contains(EP.Address.ToString())) { if (OnResultsUpdate != null) { OnResultsUpdate.Invoke(this, "Received Ack on New Address (" + EP.Address + "). Updating ..."); } CI.InternalAddresses.Add(EP.Address); } A.Responce = true; A.RecipientID = LocalClientInfo.ID; SendMessageUDP(A, EP); } } }
private void ProcessItem(IP2PBase Item, IPEndPoint EP = null) { if (Item.GetType() == typeof(Message)) { Message m = (Message)Item; ClientInfo CI = Clients.FirstOrDefault(x => x.ID == Item.ID); if (m.ID == 0) if (OnResultsUpdate != null) OnResultsUpdate.Invoke(this, m.From + ": " + m.Content); if (m.ID != 0 & EP != null & CI != null) if (OnMessageReceived != null) OnMessageReceived.Invoke(EP, new MessageReceivedEventArgs(CI, m, EP)); } else if (Item.GetType() == typeof(ClientInfo)) { ClientInfo CI = Clients.FirstOrDefault(x => x.ID == Item.ID); if (CI == null) { Clients.Add((ClientInfo)Item); if (OnClientAdded != null) OnClientAdded.Invoke(this, (ClientInfo)Item); } else { CI.Update((ClientInfo)Item); if (OnClientUpdated != null) OnClientUpdated.Invoke(this, (ClientInfo)Item); } } else if (Item.GetType() == typeof(Notification)) { Notification N = (Notification)Item; if (N.Type == NotificationsTypes.Disconnected) { ClientInfo CI = Clients.FirstOrDefault(x => x.ID == long.Parse(N.Tag.ToString())); if (CI != null) { if (OnClientRemoved != null) OnClientRemoved.Invoke(this, CI); Clients.Remove(CI); } } else if(N.Type == NotificationsTypes.ServerShutdown) { if (OnResultsUpdate != null) OnResultsUpdate.Invoke(this, "Server shutting down."); ConnectOrDisconnect(); } } else if (Item.GetType() == typeof(Req)) { Req R = (Req)Item; ClientInfo CI = Clients.FirstOrDefault(x => x.ID == R.ID); if (CI != null) { if (OnResultsUpdate != null) OnResultsUpdate.Invoke(this, "Received Connection Request from: " + CI.ToString()); IPEndPoint ResponsiveEP = FindReachableEndpoint(CI); if (ResponsiveEP != null) { if (OnResultsUpdate != null) OnResultsUpdate.Invoke(this, "Connection Successfull to: " + ResponsiveEP.ToString()); if (OnClientConnection != null) OnClientConnection.Invoke(CI, ResponsiveEP); if (OnClientUpdated != null) OnClientUpdated.Invoke(this, CI); } } } else if (Item.GetType() == typeof(Ack)) { Ack A = (Ack)Item; if (A.Responce) AckResponces.Add(A); else { ClientInfo CI = Clients.FirstOrDefault(x => x.ID == A.ID); if (CI.ExternalEndpoint.Address.Equals(EP.Address) & CI.ExternalEndpoint.Port != EP.Port) { if (OnResultsUpdate != null) OnResultsUpdate.Invoke(this, "Received Ack on Different Port (" + EP.Port + "). Updating ..."); CI.ExternalEndpoint.Port = EP.Port; if (OnClientUpdated != null) OnClientUpdated.Invoke(this, CI); } List<string> IPs = new List<string>(); CI.InternalAddresses.ForEach(new Action<IPAddress>(delegate (IPAddress IP) { IPs.Add(IP.ToString()); })); if (!CI.ExternalEndpoint.Address.Equals(EP.Address) & !IPs.Contains(EP.Address.ToString())) { if (OnResultsUpdate != null) OnResultsUpdate.Invoke(this, "Received Ack on New Address (" + EP.Address + "). Updating ..."); CI.InternalAddresses.Add(EP.Address); } A.Responce = true; A.RecipientID = LocalClientInfo.ID; SendMessageUDP(A, EP); } } }