コード例 #1
0
 public override void  Release()
 {
     lock (this)
     {
         if (LockExists())
         {
             try
             {
                 channel.Unlock(0, channel.Length);
             }
             finally
             {
                 lock_Renamed = false;
                 try
                 {
                     channel.Close();
                 }
                 finally
                 {
                     channel = null;
                     try
                     {
                         f.Close();
                     }
                     finally
                     {
                         f = null;
                         lock (LOCK_HELD)
                         {
                             LOCK_HELD.Remove(path.FullName);
                         }
                     }
                 }
             }
             bool tmpBool;
             if (System.IO.File.Exists(path.FullName))
             {
                 System.IO.File.Delete(path.FullName);
                 tmpBool = true;
             }
             else if (System.IO.Directory.Exists(path.FullName))
             {
                 System.IO.Directory.Delete(path.FullName);
                 tmpBool = true;
             }
             else
             {
                 tmpBool = false;
             }
             if (!tmpBool)
             {
                 throw new LockReleaseFailedException("failed to delete " + path);
             }
         }
     }
 }
コード例 #2
0
ファイル: Log.cs プロジェクト: liangzaixu/UtilityTools
 public void SaveLogToFile()
 {
     try
     {
         string logInfo = GetLogText();
         if (logInfo.Length > 0)
         {
             System.IO.FileStream fs = GetFileStream();
             byte[] buffer           = System.Text.UTF8Encoding.UTF8.GetBytes(logInfo);
             long   lockBegin        = fs.Length;
             long   lockEnd          = buffer.Length;
             fs.Position = lockBegin;
             fs.Lock(lockBegin, lockEnd);
             //fs.WriteAsync(buffer, 0, buffer.Length);
             fs.Write(buffer, 0, buffer.Length);
             fs.Unlock(lockBegin, lockEnd);
             fs.Flush();
             //fs.Close();
         }
     }
     catch { }
 }
コード例 #3
0
        static int _m_Unlock(RealStatePtr L)
        {
            try {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);


                System.IO.FileStream gen_to_be_invoked = (System.IO.FileStream)translator.FastGetCSObj(L, 1);



                {
                    long _position = LuaAPI.lua_toint64(L, 2);
                    long _length   = LuaAPI.lua_toint64(L, 3);

                    gen_to_be_invoked.Unlock(_position, _length);



                    return(0);
                }
            } catch (System.Exception gen_e) {
                return(LuaAPI.luaL_error(L, "c# exception:" + gen_e));
            }
        }
コード例 #4
0
        public void ParseRaw(byte[] b, int length)
        {
            if (rawData)
            {
                if (length < 0)
                    throw new System.ArgumentOutOfRangeException("length has to be above zero");

                // Do we have content left that we need to convert?
                if (this.received.Length > 0)
                {
                    byte[] old = this.Encoding.GetBytes(this.received);
            #if !COMPACT_FRAMEWORK
                    long size = (long)length + old.LongLength;

                    byte[] tmp = new byte[size];
                    System.Array.Copy(old, 0, tmp, 0, old.LongLength);
                    if (b != null)
                        System.Array.Copy(b, 0, tmp, old.LongLength, (long)length);
            #else
                    int size = length + old.Length;

                    byte[] tmp = new byte[size];
                    System.Array.Copy(old, 0, tmp, 0, old.Length);
                    if (b != null)
                        System.Array.Copy(b, 0, tmp, old.Length, length);
            #endif
                    b = tmp;
                    length += old.Length;
                    received = string.Empty;
                }

                // Do we have a working byte array?
                if (b != null && length != 0)
                {
                    BinaryMessage conMsg = new BinaryMessage(trans, b, length);
                    // Plugin handling here
                    FmdcEventArgs e = new FmdcEventArgs(Actions.CommandIncomming, conMsg);
                    MessageReceived(trans, e);
                    if (!e.Handled)
                    {
                        if (this.download)
                        {
                            if (trans.DownloadItem != null && trans.CurrentSegment.Index != -1)
                            {
                                if (trans.CurrentSegment.Length < length)
                                {
                                    trans.Disconnect("You are sending more then i want.. Why?!");
                                    return;
                                }

                                if (trans.CurrentSegment.Position == 0 && !Utils.FileOperations.PathExists(trans.DownloadItem.ContentInfo.Get(ContentInfo.STORAGEPATH)))
                                {
                                    Utils.FileOperations.AllocateFile(trans.DownloadItem.ContentInfo.Get(ContentInfo.STORAGEPATH), trans.DownloadItem.ContentInfo.Size);
                                }

                                // Create the file.
                                //using (System.IO.FileStream fs = System.IO.File.OpenWrite(trans.DownloadItem.ContentInfo.Get(ContentInfo.STORAGEPATH)))
                                using (System.IO.FileStream fs = new System.IO.FileStream(trans.DownloadItem.ContentInfo.Get(ContentInfo.STORAGEPATH), System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.Write))
                                {
                                    try
                                    {
                                        // Lock this segment of file
                                        fs.Lock(trans.CurrentSegment.Start, trans.CurrentSegment.Length);
                                        // Set position
                                        fs.Position = trans.CurrentSegment.Start + trans.CurrentSegment.Position;
                                        // Write this byte array to file
                                        fs.Write(b, 0, length);
                                        trans.CurrentSegment.Position += length;
                                        // Saves and unlocks file
                                        fs.Flush();
                                        fs.Unlock(trans.CurrentSegment.Start, trans.CurrentSegment.Length);
                                    }
                                    catch (System.Exception exp)
                                    {
                                        //trans.DownloadItem.Cancel(trans.CurrentSegment.Index, trans.Source);
                                        trans.Disconnect("Exception thrown when trying to write to file: " + exp.ToString());
                                        return;
                                    }
                                    finally
                                    {
                                        fs.Dispose();
                                        fs.Close();
                                    }
                                    if (trans.CurrentSegment.Position >= trans.CurrentSegment.Length)
                                    {
                                        trans.DownloadItem.Finished(trans.CurrentSegment.Index, trans.Source);
                                        //// Searches for a download item and a segment id
                                        // Request new segment from user. IF we have found one. ELSE disconnect.
                                        if (GetSegment(true))
                                        {
                                            OnDownload();
                                        }
                                        else
                                            trans.Disconnect("All content downloaded");
                                    }
                                }
                            }
                        }
                        else
                        {
                            trans.Disconnect("I dont want to download from you. Shove off!");
                        }
                    }
                }
            }
            else
            {
                ParseRaw(this.Encoding.GetString(b, 0, length));
            }
        }
コード例 #5
0
 public override void  Release()
 {
     lock (this)
     {
         try
         {
             if (IsLocked())
             {
                 try
                 {
                     channel.Unlock(0, channel.Length);
                 }
                 finally
                 {
                     lock_Renamed = false;
                     try
                     {
                         channel.Close();
                     }
                     finally
                     {
                         channel = null;
                         try
                         {
                             f.Close();
                         }
                         finally
                         {
                             f = null;
                             lock (LOCK_HELD)
                             {
                                 LOCK_HELD.Remove(path.FullName);
                             }
                         }
                     }
                 }
                 bool tmpBool;
                 if (System.IO.File.Exists(path.FullName))
                 {
                     System.IO.File.Delete(path.FullName);
                     tmpBool = true;
                 }
                 else if (System.IO.Directory.Exists(path.FullName))
                 {
                     System.IO.Directory.Delete(path.FullName);
                     tmpBool = true;
                 }
                 else
                 {
                     tmpBool = false;
                 }
                 bool generatedAux = tmpBool;
             }
         }
         catch (System.IO.IOException e)
         {
             // Not sure how to better message/handle this without
             // changing API?
             throw new System.SystemException("", e);
         }
     }
 }