private static void EncodeSendFileRequestExtend(BinaryWriter wtr, object extendObj) { UdpPacketSendFileRequestExtend extend = extendObj as UdpPacketSendFileRequestExtend; if (extend == null) { throw new Exception("[EncodeImageExtend]未想定附加包"); } using (MemoryStream ms = new MemoryStream()) { using (BinaryWriter bw = new BinaryWriter(ms)) { LanFile file = extend.File; bw.Write(file.Name); bw.Write(file.Length); bw.Write(file.IsFolder); bw.Close(); ms.Close(); byte[] buf = ms.ToArray(); byte[] enBuf = SecurityFactory.Encrypt(buf, extend.EncryptKey); wtr.Write(enBuf.Length); wtr.Write(enBuf); } } }
private static UdpPacketSendFileRequestExtend ResolveSendFileRequestExtend(BinaryReader rdr, byte[] priKey) { int len = rdr.ReadInt32(); byte[] buf = rdr.ReadBytes(len); byte[] deBuf = SecurityFactory.Decrypt(buf, priKey); using (MemoryStream ms = new MemoryStream(deBuf)) { using (BinaryReader bw = new BinaryReader(ms)) { LanFile file = new LanFile(); file.Name = bw.ReadString(); file.Length = bw.ReadInt32(); file.IsFolder = bw.ReadBoolean(); bw.Close(); ms.Close(); UdpPacketSendFileRequestExtend extend = new UdpPacketSendFileRequestExtend(); extend.File = file; return(extend); } } }
public long SendFile(LanUser user, string path, bool bImage = false) { //发送请求 UdpPacket packet = new UdpPacket(); packet.Address = user.IP; packet.Port = user.Port; packet.ToMAC = user.MAC; packet.Command = UdpPacket.CMD_SEND_FILE_REQUEST | UdpPacket.CMD_OPTION_NEED_RESPONSE; if (bImage) { packet.Command |= UdpPacket.CMD_OPTION_SEND_FILE_IMAGE; } packet.FromMAC = this.MAC; UdpPacketSendFileRequestExtend extend = new UdpPacketSendFileRequestExtend(); extend.EncryptKey = user.SecurityKeys.Public; extend.File = new LanFile(path); packet.Extend = extend; _client.Send(packet); //保存要发送文件一览 TransportFile file = new TransportFile(packet.ID, user.MAC, user.IP, user.Port, user.SecurityKeys.Public, extend.File); _fileTransTcpListener.AddTransportFile(file); return(file.ID); }
private void ReceivePacketEvent(object sender, UdpClientReceiveEventArgs args) { UdpPacket packet = args.Packet as UdpPacket; if (packet.CheckSendResponse) { #region 需要回应收到包 if (packet.Version == Packet.VERSION) { UdpPacket packetRsp = new UdpPacket(); packetRsp.Address = packet.Address; packetRsp.Port = packet.Port; packet.ToMAC = packet.ToMAC; packetRsp.FromMAC = this.MAC; packetRsp.Command = UdpPacket.CMD_RESPONSE; UdpPacketResponseExtend extend = new UdpPacketResponseExtend(); extend.ID = packet.ID; packetRsp.Extend = extend; _client.Send(packetRsp); } #endregion } if (packet.CMD == UdpPacket.CMD_NOTHING) { //do nothing } else if (packet.CMD == UdpPacket.CMD_ENTRY) { #region CMD_ENTRY 对方上线 UpdateState updateState = UpdateContacter(packet); //TODO 应该更新所有的,但是跨网的时候好像发送的byte太长,不能正常发送暂且去掉头像 //SendUpdateStatePacket(LanIM.UpdateState.All, // packet.Address, packet.Port, packet.FromMAC); SendUpdateStatePacket(LanIM.UpdateState.NickName | LanIM.UpdateState.PublicKey | LanIM.UpdateState.Status, packet.Address, packet.Port, packet.FromMAC); if (UserEntry != null) { LanUser user = this[packet.FromMAC]; UserStateChangeEventArgs stateArgs = new UserStateChangeEventArgs(user, updateState); UserEntry(this, stateArgs); } #endregion } else if (packet.CMD == UdpPacket.CMD_EXIT) { #region CMD_EXIT if (UserExit != null) { LanUser user = this[packet.FromMAC]; user.Status = UserStatus.Offline; UserStateChangeEventArgs stateArgs = new UserStateChangeEventArgs(user, LanIM.UpdateState.Status); UserExit(this, stateArgs); } #endregion } else if (packet.CMD == UdpPacket.CMD_STATE) { #region CMD_STATE UpdateState updateState = UpdateContacter(packet); if (UserStateChange != null) { LanUser user = this[packet.FromMAC]; UserStateChangeEventArgs stateArgs = new UserStateChangeEventArgs(user, updateState); UserStateChange(this, stateArgs); } #endregion } else if (packet.CMD == UdpPacket.CMD_SEND_TEXT) { #region CMD_SEND_TEXT UdpPacketTextExtend extend = packet.Extend as UdpPacketTextExtend; if (TextMessageReceived != null) { LanUser user = this[packet.FromMAC]; TextMessageReceivedEventArgs stateArgs = new TextMessageReceivedEventArgs(user, packet.ID, extend.Text); TextMessageReceived(this, stateArgs); } #endregion } else if (packet.CMD == UdpPacket.CMD_SEND_IMAGE) { #region CMD_SEND_IMAGE UdpPacketImageExtend extend = packet.Extend as UdpPacketImageExtend; if (ImageReceived != null) { LanUser user = this[packet.FromMAC]; ImageReceivedEventArgs stateArgs = new ImageReceivedEventArgs(user, packet.ID, extend.Image); ImageReceived(this, stateArgs); } #endregion } else if (packet.CMD == UdpPacket.CMD_SEND_FILE_REQUEST) { #region CMD_SEND_FILE_REQUEST LanUser user = this[packet.FromMAC]; UdpPacketSendFileRequestExtend extend = packet.Extend as UdpPacketSendFileRequestExtend; TransportFile file = new TransportFile(packet.ID, user.MAC, user.IP, user.Port, user.SecurityKeys.Public, extend.File); if ((packet.Command & UdpPacket.CMD_OPTION_SEND_FILE_IMAGE) != 0) { //接收图像 if (ImageReceived != null) { this.ReceiveImage(user, file); } } else { if (FileTransportRequested != null) { FileTransportRequestedEventArgs stateArgs = new FileTransportRequestedEventArgs(user, file); FileTransportRequested(this, stateArgs); } } #endregion } else if (packet.CMD == UdpPacket.CMD_RESPONSE) { UdpPacketResponseExtend extend = packet.Extend as UdpPacketResponseExtend; _client.NotifySendPacketSuccess(extend.ID); } else { throw new Exception("未想定命令"); } }