/// <summary> /// 广播客户端信息 /// </summary> public void UPDSendMessage(object message) { UdpClient udpClient = null; try { udpClient = new UdpClient(); var byteData = message is byte[] ? message as byte[] : StructHelper.SerializeObject(message); //先处理发送的数据,加上一字节头+四字节长度 var msgBuffer = new byte[byteData.Length + 5]; msgBuffer[0] = Preamble; msgBuffer[1] = (byte)(byteData.Length >> 24); msgBuffer[2] = (byte)(byteData.Length >> 16); msgBuffer[3] = (byte)(byteData.Length >> 8); msgBuffer[4] = (byte)byteData.Length; for (var i = 0; i < byteData.Length; i++) { msgBuffer[i + 5] = byteData[i]; } udpClient.Send(msgBuffer, msgBuffer.Length, ServerAddress); } catch (Exception ex) { OnError(ex.Message, null); } finally { if (udpClient != null) { udpClient.Close(); } } }
/// <summary> /// 构造初始化图像流 /// </summary> public RawImage(Bitmap image) { if (image != null) { width = image.Width; height = image.Height; format = image.PixelFormat; ByteData = StructHelper.ImageRawData(image); } }
/// <summary> /// 创建SQL参数 /// </summary> internal static DbParameter AddParameter(string column, object value, Type type, Type ptype) { var param = (DbParameter)Activator.CreateInstance(ptype); param.ParameterName = string.Format("@{0}", column); switch (type.Name) { case nameof(DateTime): var time = (DateTime)value; if (TConfig.IUtcTime) { time = time.ToUniversalTime(); } if (time == DateTime.MinValue) { param.Value = DBNull.Value; } else { param.Value = time; } break; case nameof(Image): case nameof(Bitmap): param.Value = StructHelper.SerializeImage((Image)value); param.DbType = DbType.Binary; break; case nameof(String): if ((string)value == string.Empty) { param.Value = DBNull.Value; } else { param.Value = value; } break; default: param.Value = value; break; } if (param.Value == null) { param.Value = DBNull.Value; } return(param); }
/// <summary> /// 反序列化并处理消息 /// </summary> private void HandleMessage(byte[] buffer, IPEndPoint ipAddress) { object message; try { message = StructHelper.DeserializeObject(buffer); } catch (Exception ex) { ex.Log(); message = buffer; } OnMessage(message, ipAddress); }