protected override void execute()
        {
            TnSServer server = new TnSServer(socket, new TnSProtocol());

            server.OnRequestReceived += (ToAccept request) => {
                if (RequestReceived != null)
                {
                    return(RequestReceived(request));
                }
                return(request);
            };

            server.JobInitialized += (Job job) => {
                JobsList.Receiving.push(job);
                this.job = job;
            };

            try {
                server.transfer();
            } catch (SocketException e) {
                // Trigger the event of conncetion error
                ConnectionError?.Invoke(job);
            } catch (System.IO.IOException e) {
                String sourceName = null;
                //if (job != null) {
                //    job.Status = Job.JobStatus.ConnectionError;

                //    // Trigger the event of Path error
                //    sourceName = job.Task.Info[0].Name;
                //    if (job.Task.Info.Count > 1)
                //        for (int i = 1; i > job.Task.Info.Count; i++)
                //            sourceName += ", " + job.Task.Info[i].Name;
                //}
                PathError?.Invoke(e, job);
            } catch (ObjectDisposedException e) {
                ConnectionError?.Invoke(job);

                // NOTE: 'ObjectDisposedException' and 'SocketException' make the same management as the generale 'Exception'.
                // However they have their own catch because they are very common exceptions in this point of the code, and
                // in this way they are marked and clearely visible.
                // This separation is not neede, but it's really useful to remark this concept.
            } catch (Exception e) {
                ConnectionError?.Invoke(job);
            } finally {
                // Remove the job (if any) from the list
                if (job != null)
                {
                    JobsList.Receiving.remove(job.Id);
                }
                // Close the socket
                socket.Close();
            }
        }
Exemple #2
0
        protected override void execute()
        {
            if (Settings.Instance.Network == null)
            {
                return;
            }
            NetworkInterface nic  = Settings.Instance.Network.Nic;
            IPAddress        addr = null;

            foreach (UnicastIPAddressInformation ip in nic.GetIPProperties().UnicastAddresses)
            {
                if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                {
                    addr = ip.Address;
                }
            }
            //IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, Settings.Instance.SERV_ACCEPTING_PORT);
            IPEndPoint localEndPoint = new IPEndPoint(addr, Settings.Instance.SERV_ACCEPTING_PORT);

            listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try {
                listener.Bind(localEndPoint);
                listener.Listen(50);

                while (true)
                {
                    Console.WriteLine("Waiting for a connection...");
                    Socket handler = listener.Accept();

                    ReceptionExecutor receiver = new ReceptionExecutor(handler);
                    receiver.RequestReceived += (ToAccept request) => {
                        if (RequestReceived != null)
                        {
                            return(RequestReceived(request));
                        }
                        return(request);
                    };
                    receiver.ConnectionError += (Job j) => {
                        ConnectionError?.Invoke(j);
                    };
                    receiver.PathError += (System.IO.IOException e, Job job) => {
                        PathError?.Invoke(e, job);
                    };
                    RegisterChild(receiver);
                    receiver.run();
                }
            } catch (Exception e) {
                Console.WriteLine(e.ToString());
            }
        }