示例#1
0
 public void ReleaseLock()
 {
     if (ntsm == null)
     {
         throw new Exception("CriticalSection.ReleaseLock() failed:  Network stream has already been closed due to previous exceptions.");
     }
     if (!hasLock)
     {
         throw new Exception("CriticalSection.ReleaseLock() failed:  The current critical section does not own the lock.");
     }
     try
     {
         ntsm.WriteByte((byte)'+');  //release lock
         if (ntsm.ReadByte() == (int)'+')
         {
             hasLock = false;
         }
         else
         {
             throw new Exception("CriticalSection.ReleaseLock() handshake failed.");
         }
     }
     catch
     {
         if (object.ReferenceEquals(this, currentSection))
         {
             currentSection = null;
         }
         ntsm.Close();
         ntsm = null;
         sock.Close();
         sock = null;
         throw;
     }
 }
示例#2
0
        public static CriticalSection Create(string lockname)
        {
            if (null == lockname)
            {
                if (currentSection != null)
                {
                    return(currentSection);
                }
            }
            else
            {
                if (0 == lockname.Length)
                {
                    throw new Exception("Empty lock name is invalid");
                }
            }

            System.Net.Sockets.Socket sock = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork,
                                                                           System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
            System.Net.Sockets.NetworkStream ntsm = null;
            try
            {
                ////sock.Connect(Surrogate.MasterHost, 55900);
                sock.Connect(DO5_Surrogate_LocateMasterHost(".."), 55900);
                ntsm = new XNetworkStream(sock);
                ntsm.WriteByte((byte)'J'); //hand shake with surrogate
                if (ntsm.ReadByte() == (byte)'+')
                {
                    CriticalSection sec = new CriticalSection();
                    sec.sock    = sock;
                    sec.ntsm    = ntsm;
                    sec.hasLock = false;
                    if (null == lockname)
                    {
                        currentSection = sec;
                    }
                    else
                    {
                        sec.locknamebytes = Encoding.UTF8.GetBytes(lockname);
                    }
                    return(sec);
                }
                else
                {
                    throw new Exception("CriticalSection.Create() handshake failed.");
                }
            }
            catch
            {
                if (ntsm != null)
                {
                    ntsm.Close();
                    ntsm = null;
                }
                sock.Close();
                sock = null;
                throw;
            }
        }
示例#3
0
        public static CriticalSection Create(string lockname)
        {
            if (null == lockname)
            {
                if (currentSection != null)
                {
                    return currentSection;
                }
            }
            else
            {
                if (0 == lockname.Length)
                {
                    throw new Exception("Empty lock name is invalid");
                }
            }

            System.Net.Sockets.Socket sock = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork,
                System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
            System.Net.Sockets.NetworkStream ntsm = null;
            try
            {
                ////sock.Connect(Surrogate.MasterHost, 55900);
                sock.Connect(DO5_Surrogate_LocateMasterHost(".."), 55900);
                ntsm = new XNetworkStream(sock);
                ntsm.WriteByte((byte)'J'); //hand shake with surrogate
                if (ntsm.ReadByte() == (byte)'+')
                {
                    CriticalSection sec = new CriticalSection();
                    sec.sock = sock;
                    sec.ntsm = ntsm;
                    sec.hasLock = false;
                    if (null == lockname)
                    {
                        currentSection = sec;
                    }
                    else
                    {
                        sec.locknamebytes = Encoding.UTF8.GetBytes(lockname);
                    }
                    return sec;
                }
                else
                {
                    throw new Exception("CriticalSection.Create() handshake failed.");
                }
            }
            catch
            {
                if (ntsm != null)
                {
                    ntsm.Close();
                    ntsm = null;
                }
                sock.Close();
                sock = null;
                throw;
            }
        }
示例#4
0
 public void Dispose()
 {
     if (cs != null)
     {
         cs.ReleaseLock();
         cs = null;
     }
 }
示例#5
0
        public static IDisposable GetLock_internal(string x)
        {
            CriticalSection _cs = CriticalSection.Create(x);

            _cs.EnterLock();

            GlobalCriticalSection gs = new GlobalCriticalSection();

            gs.cs = _cs;
            return(gs);
        }
示例#6
0
 public bool EnterLock()
 {
     if (ntsm == null)
     {
         throw new Exception("CriticalSection.EnterLock() failed:  Network stream has already been closed due to previous exceptions.");
     }
     try
     {
         // acquire lock
         for (int iacq = 0; ;)
         {
             try
             {
                 if (null == locknamebytes)
                 {
                     ntsm.WriteByte((byte)'Q');
                 }
                 else
                 {
                     ntsm.WriteByte(128 + (byte)'Q'); // 128+Q
                     XContent.SendXContent(ntsm, locknamebytes);
                 }
             }
             catch
             {
                 if (++iacq > 60 / 5)
                 {
                     throw;
                 }
                 System.Threading.Thread.Sleep(1000 * 5);
                 _reopen();
                 continue;
             }
             break;
         }
         if (hasLock)
         {
             throw new Exception("CriticalSection.EnterLock() failed:  cannot enter lock twice");
         }
         if (ntsm.ReadByte() == (int)'+')
         {
             hasLock = true;
             return(true);
         }
         else
         {
             throw new Exception("CriticalSection.EnterLock() handshake failed.");
         }
     }
     catch
     {
         if (object.ReferenceEquals(this, currentSection))
         {
             currentSection = null;
         }
         ntsm.Close();
         ntsm = null;
         sock.Close();
         sock = null;
         throw;
     }
 }
示例#7
0
 public void Dispose()
 {
     if (cs != null)
     {
         cs.ReleaseLock();
         cs = null;
     }
 }
示例#8
0
 public bool EnterLock()
 {
     if (ntsm == null)
     {
         throw new Exception("CriticalSection.EnterLock() failed:  Network stream has already been closed due to previous exceptions.");
     }
     try
     {
         // acquire lock
         for (int iacq = 0; ; )
         {
             try
             {
                 if (null == locknamebytes)
                 {
                     ntsm.WriteByte((byte)'Q');
                 }
                 else
                 {
                     ntsm.WriteByte(128 + (byte)'Q'); // 128+Q
                     XContent.SendXContent(ntsm, locknamebytes);
                 }
             }
             catch
             {
                 if (++iacq > 60 / 5)
                 {
                     throw;
                 }
                 System.Threading.Thread.Sleep(1000 * 5);
                 _reopen();
                 continue;
             }
             break;
         }
         if (hasLock)
         {
             throw new Exception("CriticalSection.EnterLock() failed:  cannot enter lock twice");
         }
         if (ntsm.ReadByte() == (int)'+')
         {
             hasLock = true;
             return true;
         }
         else
         {
             throw new Exception("CriticalSection.EnterLock() handshake failed.");
         }
     }
     catch
     {
         if (object.ReferenceEquals(this, currentSection))
         {
             currentSection = null;
         }
         ntsm.Close();
         ntsm = null;
         sock.Close();
         sock = null;
         throw;
     }
 }
示例#9
0
 public void ReleaseLock()
 {
     if (ntsm == null)
     {
         throw new Exception("CriticalSection.ReleaseLock() failed:  Network stream has already been closed due to previous exceptions.");
     }
     if (!hasLock)
     {
         throw new Exception("CriticalSection.ReleaseLock() failed:  The current critical section does not own the lock.");
     }
     try
     {
         ntsm.WriteByte((byte)'+');  //release lock
         if (ntsm.ReadByte() == (int)'+')
         {
             hasLock = false;
         }
         else
         {
             throw new Exception("CriticalSection.ReleaseLock() handshake failed.");
         }
     }
     catch
     {
         if (object.ReferenceEquals(this, currentSection))
         {
             currentSection = null;
         }
         ntsm.Close();
         ntsm = null;
         sock.Close();
         sock = null;
         throw;
     }
 }