示例#1
0
文件: Spawner.cs 项目: zofuthan/xsp
        public static Process SpawnOndemandChild(string socketFile)
        {
            CompatArraySegment <byte>?torelease = null;

            try {
                Logger.Write(LogLevel.Debug, "Spawning via the shim {0}", socketFile);
                var client = new UnixClient();
                client.Connect(socketFile);
                CompatArraySegment <byte> buffer = buffers.ClaimBuffer();
                torelease = buffer;
                int receivedCount;
                using (NetworkStream socket = client.GetStream()) {
                    socket.Write(spawnString, 0, spawnString.Length);
                    receivedCount = socket.Read(buffer.Array, buffer.Offset, buffer.Count);
                    if (receivedCount < 0)
                    {
                        throw new Exception("Didn't receive the child pid");
                    }
                }
                string received = Encoding.UTF8.GetString(buffer.Array, buffer.Offset, receivedCount);
                string clean    = received.Trim();
                int    pid;
                if (!Int32.TryParse(clean, out pid))
                {
                    throw new Exception("Couldn't parse the pid \"" + clean + "\"");
                }

                if (pid < 0)
                {
                    throw new Exception("Invalid pid: " + pid);
                }

                return(Process.GetProcessById(pid));
            } catch (Exception e) {
                Logger.Write(LogLevel.Error, "Error while talking to the shim for socket file {0}", socketFile);
                Logger.Write(e);
                return(null);
            } finally {
                if (torelease != null)
                {
                    buffers.ReturnBuffer(torelease.Value);
                }
            }
        }