private void ClientSocket_AsyncOnClientDataRecived(ClientSocket clientSocket, byte[] ReceivedData, ParseResult parseResult) { string content; if (parseResult.ContentLength == 0) { content = ""; } else { byte[] contentByte = new byte[parseResult.ContentLength]; Buffer.BlockCopy(ReceivedData, sizeof(int), contentByte, 0, contentByte.Length); content = Encoding.UTF8.GetString(contentByte); } AsyncOnClientDataRecived?.Invoke(clientSocket, content); }
private void Receivecallback(IAsyncResult ar) { try { int receivedByte = Client.EndReceive(ar); if (receivedByte == 0) { AsyncOnMessageRecived?.Invoke("Error Handled, Client force disconnected"); Dispose(); } if (!HeaderParsed) { parseResult = PacketParser.HeaderParse(Buffer, (uint)receivedByte); if (parseResult.HeaderParsed == false) { Process(sizeof(int) - receivedByte); } else if (parseResult.ContentLength == parseResult.ReceivedContentLength) { byte[] bufferClone = new byte[Buffer.Length]; System.Buffer.BlockCopy(Buffer, 0, bufferClone, 0, sizeof(int) + parseResult.ContentLength); HeaderParsed = false; AsyncOnClientDataRecived?.Invoke(this, bufferClone, parseResult); Process(sizeof(int)); } else { LeftContentByte = parseResult.ContentLength - parseResult.ReceivedContentLength; ContentOffSet = sizeof(int) + parseResult.ReceivedContentLength; ContentFullBuffer = new byte[sizeof(int) + parseResult.ContentLength]; System.Buffer.BlockCopy(Buffer, 0, ContentFullBuffer, 0, receivedByte); HeaderParsed = true; Process(LeftContentByte); } } else { System.Buffer.BlockCopy(Buffer, 0, ContentFullBuffer, ContentOffSet, receivedByte); LeftContentByte -= receivedByte; ContentOffSet += receivedByte; if (LeftContentByte <= 0) { byte[] bufferClone = new byte[ContentFullBuffer.Length]; System.Buffer.BlockCopy(ContentFullBuffer, 0, bufferClone, 0, sizeof(int) + parseResult.ContentLength); HeaderParsed = false; AsyncOnClientDataRecived?.Invoke(this, bufferClone, parseResult); Process(sizeof(int)); } else { Process(LeftContentByte); } } } catch (SocketException e) { AsyncOnErrMessageRecived?.Invoke(e.Message); AsyncOnMessageRecived?.Invoke("Error Handled, Client force disconnected"); Dispose(); } catch (ObjectDisposedException e) { AsyncOnErrMessageRecived?.Invoke(e.Message); AsyncOnMessageRecived?.Invoke("Error Handled, Client force disconnected"); Dispose(); } }
private void Receivecallback(IAsyncResult ar) { try { int receivedByte = _client.EndReceive(ar); if (receivedByte == 0) { AsyncOnMessageRecived?.Invoke("Error Handled, Client force disconnected"); Dispose(); } if (!_headerParsed) { _parseResult = PacketParser.HeaderParse(_buffer, (uint)receivedByte); if (_parseResult.HeaderParsed == false) { Process(PacketParser.HeaderSize - receivedByte); } else if (_parseResult.ContentLength == _parseResult.ReceivedContentLength) { byte[] bufferClone = new byte[_buffer.Length]; Buffer.BlockCopy(_buffer, 0, bufferClone, 0, PacketParser.HeaderSize + _parseResult.ContentLength); _headerParsed = false; AsyncOnClientDataRecived?.Invoke(this, bufferClone, _parseResult); Process(PacketParser.HeaderSize); } else { _leftContentByte = _parseResult.ContentLength - _parseResult.ReceivedContentLength; _contentOffSet = PacketParser.HeaderSize + _parseResult.ReceivedContentLength; _contentFullBuffer = new byte[PacketParser.HeaderSize + _parseResult.ContentLength]; Buffer.BlockCopy(_buffer, 0, _contentFullBuffer, 0, receivedByte); _headerParsed = true; Process(_leftContentByte); } } else { Buffer.BlockCopy(_buffer, 0, _contentFullBuffer, _contentOffSet, receivedByte); _leftContentByte -= receivedByte; _contentOffSet += receivedByte; if (_leftContentByte <= 0) { byte[] bufferClone = new byte[_contentFullBuffer.Length]; Buffer.BlockCopy(_contentFullBuffer, 0, bufferClone, 0, PacketParser.HeaderSize + _parseResult.ContentLength); _headerParsed = false; AsyncOnClientDataRecived?.Invoke(this, bufferClone, _parseResult); Process(PacketParser.HeaderSize); } else { Process(_leftContentByte); } } } catch (SocketException e) { AsyncOnErrMessageRecived?.Invoke(e.Message); AsyncOnMessageRecived?.Invoke("Error Handled, Client force disconnected"); Dispose(); } catch (ObjectDisposedException e) { AsyncOnErrMessageRecived?.Invoke(e.Message); AsyncOnMessageRecived?.Invoke("Error Handled, Client force disconnected"); Dispose(); } }