public double ToDouble() { double result = double.NaN; try { var byteArray = Convert.FromBase64String(Value); if (DataType != null) { if (TypeCode.Double == DataType.Type) { result = BitConverter.ToInt32(byteArray, 0); } else { MsgLogger.WriteError($"{GetType().Name} - ToDouble", $"Cannot convert base64 value = {Value}, to data type {DataType.Type}!"); } } else { MsgLogger.WriteWarning($"{GetType().Name} - ToDouble", $"Cannot convert base64 value = {Value}, unknown data type!"); result = BitConverter.ToInt32(byteArray, 0); } } catch (Exception e) { MsgLogger.Exception($"{GetType().Name} - ToDouble", e); } return(result); }
public ulong ToULong() { ulong result = ulong.MinValue; try { var byteArray = Convert.FromBase64String(Value); if (DataType != null) { switch (DataType.Type) { case TypeCode.Byte: result = BitConverter.ToChar(byteArray, 0); break; case TypeCode.SByte: result = BitConverter.ToChar(byteArray, 0); break; case TypeCode.UInt16: result = BitConverter.ToUInt16(byteArray, 0); break; case TypeCode.UInt32: result = BitConverter.ToUInt32(byteArray, 0); break; case TypeCode.UInt64: result = BitConverter.ToUInt64(byteArray, 0); break; default: MsgLogger.WriteError($"{GetType().Name} - ToULong", $"Cannot convert base64 value = {Value}, to data type {DataType.Type}!"); break; } } else { MsgLogger.WriteWarning($"{GetType().Name} - ToULong", $"Cannot convert base64 value = {Value}, unknown data type!"); result = BitConverter.ToUInt64(byteArray, 0); } } catch (Exception e) { MsgLogger.Exception($"{GetType().Name} - ToULong", e); } return(result); }
private async Task <string> ReadMessage() { string result = string.Empty; int bufferSize = BufferSize; var buffer = new byte[bufferSize]; var segment = new ArraySegment <byte>(buffer); var fullBuffer = new byte[bufferSize]; int offset = 0; int fullBufferSize = 0; try { if (Socket.State == WebSocketState.Open) { var receiveResult = await Socket.ReceiveAsync(segment, _cancellationTokenSource.Token); while (!receiveResult.CloseStatus.HasValue) { if (receiveResult.EndOfMessage) { if (fullBufferSize > 0) { if (fullBuffer.Length < receiveResult.Count + offset) { Array.Resize(ref fullBuffer, receiveResult.Count + offset); } Array.Copy(buffer, 0, fullBuffer, offset, receiveResult.Count); offset += receiveResult.Count; fullBufferSize += receiveResult.Count; } else { if (fullBuffer.Length < receiveResult.Count) { Array.Resize(ref fullBuffer, receiveResult.Count); } Array.Copy(buffer, 0, fullBuffer, 0, receiveResult.Count); fullBufferSize = receiveResult.Count; } result = Encoding.UTF8.GetString(fullBuffer, 0, fullBufferSize); if (!string.IsNullOrEmpty(result)) { break; } buffer = new byte[bufferSize]; offset = 0; fullBufferSize = 0; } else { if (fullBuffer.Length < receiveResult.Count + offset) { Array.Resize(ref fullBuffer, receiveResult.Count + offset); } Array.Copy(buffer, 0, fullBuffer, offset, receiveResult.Count); offset += receiveResult.Count; fullBufferSize += receiveResult.Count; } if (Socket.State == WebSocketState.Open) { receiveResult = await Socket.ReceiveAsync(new ArraySegment <byte>(buffer), _cancellationTokenSource.Token); } else { MsgLogger.WriteError($"{GetType().Name} - ReadMessage", $"cannot read - socket state = {Socket.State}"); break; } } if (receiveResult.MessageType == WebSocketMessageType.Close && (Socket.State == WebSocketState.CloseReceived || Socket.State == WebSocketState.Open)) { await Socket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "Server closed connection", _cancellationTokenSource.Token); } } else { MsgLogger.WriteWarning($"{GetType().Name} - ReadMessage", $"cannot read - socket state = {Socket.State}"); } } catch (WebSocketException e) { MsgLogger.Exception($"{GetType().Name} - ReadMessage", e.InnerException != null ? e.InnerException : e); } catch (InvalidOperationException e) { MsgLogger.Exception($"{GetType().Name} - ReadMessage", e); } catch (Exception e) { MsgLogger.Exception($"{GetType().Name} - ReadMessage", e); } return(result); }