private Atom CreatePCPHelo() { var helo = new AtomCollection(); helo.SetHeloAgent(PeerCast.AgentName); helo.SetHeloSessionID(PeerCast.SessionID); switch (PeerCast.GetPortStatus(Channel.NetworkAddressFamily)) { case PortStatus.Open: { var listener = PeerCast.FindListener( connection.RemoteEndPoint.Address, OutputStreamType.Relay | OutputStreamType.Metadata); helo.SetHeloPort(listener.LocalEndPoint.Port); } break; case PortStatus.Firewalled: break; case PortStatus.Unknown: { var listener = PeerCast.FindListener( connection.RemoteEndPoint.Address, OutputStreamType.Relay | OutputStreamType.Metadata); if (listener != null) { helo.SetHeloPing(listener.LocalEndPoint.Port); } } break; } PCPVersion.SetHeloVersion(helo); return(new Atom(Atom.PCP_HELO, helo)); }
public virtual void SendPCPHelo() { Logger.Debug("Handshake Started"); var helo = new AtomCollection(); helo.SetHeloAgent(PeerCast.AgentName); helo.SetHeloSessionID(PeerCast.SessionID); if (PeerCast.IsFirewalled.HasValue) { if (PeerCast.IsFirewalled.Value) { //Do nothing } else { var listener = PeerCast.FindListener( RemoteEndPoint.Address, OutputStreamType.Relay | OutputStreamType.Metadata); helo.SetHeloPort(listener.LocalEndPoint.Port); } } else { var listener = PeerCast.FindListener( RemoteEndPoint.Address, OutputStreamType.Relay | OutputStreamType.Metadata); if (listener != null) { helo.SetHeloPing(listener.LocalEndPoint.Port); } } helo.SetHeloVersion(PCP_VERSION); Send(new Atom(Atom.PCP_HELO, helo)); }
private bool SendPCPHelo() { var helo = new AtomCollection(); helo.SetHeloAgent(PeerCast.AgentName); helo.SetHeloSessionID(PeerCast.SessionID); if (PeerCast.IsFirewalled.HasValue) { if (PeerCast.IsFirewalled.Value) { //Do nothing } else { var listener = PeerCast.FindListener( RemoteEndPoint.Address, OutputStreamType.Relay | OutputStreamType.Metadata); helo.SetHeloPort(listener.LocalEndPoint.Port); } } else { var listener = PeerCast.FindListener( RemoteEndPoint.Address, OutputStreamType.Relay | OutputStreamType.Metadata); if (listener != null) { helo.SetHeloPing(listener.LocalEndPoint.Port); } } PCPVersion.SetHeloVersion(helo); try { connection.Send(stream => { AtomWriter.Write(stream, new Atom(Atom.PCP_HELO, helo)); }); } catch (IOException e) { Logger.Info(e); Stop(StopReason.ConnectionError); return(false); } return(true); }
private void PostHostInfo(AtomCollection parent, Channel channel, bool playing) { var hostinfo = new AtomCollection(); hostinfo.SetHostChannelID(channel.ChannelID); hostinfo.SetHostSessionID(PeerCast.SessionID); var globalendpoint = PeerCast.GetGlobalEndPoint(AddressFamily.InterNetwork, OutputStreamType.Relay); if (globalendpoint != null) { hostinfo.AddHostIP(globalendpoint.Address); hostinfo.AddHostPort(globalendpoint.Port); } var localendpoint = PeerCast.GetLocalEndPoint(AddressFamily.InterNetwork, OutputStreamType.Relay); if (localendpoint != null) { hostinfo.AddHostIP(localendpoint.Address); hostinfo.AddHostPort(localendpoint.Port); } hostinfo.SetHostNumListeners(channel.TotalDirects); hostinfo.SetHostNumRelays(channel.TotalRelays); hostinfo.SetHostUptime(channel.Uptime); if (channel.Contents.Count > 0) { hostinfo.SetHostOldPos((uint)(channel.Contents.Oldest.Position & 0xFFFFFFFFU)); hostinfo.SetHostNewPos((uint)(channel.Contents.Newest.Position & 0xFFFFFFFFU)); } PCPVersion.SetHostVersion(hostinfo); var relayable = PeerCast.AccessController.IsChannelRelayable(channel); var playable = PeerCast.AccessController.IsChannelPlayable(channel) && PeerCast.FindListener(remoteEndPoint.Address, OutputStreamType.Play) != null; var firewalled = !PeerCast.IsFirewalled.HasValue || PeerCast.IsFirewalled.Value || PeerCast.FindListener(remoteEndPoint.Address, OutputStreamType.Relay) == null; var receiving = playing && channel.Status == SourceStreamStatus.Receiving; hostinfo.SetHostFlags1( (relayable ? PCPHostFlags1.Relay : 0) | (playable ? PCPHostFlags1.Direct : 0) | (firewalled ? PCPHostFlags1.Firewalled : 0) | PCPHostFlags1.Tracker | (receiving ? PCPHostFlags1.Receiving : PCPHostFlags1.None)); parent.SetHost(hostinfo); }
private void AnnounceThreadProc() { Logger.Debug("Thread started"); var host = Uri.DnsSafeHost; var port = Uri.Port; if (port < 0) { port = DefaultPort; } while (!IsStopped) { int next_update = Environment.TickCount; posts.Clear(); try { Logger.Debug("Connecting to YP"); AnnouncingStatus = AnnouncingStatus.Connecting; using (var client = new TcpClient(host, port)) { using (var stream = client.GetStream()) { AtomWriter.Write(stream, new Atom(new ID4("pcp\n"), (int)1)); var helo = new AtomCollection(); Logger.Debug("Sending Handshake"); helo.SetHeloAgent(PeerCast.AgentName); helo.SetHeloVersion(1218); helo.SetHeloSessionID(PeerCast.SessionID); helo.SetHeloBCID(PeerCast.BroadcastID); if (PeerCast.IsFirewalled.HasValue) { if (PeerCast.IsFirewalled.Value) { //Do nothing } else { var listener = PeerCast.FindListener( ((IPEndPoint)client.Client.RemoteEndPoint).Address, OutputStreamType.Relay | OutputStreamType.Metadata); if (listener != null) { helo.SetHeloPort(listener.LocalEndPoint.Port); } } } else { var listener = PeerCast.FindListener( ((IPEndPoint)client.Client.RemoteEndPoint).Address, OutputStreamType.Relay | OutputStreamType.Metadata); if (listener != null) { helo.SetHeloPing(listener.LocalEndPoint.Port); } } AtomWriter.Write(stream, new Atom(Atom.PCP_HELO, helo)); while (!IsStopped) { var atom = AtomReader.Read(stream); if (atom.Name == Atom.PCP_OLEH) { OnPCPOleh(atom); break; } else if (atom.Name == Atom.PCP_QUIT) { Logger.Debug("Handshake aborted by PCP_QUIT ({0})", atom.GetInt32()); throw new QuitException(); } if (restartEvent.WaitOne(10)) { throw new RestartException(); } } Logger.Debug("Handshake succeeded"); AnnouncingStatus = AnnouncingStatus.Connected; while (!IsStopped) { if (next_update - Environment.TickCount <= 0) { Logger.Debug("Sending channel info"); lock (announcingChannels) { foreach (var announcing in announcingChannels) { PostChannelBcst(announcing.Channel, true); } } next_update = Environment.TickCount + 30000; } if (stream.DataAvailable) { Atom atom = AtomReader.Read(stream); ProcessAtom(atom); } lock (posts) { foreach (var atom in posts) { AtomWriter.Write(stream, atom); } posts.Clear(); } if (restartEvent.WaitOne(10)) { throw new RestartException(); } } lock (posts) { foreach (var atom in posts) { AtomWriter.Write(stream, atom); } posts.Clear(); } Logger.Debug("Closing connection"); AtomWriter.Write(stream, new Atom(Atom.PCP_QUIT, Atom.PCP_ERROR_QUIT)); } } } catch (RestartException) { Logger.Debug("Connection retrying"); AnnouncingStatus = AnnouncingStatus.Connecting; } catch (BannedException) { AnnouncingStatus = AnnouncingStatus.Error; Logger.Error("Your BCID is banned"); break; } catch (QuitException) { AnnouncingStatus = AnnouncingStatus.Error; } catch (SocketException e) { AnnouncingStatus = AnnouncingStatus.Error; Logger.Info(e); } catch (IOException e) { AnnouncingStatus = AnnouncingStatus.Error; Logger.Info(e); } Logger.Debug("Connection closed"); if (!IsStopped) { restartEvent.WaitOne(10000); } else { AnnouncingStatus = AnnouncingStatus.Idle; } } Logger.Debug("Thread finished"); }