// Decompiled with JetBrains decompiler // Type: Rei.Net.HttpServer.LineReadableSocketStream // Assembly: Rei.Net.HttpServer, Version=1.13.2.9297, Culture=neutral, PublicKeyToken=null // MVID: 6174F8E9-E7BA-46AD-8F2E-196645884F28 // Assembly location: F:\Eigene Dateien\Dropbox\portable Collection\Progs\CarotDAV\Rei.Net.HttpServer.dll using Microsoft.VisualBasic.CompilerServices; using System; using System.Diagnostics; using System.IO; using System.Net.Sockets; using System.Runtime.CompilerServices; using System.Text; using System.Threading; namespace Rei.Net.HttpServer { public class LineReadableSocketStream : Stream { private Socket _soc; private long _readbytes; private long _writebytes; private bool _shutdownreceive; private bool _shutdownsend; private int _receive_bufcount; private byte[] _receive_buf; private int _readtimeout; private int _writetimeout; private bool disposedValue; public LineReadableSocketStream(Socket basesocket) { this._shutdownreceive = false; this._shutdownsend = false; this._readtimeout = 10000; this._writetimeout = 10000; this.disposedValue = false; if (basesocket == null) throw new ArgumentNullException(); this._soc = basesocket; this._receive_buf = new byte[65536]; } public void Shutdown(SocketShutdown how) { if (this._soc == null) throw new ObjectDisposedException(this.ToString()); this._soc.Shutdown(how); if (how == SocketShutdown.Both) { this._shutdownreceive = true; this._shutdownsend = true; } else if (how == SocketShutdown.Receive) { this._shutdownreceive = true; } else { if (how != SocketShutdown.Send) return; this._shutdownsend = true; } } public override bool CanSeek { get { return false; } } public override void Flush() { } public override long Seek(long offset, SeekOrigin origin) { throw new NotImplementedException(); } public override void SetLength(long value) { throw new NotImplementedException(); } public override long Length { get { throw new NotImplementedException(); } } public override long Position { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } } public long ReadBytes { get { return this._readbytes; } } public override bool CanRead { get { return !this._shutdownreceive; } } public override int ReadTimeout { get { return this._readtimeout; } set { this._readtimeout = value; } } public override int Read(byte[] buffer, int offset, int count) { IAsyncResult asyncresult = this.BeginRead(buffer, offset, count, (AsyncCallback) null, (object) null); if (!asyncresult.AsyncWaitHandle.WaitOne(this.ReadTimeout, false)) throw new IOException(); return this.EndRead(asyncresult); } public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object asyncstate) { if (this._soc == null) throw new ObjectDisposedException(this.ToString()); if (count < 0) throw new ArgumentOutOfRangeException(); int num = this._receive_bufcount; if (num > count) num = count; Array.Copy((Array) this._receive_buf, 0, (Array) buffer, offset, num); this._receive_bufcount = checked (this._receive_bufcount - num); Array.Copy((Array) this._receive_buf, num, (Array) this._receive_buf, 0, this._receive_bufcount); MyAsyncResult myAsyncResult = new MyAsyncResult(callback, RuntimeHelpers.GetObjectValue(asyncstate)); myAsyncResult.ReturnValue = (object) num; if (count == num) myAsyncResult.Complete((Exception) null); else this._soc.BeginReceive(buffer, checked (offset + num), checked (count - num), SocketFlags.None, new AsyncCallback(this.OnReceive), (object) myAsyncResult); return (IAsyncResult) myAsyncResult; } private void OnReceive(IAsyncResult asyncresult) { MyAsyncResult asyncState = (MyAsyncResult) asyncresult.AsyncState; if (!asyncresult.CompletedSynchronously) asyncState.SetAsync(); int num; try { num = this._soc.EndReceive(asyncresult); } catch (Exception ex1) { ProjectData.SetProjectError(ex1); Exception ex2 = ex1; asyncState.Complete(ex2); ProjectData.ClearProjectError(); return; } Interlocked.Add(ref this._readbytes, (long) num); asyncState.ReturnValue = (object) checked ((int) asyncState.ReturnValue + num); asyncState.Complete(); } public override int EndRead(IAsyncResult asyncresult) { MyAsyncResult myAsyncResult = asyncresult as MyAsyncResult; myAsyncResult.AsyncEnd(); return (int) myAsyncResult.ReturnValue; } public string ReadLine() { IAsyncResult asyncresult = this.BeginReadLine((AsyncCallback) null, (object) null); if (!asyncresult.AsyncWaitHandle.WaitOne(this.ReadTimeout, false)) throw new IOException(); return this.EndReadLine(asyncresult); } public IAsyncResult BeginReadLine(AsyncCallback callback, object asyncstate) { if (this._soc == null) throw new ObjectDisposedException(this.ToString()); MyAsyncResult myAsyncResult = new MyAsyncResult(callback, RuntimeHelpers.GetObjectValue(asyncstate)); int num1 = 0; int num2 = checked (this._receive_bufcount - 1); int index = num1; while (index <= num2) { if ((int) this._receive_buf[index] == 10) { byte[] numArray = index <= 0 || (int) this._receive_buf[checked (index - 1)] != 13 ? new byte[checked (index - 1 + 1)] : new byte[checked (index - 2 + 1)]; Array.Copy((Array) this._receive_buf, 0, (Array) numArray, 0, numArray.Length); this._receive_bufcount = checked (this._receive_bufcount - index + 1); Array.Copy((Array) this._receive_buf, checked (index + 1), (Array) this._receive_buf, 0, this._receive_bufcount); myAsyncResult.ReturnValue = (object) numArray; myAsyncResult.Complete(); return (IAsyncResult) myAsyncResult; } checked { ++index; } } if (checked (this._receive_buf.Length - this._receive_bufcount) < 1024) this._receive_buf = (byte[]) Utils.CopyArray((Array) this._receive_buf, (Array) new byte[checked (this._receive_buf.Length * 2 - 1 + 1)]); this._soc.BeginReceive(this._receive_buf, this._receive_bufcount, checked (this._receive_buf.Length - this._receive_bufcount), SocketFlags.None, new AsyncCallback(this.OnReceiveToBuffer), (object) myAsyncResult); return (IAsyncResult) myAsyncResult; } private void OnReceiveToBuffer(IAsyncResult asyncresult) { MyAsyncResult asyncState = (MyAsyncResult) asyncresult.AsyncState; if (!asyncresult.CompletedSynchronously) asyncState.SetAsync(); int num1; try { num1 = this._soc.EndReceive(asyncresult); } catch (Exception ex1) { ProjectData.SetProjectError(ex1); Exception ex2 = ex1; asyncState.Complete(ex2); ProjectData.ClearProjectError(); return; } if (num1 == 0) { asyncState.ReturnValue = (object) null; asyncState.Complete(); } else { Interlocked.Add(ref this._readbytes, (long) num1); this._receive_bufcount = checked (this._receive_bufcount + num1); int num2 = 0; int num3 = checked (this._receive_bufcount - 1); int index = num2; while (index <= num3) { if ((int) this._receive_buf[index] == 10) { byte[] numArray = index <= 0 || (int) this._receive_buf[checked (index - 1)] != 13 ? new byte[checked (index - 1 + 1)] : new byte[checked (index - 2 + 1)]; Array.Copy((Array) this._receive_buf, 0, (Array) numArray, 0, numArray.Length); this._receive_bufcount = checked (this._receive_bufcount - index + 1); Array.Copy((Array) this._receive_buf, checked (index + 1), (Array) this._receive_buf, 0, this._receive_bufcount); asyncState.ReturnValue = (object) numArray; asyncState.Complete(); return; } checked { ++index; } } if (checked (this._receive_buf.Length - this._receive_bufcount) < 1024) this._receive_buf = (byte[]) Utils.CopyArray((Array) this._receive_buf, (Array) new byte[checked (this._receive_buf.Length * 2 - 1 + 1)]); this._soc.BeginReceive(this._receive_buf, this._receive_bufcount, checked (this._receive_buf.Length - this._receive_bufcount), SocketFlags.None, new AsyncCallback(this.OnReceiveToBuffer), (object) asyncState); } } public string EndReadLine(IAsyncResult asyncresult) { MyAsyncResult myAsyncResult = (MyAsyncResult) asyncresult; myAsyncResult.AsyncEnd(); if (myAsyncResult.ReturnValue == null) return (string) null; return Encoding.UTF8.GetString((byte[]) myAsyncResult.ReturnValue); } public long WriteBytes { get { return this._writebytes; } } public override bool CanWrite { get { return !this._shutdownsend; } } public override int WriteTimeout { get { return this._writetimeout; } set { this._writetimeout = value; } } public override void Write(byte[] buffer, int offset, int count) { IAsyncResult asyncResult = this.BeginWrite(buffer, offset, count, (AsyncCallback) null, (object) null); if (!asyncResult.AsyncWaitHandle.WaitOne(this.WriteTimeout, false)) throw new IOException(); this.EndWrite(asyncResult); } public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state) { if (this._soc == null) throw new ObjectDisposedException(this.ToString()); MyAsyncResult myAsyncResult = new MyAsyncResult(callback, RuntimeHelpers.GetObjectValue(state)); LineReadableSocketStream.StreamReadWriteParameter readWriteParameter = new LineReadableSocketStream.StreamReadWriteParameter(); if (count < 0) throw new ArgumentOutOfRangeException(); if (count == 0) { myAsyncResult.ReturnValue = (object) 0; myAsyncResult.Complete(); } else { IAsyncResult asyncResult; try { readWriteParameter.buffer = buffer; readWriteParameter.offset = offset; readWriteParameter.count = count; myAsyncResult.Tag = (object) readWriteParameter; this._soc.BeginSend(readWriteParameter.buffer, readWriteParameter.offset, readWriteParameter.count, SocketFlags.None, new AsyncCallback(this.OnSend), (object) myAsyncResult); goto label_8; } catch (Exception ex1) { ProjectData.SetProjectError(ex1); Exception ex2 = ex1; myAsyncResult.Complete(ex2); asyncResult = (IAsyncResult) myAsyncResult; ProjectData.ClearProjectError(); } return asyncResult; } label_8: return (IAsyncResult) myAsyncResult; } private void OnSend(IAsyncResult asyncresult) { MyAsyncResult asyncState = (MyAsyncResult) asyncresult.AsyncState; LineReadableSocketStream.StreamReadWriteParameter tag = (LineReadableSocketStream.StreamReadWriteParameter) asyncState.Tag; if (!asyncresult.CompletedSynchronously) asyncState.SetAsync(); try { int num = this._soc.EndSend(asyncresult); Interlocked.Add(ref this._writebytes, (long) num); LineReadableSocketStream.StreamReadWriteParameter readWriteParameter1 = tag; readWriteParameter1.count = checked (readWriteParameter1.count - num); LineReadableSocketStream.StreamReadWriteParameter readWriteParameter2 = tag; readWriteParameter2.offset = checked (readWriteParameter2.offset + num); if (tag.count > 0) { this._soc.BeginSend(tag.buffer, tag.offset, tag.count, SocketFlags.None, new AsyncCallback(this.OnSend), (object) asyncState); return; } } catch (Exception ex1) { ProjectData.SetProjectError(ex1); Exception ex2 = ex1; asyncState.Complete(ex2); ProjectData.ClearProjectError(); return; } asyncState.Complete(); } public override void EndWrite(IAsyncResult asyncResult) { ((MyAsyncResult) asyncResult).AsyncEnd(); } protected override void Dispose(bool disposing) { if (!this.disposedValue && disposing && this._soc != null) { this._soc.Close(); this._soc = (Socket) null; } this.disposedValue = true; base.Dispose(disposing); } private class StreamReadWriteParameter { public byte[] buffer; public int offset; public int count; [DebuggerNonUserCode] public StreamReadWriteParameter() { } } } }