示例#1
0
 /// <summary>
 /// Добавить данные ts к текущему объекту.
 /// </summary>
 public void AddTS(TransferState ts)
 {
     lock(lockobj)
         {
         this.totalreadbytes += ts.totalreadbytes;
         this.totalwritebytes += ts.totalwritebytes;
         }
 }
 private void GetCurrentClientStatus(out TransferState ts)
 {
     ts = new TransferState();
     lock (clientlist)
     {
         foreach (IClientWorker cw in clientlist)
         {
             ts.AddTS(cw.GetTS());
         }
     }
 }
        /// <summary>
        /// Обработчик данных. Чтение/Запись данных осуществляется синхронно.
        /// </summary>
        protected override EventWaitHandle DoLoopInner(NetworkStream ns, TransferState ts)
        {
            byte[] bufferread = new byte[4096];
            byte[] bufferwrite = null;
            int count;
            ts.Reset();

            while ((count = ns.Read(bufferread, 0, bufferread.Length)) > 0)
            {
                ts.AddReadBytes(count);
            #if VERBOSE
                //if (totalreadbytes % 10000 == 0) ConsoleLogger.LogMessage("read: " + totalreadbytes);
            #endif

                bool EOFPresent = HandleBuffer(bufferread, count,answer, out bufferwrite);
                ns.Write(bufferwrite, 0, bufferwrite.Length);

                ts.AddWriteBytes(bufferwrite.Length);
                if (EOFPresent) break;
            }
            return new ManualResetEvent(true);
        }
示例#4
0
        private void TestHandleBuffer(int rptcnt)
        {
            StringBuilder sb = new StringBuilder();
            string bufcmd = "get bbbbcaacba";
            int cmdcnt = 1024 / (bufcmd.Length + 2);
            for (int i = 0; i < cmdcnt; i++)
            {
                sb.Append(bufcmd + EOL);
            }

            byte[] buffer = Encoding.ASCII.GetBytes(sb.ToString());
            byte[] answer_bytes = null;
            StringBuilder answer = new StringBuilder();
            TransferState testts = new TransferState();
            TimerUtil tu = new TimerUtil();

            for (int hbi = 1; hbi < rptcnt; hbi++)
            {
                HandleBuffer(buffer, buffer.Length, answer, out answer_bytes);
                ts.AddReadBytes(buffer.Length);
                ts.AddWriteBytes(answer_bytes.Length);
            }
            ConsoleLogger.LogMessage(String.Format("TestHandleBuffer Cnt: {0:D} {1:S} {2:S}", rptcnt, tu.GetInterval(), ts.ToStringTS(tu.GetInterval())));
            //throw new Exception("1");
        }
示例#5
0
 /// <summary>
 /// Метод должен быть переопределен в потомках
 /// </summary>
 protected virtual EventWaitHandle DoLoopInner(NetworkStream ns, TransferState ts)
 {
     return null;
 }
        protected override EventWaitHandle DoLoopInner(NetworkStream ns, TransferState ts)
        {
            byte[] bufferread = new byte[1024*1];
            byte[] bufferwrite =null;
            int count;

            ManualResetEvent mreDone = new ManualResetEvent(false);

            AsyncCallback readcallback=null;

            readcallback = ar_c =>
            {
                try
                {
                    count = ns.EndRead(ar_c);
                    ts.AddReadBytes(count);
            #if VERBOSE
                    //if (ts.totalreadbytes % 10000 == 0) ConsoleLogger.LogMessage("read: " + ts.totalreadbytes);
            #endif
                    bool EOFPresent = false;
                    if (count > 0)
                    {

                        EOFPresent = HandleBuffer(bufferread, count, answer, out bufferwrite);
                        ns.Write(bufferwrite, 0, bufferwrite.Length);

                        if (bufferwrite != null)
                        {
                            ts.AddWriteBytes(bufferwrite.Length);
                        }
                    }

                    if (EOFPresent == false && count>0)
                    {
                        ns.BeginRead(bufferread, 0, bufferread.Length, readcallback, ts);
                    }
                    else
                    {
                        mreDone.Set();
                    }
                }
                catch (IOException ioe)
                {
                    if (ioe.InnerException != null)
                    {
                        SocketException se = ioe.InnerException as SocketException;
                        if (se != null)
                        {
                            if (se.SocketErrorCode == SocketError.ConnectionReset)
                            {
                                ConsoleLogger.LogMessage("Client closed connection!");
                                mreDone.Set();
                                return;
                            }
                        }
                        ObjectDisposedException ode = ioe.InnerException as ObjectDisposedException;
                        if (ode != null)
                        {
                            ConsoleLogger.LogMessage("Client closed connection.");
                            mreDone.Set();
                            return;
                        }
                    }
                    throw ioe;
                }
                catch (Exception ex)
                {
                    ConsoleLogger.LogMessage("Error in readcallback: " + ex.ToString());
                    mreDone.Set();
                }
            };

            //начинаем асинхронное чтение данных
            ns.BeginRead(bufferread, 0, bufferread.Length, readcallback, ts);

            //ожидаем завершения обработки
            mreDone.WaitOne();

            //обработка завершена
            ConsoleLogger.LogMessage("Thread shutdown!");
            return mreDone;
        }
示例#7
0
 /// <summary>
 /// Создает новый объект и инициализирует переданным объектом
 /// </summary>               
 public TransferState(TransferState ts)
 {
     Reset();
         AddTS(ts);
 }