/// <summary> /// Starts listening and handling requests from the client /// </summary> public void Start() { messageBuffer = new HttpMessageBuffer(); messageBuffer.MessageReceived += messageBuffer_MessageReceived; buffer = new byte[65536]; lock (socketLock) { if (socket == null) { return; } try { //Logger.Debug("HttpServer: Listening for new requests"); inputStream.BeginRead(buffer, 0, buffer.Length, onInputReadComplete, null); } catch (Exception ex) { Logger.Error("HttpParser: Failed to start -", ex); inputStream.Close(); outputStream.Close(); socket.Close(); socket = null; } } }
void OnRead(IAsyncResult result) { try { if (status == HttpRequestStatus.Aborted) { return; } BufferedStream responseBufferedStream = (BufferedStream)result.AsyncState; int length = responseBufferedStream.EndRead(result); byte[] newBuffer = new byte[responseCompleteBuffer.Length + length]; Array.Copy(responseCompleteBuffer, newBuffer, responseCompleteBuffer.Length); Array.Copy(responseBuffer, 0, newBuffer, responseCompleteBuffer.Length, length); responseCompleteBuffer = newBuffer; // Update status since responseCompleteBuffer size has changed UpdateStatus(HttpRequestStatus.Downloading); if (length > 0) { responseBufferedStream.BeginRead(responseBuffer, 0, BUFFER_SIZE, new AsyncCallback(OnRead), responseBufferedStream); } else { responseBufferedStream.Close(); UpdateStatus(HttpRequestStatus.Done); } } catch (Exception ex) { UpdateError(ex.Message); } }
void OnGetResponse(IAsyncResult result) { try { if (status == HttpRequestStatus.Aborted) { return; } UpdateStatus(HttpRequestStatus.Downloading); WebResponse response = request.EndGetResponse(result); Stream responseStream = response.GetResponseStream(); BufferedStream responseBufferedStream = new BufferedStream(responseStream, BUFFER_SIZE); responseBuffer = new byte[BUFFER_SIZE]; responseBufferedStream.BeginRead(responseBuffer, 0, BUFFER_SIZE, new AsyncCallback(OnRead), responseBufferedStream); } catch (Exception ex) { UpdateError(ex.Message); } }
public void Download() { response = request.GetResponse() as FtpWebResponse; stream = new BufferedStream(response.GetResponseStream()); msStream = new BufferedStream(ms); //Exception. stream.BeginRead(buffer, 0, buffer.Length, EndRead, stream); }
/// <summary> /// Extends BeginRead so that when a state object is not needed, null does not need to be passed. /// <example> /// stream.BeginRead(buffer, offset, count, callback); /// </example> /// </summary> public static IAsyncResult BeginRead(this BufferedStream stream, Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback) { if (stream == null) { throw new ArgumentNullException("stream"); } return(stream.BeginRead(buffer, offset, count, callback, null)); }
private void ReadBytes() { readCallback = new System.AsyncCallback(ReadBufComplete); if (buffer.AvailableDataCount <= 1) { buffer.ResetUsingLastByte(); } bstream.BeginRead(buffer.Buffer, buffer.NextWriteOffset, buffer.WritableDataCount, readCallback, null); }
private void OnEndConnect(IAsyncResult ar) { clientSocket.EndConnect(ar); clientSocket.NoDelay = true; // Improve performance stream = new BufferedStream(new NetworkStream(clientSocket)); stream.BeginRead(readBuffer, 0, readBuffer.Length, OnRead, null); Debug.Log("Connected to server"); }
protected void OnEndAccept(IAsyncResult ar) { Debug.Log("Received request"); clientSocket = serverSocket.EndAccept(ar); clientSocket.NoDelay = true; // Improve performance stream = new BufferedStream(new NetworkStream(clientSocket)); stream.BeginRead(readBuffer, 0, readBuffer.Length, OnRead, null); Debug.Log("Accepted client"); }
/// <summary> /// Extends BeginRead so that buffer offset of 0 and call to Array.Length are not needed. /// <example> /// stream.BeginRead(buffer, callback); /// </example> /// </summary> public static IAsyncResult BeginRead(this BufferedStream stream, Byte[] buffer, AsyncCallback callback) { if (stream == null) { throw new ArgumentNullException("stream"); } if (buffer == null) { throw new ArgumentNullException("buffer"); } return(stream.BeginRead(buffer, 0, buffer.Length, callback)); }
protected void OnEndAccept(IAsyncResult ar) { //Debug.Log("Received request"); clientSocket = serverSocket.EndAccept(ar); clientSocket.NoDelay = true; // Improve performance stream = new BufferedStream(new NetworkStream(clientSocket)); stream.BeginRead(readBuffer, 0, readBuffer.Length, OnRead, null); Connected = true; //Debug.Log("Accepted client"); StopListening(); // Don't listen for additional clients }
private void OnRead(IAsyncResult ar) { Debug.Log("Received"); int length = stream.EndRead(ar); if (length <= 0) { OnDisconnect(); } else { Debug.Log(BitConverter.ToInt16(readBuffer, 0)); } stream.BeginRead(readBuffer, 0, readBuffer.Length, OnRead, null); }
public void EndRead(IAsyncResult result) { int n = stream.EndRead(result); filesize += n; msStream.Write(buffer, 0, n); if (stream.CanRead && n != 0) { stream.BeginRead(buffer, 0, buffer.Length, EndRead, stream); } else { response.Close(); stream.Close(); msStream.Close(); raw = ms.ToArray(); ms.Close(); complete = true; } }
private void OnRead(IAsyncResult ar) { //Debug.Log("Received"); int readLength = stream.EndRead(ar); if (readLength <= 0) { //Debug.Log("Client disconnected"); OnDisconnect(); } else { short bodyLength = BitConverter.ToInt16(readBuffer, 1); byte[] body = new byte[bodyLength]; Array.Copy(readBuffer, 3, body, 0, bodyLength); serializer.Receive(readBuffer[0], body); } stream.BeginRead(readBuffer, 0, readBuffer.Length, OnRead, null); }
// 作用: BufferedStream 是一种对流对象的缓冲,可以减小I/O开销,提高性能 static void Main(string[] args) { // BufferedStream 从本质上来说就是一个装饰流的流类 /* * 构造函数 * BufferedStream(Stream stream, int bufferSize) * BufferedStream( stream, 4096 ) */ /* 属性 : 由于是装饰类,所以获取的属性,实际是被装饰的流的属性 * CanRead * CanSeek * CanTimeout * CanWrite * Length * Position * ReadTimeout * WriteTimeout */ Console.WriteLine("-------------- 属性 ---------------"); using (MemoryStream memoryStream = new MemoryStream()) { using (BufferedStream bufferedStream = new BufferedStream(memoryStream)) { Console.WriteLine(bufferedStream.CanRead); Console.WriteLine(bufferedStream.CanSeek); Console.WriteLine(bufferedStream.CanTimeout); Console.WriteLine(bufferedStream.CanWrite); Console.WriteLine(bufferedStream.Length); Console.WriteLine(bufferedStream.Position); byte bt = 100; for (int i = 0; i < 5000; i++) { bufferedStream.WriteByte(bt); } Console.WriteLine(bufferedStream.Length); Console.WriteLine(bufferedStream.Position); Console.WriteLine(memoryStream.GetBuffer().Length); // 测试说明 流的长度,也是跟 被装饰的流 的类型有关 } } /* * 方法 * 自身的方法 * BeginRead / EndRead * BeginWrite / EndWrite * * 继承Stream ,采用被装饰的流的方法 * CopyTo / CopyToAsync * Close/Dispose * Flush/FlushAsync * * 重写方法,还是被装饰的流的方法 * Read/ReadAsync/ReadByte * Write/WriteAsync/WriteByte * Seek * SetLength */ // BeginWrite / EndWrite Console.WriteLine("-------------- BeginWrite/ EndWrite ---------------"); FileStream fileStream = new FileStream("D:\\Desktop\\1.txt", FileMode.Create); BufferedStream bufferedStream1 = new BufferedStream(fileStream); string str = "abc123你好!+"; byte[] tempBytes = Encoding.UTF8.GetBytes(str); bufferedStream1.BeginWrite(tempBytes, 0, tempBytes.Length, BeginWriteCallBack, bufferedStream1); // BeginRead / EndRead Console.WriteLine("-------------- BeginRead / EndRead ---------------"); byte[] bytes = new byte[] { 100, 101, 102, 103, 104 }; MemoryStream memoryStream1 = new MemoryStream(bytes); Console.WriteLine(memoryStream1.Position); BufferedStream bufferedStream2 = new BufferedStream(memoryStream1); Console.WriteLine(bufferedStream2.Length); Console.WriteLine(bufferedStream2.Position); bufferedStream2.BeginRead(tempBytes1, 0, (int)bufferedStream2.Length, BeginReadCallBack, bufferedStream2); Console.ReadLine(); }
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object?state) => innerStream.BeginRead(buffer, offset, count, callback, state);