public static string GetNetworkPath(System.Net.Sockets.NetworkStream nstm, string host)
        {
#if VARIABLE_NETWORK_PATHS
            nstm.WriteByte((byte)'d'); // Get current directory.

            if ((int)'+' != nstm.ReadByte())
            {
                throw new Exception("GetNetworkPath failure (service didn't report success)");
            }

            return(LocalPathToNetworkPath(XContent.ReceiveXString(nstm, null), host));
#else
            return(LocalPathToNetworkPath(_netpath, host));
#endif
        }
Beispiel #2
0
        public virtual void ProcessCommands(NetworkStream nstm)
        {
            //nstm = new XNetworkStream(sock);
            int x;

            this.nstm = nstm;
            try
            {
                for (; ;)
                {
                    x = nstm.ReadByte();
                    if (x < 0)
                    {
                        //throw new Exception("Unable to read tag for command");
                        break;
                    }
                    buf[0]         = (byte)x;
                    CurrentCommand = (char)x;

                    if ('\\' == (char)buf[0])
                    {
                        if (XLog.logging)
                        {
                            XLog.log("Received close command from service; closing");
                        }
                        break;
                    }
                    else if ('<' == (char)buf[0])
                    {
                        try
                        {
                            string xml = XContent.ReceiveXString(nstm, buf);
                            System.Xml.XmlDocument xd = new System.Xml.XmlDocument();
                            xd.LoadXml(xml);
                            DistributedObjectsSlave.InitXmlConfig(xd);
                            ReloadConfig();
                        }
                        catch
                        {
                            nstm.WriteByte((byte)'-');
                            throw;
                        }
                        nstm.WriteByte((byte)'+');
                        continue;
                    }
                    else if ('?' == (char)buf[0])
                    {
                        if (null != lasterror)
                        {
                            string lerr = lasterror;
                            lasterror = null; // Also resets error!
                            XContent.SendXContent(nstm, lerr);
                        }
                        else
                        {
                            XContent.SendXContent(nstm, buf, 0); // No error!
                        }
                        continue;
                    }

                    /*
                     * if (XLog.logging)
                     * {
                     *  XLog.log("Slave processing command " + ((char)buf[0]).ToString());
                     * }
                     * */
                    ProcessCommand(nstm, (char)buf[0]);
                }
            }
            catch (System.IO.IOException ioex)
            {
                // Drop SocketException (shutdown during tasks), rethrow others.
                if ((ioex.InnerException as SocketException) == null)
                {
                    throw;
                }
#if DEBUG
                XLog.errorlog("DistributedObjectsSlave Warning: IOException+SocketException during task shutdown: " + ioex.ToString());
#endif
            }

            nstm.Close();
            nstm.Dispose();
        }
Beispiel #3
0
        protected override void ProcessCommand(System.Net.Sockets.NetworkStream nstm, char tag)
        {
            int len;

            switch (tag)
            {
            case 'R':     // Remote!
            {
                string classname = XContent.ReceiveXString(nstm, buf);

                string xlibfn = CreateXlibFileName("remote");
                {
                    buf = XContent.ReceiveXBytes(nstm, out len, buf);
                    if (0 != len)
                    {
                        System.IO.FileStream stm = System.IO.File.Create(xlibfn);
                        stm.Write(buf, 0, len);
                        stm.Close();
                    }
                }

                string dllfn = CreateDllFileName("remote");
                {
                    buf = XContent.ReceiveXBytes(nstm, out len, buf);
                    System.IO.FileStream stm = System.IO.File.Create(dllfn);
                    stm.Write(buf, 0, len);
                    stm.Close();
                }

                if (XLog.logging)
                {
                    string xclassname = classname;
                    if (null == xclassname)
                    {
                        xclassname = "<null>";
                    }
                    XLog.log("Loading IRemote plugin named " + xclassname + " for remote: " + dllfn);
                }

                rem = LoadRemotePlugin(dllfn, classname);
#if DEBUG
                try
                {
                    rem.OnRemote();
                }
                catch (Exception e)
                {
                    throw new UserException(e);
                }
#else
                rem.OnRemote();
#endif
            }
            break;

            case 'O':     //Query DGlobals
            {
                int byteCount = DGlobalsM.ToBytes(ref buf);
                XContent.SendXContent(nstm, buf, byteCount);
            }
            break;

            case 'r':
            {
                buf = XContent.ReceiveXBytes(nstm, out len, buf);
                int n     = Entry.BytesToInt(buf);
                int count = 0;
                if (null != rem)
                {
                    List <long> appendsizes = new List <long>();
                    try
                    {
                        count = rem.GetOutputFileCount(n, appendsizes);
                    }
                    catch (Exception e)
                    {
                        throw new DistributedObjectsSlave.DistObjectAbortException(e);
                    }

                    if (buf.Length < 4 + 8 * appendsizes.Count)
                    {
                        buf = new byte[Entry.Round2Power(4 + 8 * appendsizes.Count)];
                    }
                    Entry.ToBytes(count, buf, 0);
                    int offset = 4;
                    for (int i = 0; i < appendsizes.Count; i++, offset += 8)
                    {
                        Entry.LongToBytes(appendsizes[i], buf, offset);
                    }
                    XContent.SendXContent(nstm, buf, 4 + 8 * appendsizes.Count);
                    break;         // !
                }
                Entry.ToBytes(count, buf, 0);
                XContent.SendXContent(nstm, buf, 4);
            }
            break;

            default:
                base.ProcessCommand(nstm, tag);
                break;
            }
        }
        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();
                    }
                }
            }
        }