Esempio n. 1
0
        /// <summary>
        /// Links the given object files into an executable binary.
        /// </summary>
        /// <returns>The exit code of the link process.</returns>
        public override int Execute()
        {
            List <ObjectFile> files = new List <ObjectFile>();

            foreach (string path in ObjectFiles)
            {
                if (!File.Exists(path))
                {
                    Console.WriteLine(path + " does't exist.");

                    return(1);
                }

                using (Stream stream = File.OpenRead(path))
                {
                    AtomReader reader = new AtomReader();
                    files.Add(reader.Read(stream));
                }
            }

            using (Stream destination = File.Create(Destination))
            {
                AtomLinker linker = new AtomLinker();
                linker.Link(files, destination);
            }

            return(0);
        }
Esempio n. 2
0
        protected virtual bool PingHost(IPEndPoint target, Guid remote_session_id)
        {
            Logger.Debug("Ping requested. Try to ping: {0}({1})", target, remote_session_id);
            bool result = false;

            try {
                var client = new System.Net.Sockets.TcpClient();
                client.Connect(target);
                client.ReceiveTimeout = 3000;
                client.SendTimeout    = 3000;
                var stream = client.GetStream();
                var conn   = new Atom(Atom.PCP_CONNECT, 1);
                AtomWriter.Write(stream, conn);
                var helo = new AtomCollection();
                helo.SetHeloSessionID(PeerCast.SessionID);
                AtomWriter.Write(stream, new Atom(Atom.PCP_HELO, helo));
                var res = AtomReader.Read(stream);
                if (res.Name == Atom.PCP_OLEH)
                {
                    var session_id = res.Children.GetHeloSessionID();
                    if (session_id.HasValue && session_id.Value == remote_session_id)
                    {
                        Logger.Debug("Ping succeeded");
                        result = true;
                    }
                    else
                    {
                        Logger.Debug("Ping failed. Remote SessionID mismatched");
                    }
                }
                AtomWriter.Write(stream, new Atom(Atom.PCP_QUIT, Atom.PCP_ERROR_QUIT));
                stream.Close();
                client.Close();
            }
            catch (InvalidDataException e) {
                Logger.Debug("Ping failed");
                Logger.Debug(e);
            }
            catch (System.Net.Sockets.SocketException e) {
                Logger.Debug("Ping failed");
                Logger.Debug(e);
            }
            catch (EndOfStreamException e) {
                Logger.Debug("Ping failed");
                Logger.Debug(e);
            }
            catch (System.IO.IOException io_error) {
                Logger.Debug("Ping failed");
                Logger.Debug(io_error);
                if (!(io_error.InnerException is System.Net.Sockets.SocketException))
                {
                    throw;
                }
            }
            return(result);
        }
Esempio n. 3
0
        protected Atom RecvAtom()
        {
            Atom res = null;

            if (Recv(s => { res = AtomReader.Read(s); }))
            {
                return(res);
            }
            else
            {
                return(null);
            }
        }
        private List <Host> ReadHosts(Stream s, Guid channel_id)
        {
            var  res  = new List <Host>();
            bool quit = false;

            try {
                while (!quit)
                {
                    var atom = AtomReader.Read(s);
                    if (atom.Name == Atom.PCP_HOST)
                    {
                        if (atom.Children.GetHostChannelID() == channel_id)
                        {
                            var host      = new HostBuilder();
                            var endpoints = atom.Children.GetHostEndPoints();
                            if (endpoints.Length > 0)
                            {
                                host.GlobalEndPoint = endpoints[0];
                            }
                            if (endpoints.Length > 1)
                            {
                                host.LocalEndPoint = endpoints[1];
                            }
                            host.DirectCount = atom.Children.GetHostNumListeners() ?? 0;
                            host.RelayCount  = atom.Children.GetHostNumRelays() ?? 0;
                            host.SessionID   = atom.Children.GetHostSessionID() ?? Guid.Empty;
                            if (atom.Children.GetHostFlags1().HasValue)
                            {
                                var flags = atom.Children.GetHostFlags1().Value;
                                host.IsControlFull = (flags & PCPHostFlags1.ControlIn) != 0;
                                host.IsFirewalled  = (flags & PCPHostFlags1.Firewalled) != 0;
                                host.IsDirectFull  = (flags & PCPHostFlags1.Direct) == 0;
                                host.IsRelayFull   = (flags & PCPHostFlags1.Relay) == 0;
                                host.IsReceiving   = (flags & PCPHostFlags1.Receiving) != 0;
                                host.IsTracker     = (flags & PCPHostFlags1.Tracker) != 0;
                            }
                            res.Add(host.ToHost());
                        }
                    }
                    if (atom.Name == Atom.PCP_QUIT)
                    {
                        quit = true;
                    }
                }
            }
            catch (InvalidCastException e) {
                Logger.Error(e);
            }
            return(res);
        }
        public static IEnumerable <Atom> RecvAtoms(this StreamConnection connection)
        {
            var res = new Queue <Atom>();

            connection.Recv(s => {
                while (s.Position < s.Length)
                {
                    var pos = s.Position;
                    try {
                        var atom = AtomReader.Read(s);
                        res.Enqueue(atom);
                    }
                    catch (System.IO.EndOfStreamException) {
                        s.Position = pos;
                        break;
                    }
                }
            });
            return(res);
        }
        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");
        }