void HandleReceivedBuffer(int ReceivedDataSize, SReceivedData ReceivedData) { if (ReceivedDataSize != ReceivedData.Buffer.Length) { ClientSock.BeginReceive(ReceivedData.Buffer, ReceivedDataSize, ReceivedData.Buffer.Length - ReceivedDataSize, SocketFlags.None, BeginReceive_Callback, ReceivedData); } else { if (ReceivedData.NextBufferSize == -1) { ReceivedData.NextBufferSize = BitConverter.ToInt32(ReceivedData.Buffer, 0); ReceivedData.Buffer = new byte[ReceivedData.NextBufferSize]; ClientSock.BeginReceive(ReceivedData.Buffer, 0, ReceivedData.NextBufferSize, SocketFlags.None, BeginReceive_Callback, ReceivedData); } else { if (OnClientReceivedData != null) { OnClientReceivedData.Invoke(ReceivedData.Buffer); } ReceivedData.NextBufferSize = -1; ReceivedData.Buffer = new byte[4]; ClientSock.BeginReceive(ReceivedData.Buffer, 0, ReceivedData.Buffer.Length, SocketFlags.None, BeginReceive_Callback, ReceivedData); } } }
protected virtual void DataReceived(IAsyncResult ar) { ClientSocket = (Socket)ar.AsyncState; int len = ClientSocket.EndReceive(ar, out SocketError error); if (IsRunning) { switch (error) { case SocketError.Success: if (len > 0) { _rcvBuffer.Write(ReceivedBuffer, 0, len); OnClientReceivedData?.Invoke(_rcvBuffer.ToArray()); ReceivedBuffer = new byte[BUFFER_LENGTH]; _rcvBuffer.Dispose(); _rcvBuffer = new MemoryStream(); Receive(); } return; } } else { if (!_leaveRequest) { OnClientDisconnected?.Invoke(); } } }
private void _dataReceived(IAsyncResult ar) { socket = (Socket)ar.AsyncState; int len = socket.EndReceive(ar, out SocketError errorCode); if (isRunning && errorCode == SocketError.Success && len > 0) { dataRcvBuffer = new MemoryStream(); dataRcvBuffer.Write(rcvDataBuffer, 0, len); OnClientReceivedData?.Invoke(dataRcvBuffer); BeginReceive(); return; } Stop(); }
private void ClientReceiveData(byte[] data) { OnClientReceivedData?.Invoke(data); }