예제 #1
0
        public virtual void Open()
        {
            if (didopen)
            {
                throw new Exception("Attempted to Open after already Open");
            }
            didopen = true;
            string portfilename = null;
            System.IO.FileStream portfile = null;
            try
            {
                Socket lsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint lipep;
                //int lptests = 0;
                for (; ; )
                {
                    // Try a few ports...
                    lipep = new IPEndPoint(IPAddress.Any, GetSlavePort());
                    try
                    {
                        System.Threading.Mutex lm = new System.Threading.Mutex(false, "DODLL_Listen{2FDD7284-14A8-4f4d-8448-0CE229C1E596}");
                        lm.WaitOne();
                        try
                        {
                            string spf = lipep.Port.ToString() + ".pf";
                            if (System.IO.File.Exists(spf))
                            {
                                continue;
                            }
                            lsock.Bind(lipep);
                            lsock.Listen(2);
                            portfile = new System.IO.FileStream(spf, System.IO.FileMode.Create, System.IO.FileAccess.Write,
                                System.IO.FileShare.Delete, 8, System.IO.FileOptions.DeleteOnClose);
                            portfilename = spf;
                        }
                        finally
                        {
                            lm.ReleaseMutex();
                        }
                    }
                    catch (SocketException e)
                    {
                        //if (++lptests < 4)
                        {
                            continue;
                        }
                        //throw new Exception(e.ToString() + "  [Note: ensure port " + lipep.Port.ToString() + " is free / not blocked (ports >= " + SlavePortMin.ToString() + ")]");
                    }
                    break;
                }
#if DEBUG
                this.OpenPort = lipep.Port;
                lock (AllOpenPorts)
                {
                    AllOpenPorts.Add(this.OpenPort);
                }
#endif

                foreach (SlaveInfo slave in dslaves)
                {
                    NetworkStream servStm = null;

#if DEBUGfailtest
                    bool failtest = true;
#else
                    const bool failtest = false;
#endif
                    for (; ; )
                    {
                        Socket servSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        servSock.Connect(slave.blockinfo[3], 55900);

                        servStm = new XNetworkStream(servSock);

                        servStm.WriteByte((byte)'B'); // AddBlock.
                        XContent.SendXContent(servStm, slave.sblockinfo);
                        XContent.SendXContent(servStm, lipep.Port.ToString());
                        XContent.SendXContent(servStm, sjid + ":" + Convert.ToBase64String(Encoding.UTF8.GetBytes(jobdesc)));
                        {
                            int rB = servStm.ReadByte();
                            if ('+' != rB)
                            {
                                if ('_' == rB)
                                {
                                    throw new Exception("Error returned from AddBlock: " + XContent.ReceiveXString(servStm, null));
                                }
                                else
                                {
                                    throw new Exception("AddBlock failure");
                                }
                            }
                        }

                        // 30000000 microseconds == 30 seconds
                        if (failtest || !lsock.Poll(30000000, SelectMode.SelectRead))
                        {
#if DEBUGfailtest
                            failtest = false;
#endif
                            servStm.Close();
                            continue;
                        }
                        Socket slaveSock = lsock.Accept();
                        slave.nstm = new XNetworkStream(slaveSock);
                        break;
                    }
                    if (1 != slave.nstm.ReadByte())
                    {
                        throw new Exception("Sub process connection error: invalid handshake  [Non-sub-process connection?]");
                    }
                    {
                        int len;
                        byte[] bpid = XContent.ReceiveXBytes(slave.nstm, out len, null);
                        if (len < 4)
                        {
                            throw new Exception("Sub process connection error: invalid SlavePID handshake");
                        }
                        slave.pid = BytesToInt(bpid);
                    }

                    servStm.WriteByte((byte)'\\');
                    servStm.Close(1000);
                }

                lsock.Close();
            }
            catch (FormatException e)
            {
                throw new FormatException("Format error in Open: " + e.ToString());
            }
            catch (Exception e)
            {
                throw new Exception("Error in Open: " + e.ToString() + "  [Note: ensure the Windows service is running]");
            }
            finally
            {
#if DEBUG
                if (0 != this.OpenPort)
                {
                    lock (AllOpenPorts)
                    {
                        AllOpenPorts.Remove(this.OpenPort);
                    }
                }
#endif
                if (null != portfile)
                {
                    System.Threading.Mutex lm = new System.Threading.Mutex(false, "DODLL_Listen{2FDD7284-14A8-4f4d-8448-0CE229C1E596}");
                    lm.WaitOne();
                    try
                    {
                        if (null != portfile)
                        {
                            portfile.Close();
                        }
                        System.IO.File.Delete(portfilename);
                    }
                    catch
                    {
                    }
                    finally
                    {
                        lm.ReleaseMutex();
                    }
                }
            }
        }
예제 #2
0
        public virtual void Open()
        {
            if (didopen)
            {
                throw new Exception("Attempted to Open after already Open");
            }
            didopen = true;
            string portfilename = null;

            System.IO.FileStream portfile = null;
            try
            {
                Socket     lsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint lipep;
                //int lptests = 0;
                for (; ;)
                {
                    // Try a few ports...
                    lipep = new IPEndPoint(IPAddress.Any, GetSlavePort());
                    try
                    {
                        System.Threading.Mutex lm = new System.Threading.Mutex(false, "DODLL_Listen{2FDD7284-14A8-4f4d-8448-0CE229C1E596}");
                        lm.WaitOne();
                        try
                        {
                            string spf = lipep.Port.ToString() + ".pf";
                            if (System.IO.File.Exists(spf))
                            {
                                continue;
                            }
                            lsock.Bind(lipep);
                            lsock.Listen(2);
                            portfile = new System.IO.FileStream(spf, System.IO.FileMode.Create, System.IO.FileAccess.Write,
                                                                System.IO.FileShare.Delete, 8, System.IO.FileOptions.DeleteOnClose);
                            portfilename = spf;
                        }
                        finally
                        {
                            lm.ReleaseMutex();
                        }
                    }
                    catch (SocketException e)
                    {
                        //if (++lptests < 4)
                        {
                            continue;
                        }
                        //throw new Exception(e.ToString() + "  [Note: ensure port " + lipep.Port.ToString() + " is free / not blocked (ports >= " + SlavePortMin.ToString() + ")]");
                    }
                    break;
                }
#if DEBUG
                this.OpenPort = lipep.Port;
                lock (AllOpenPorts)
                {
                    AllOpenPorts.Add(this.OpenPort);
                }
#endif

                foreach (SlaveInfo slave in dslaves)
                {
                    NetworkStream servStm = null;

#if DEBUGfailtest
                    bool failtest = true;
#else
                    const bool failtest = false;
#endif
                    for (; ;)
                    {
                        Socket servSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        servSock.Connect(slave.blockinfo[3], 55900);

                        servStm = new XNetworkStream(servSock);

                        servStm.WriteByte((byte)'B'); // AddBlock.
                        XContent.SendXContent(servStm, slave.sblockinfo);
                        XContent.SendXContent(servStm, lipep.Port.ToString());
                        XContent.SendXContent(servStm, sjid + ":" + Convert.ToBase64String(Encoding.UTF8.GetBytes(jobdesc)));
                        {
                            int rB = servStm.ReadByte();
                            if ('+' != rB)
                            {
                                if ('_' == rB)
                                {
                                    throw new Exception("Error returned from AddBlock: " + XContent.ReceiveXString(servStm, null));
                                }
                                else
                                {
                                    throw new Exception("AddBlock failure");
                                }
                            }
                        }

                        // 30000000 microseconds == 30 seconds
                        if (failtest || !lsock.Poll(30000000, SelectMode.SelectRead))
                        {
#if DEBUGfailtest
                            failtest = false;
#endif
                            servStm.Close();
                            continue;
                        }
                        Socket slaveSock = lsock.Accept();
                        slave.nstm = new XNetworkStream(slaveSock);
                        break;
                    }
                    if (1 != slave.nstm.ReadByte())
                    {
                        throw new Exception("Sub process connection error: invalid handshake  [Non-sub-process connection?]");
                    }
                    {
                        int    len;
                        byte[] bpid = XContent.ReceiveXBytes(slave.nstm, out len, null);
                        if (len < 4)
                        {
                            throw new Exception("Sub process connection error: invalid SlavePID handshake");
                        }
                        slave.pid = BytesToInt(bpid);
                    }

                    servStm.WriteByte((byte)'\\');
                    servStm.Close(1000);
                }

                lsock.Close();
            }
            catch (FormatException e)
            {
                throw new FormatException("Format error in Open: " + e.ToString());
            }
            catch (Exception e)
            {
                throw new Exception("Error in Open: " + e.ToString() + "  [Note: ensure the Windows service is running]");
            }
            finally
            {
#if DEBUG
                if (0 != this.OpenPort)
                {
                    lock (AllOpenPorts)
                    {
                        AllOpenPorts.Remove(this.OpenPort);
                    }
                }
#endif
                if (null != portfile)
                {
                    System.Threading.Mutex lm = new System.Threading.Mutex(false, "DODLL_Listen{2FDD7284-14A8-4f4d-8448-0CE229C1E596}");
                    lm.WaitOne();
                    try
                    {
                        if (null != portfile)
                        {
                            portfile.Close();
                        }
                        System.IO.File.Delete(portfilename);
                    }
                    catch
                    {
                    }
                    finally
                    {
                        lm.ReleaseMutex();
                    }
                }
            }
        }