private void UserListBox_SelectionChanged(object sender, System.EventArgs e) { UserListItem item = this.userListBox.SelectedItem as UserListItem; LanUser user = item.User; if (user == null) { return; } labelUserName.Text = user.NickName; UserChatControl ucc = item.ChatControl; if (ucc == null) { ucc = new UserChatControl(); ucc.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom; ucc.User = _user; ucc.Contacter = user; ucc.SendMessageEnabled = user.Status != UserStatus.Offline; ucc.SendMessage += Ucc_SendMessage; ucc.InitializeLatastMessage(item.WaitDisplayMessages); //显示到界面后,缓存就清理掉 item.WaitDisplayMessages.Clear(); this.Controls.Add(ucc); item.ChatControl = ucc; } ucc.Location = new Point(this.searchBox.Right, searchBox.Top + 1); ucc.Size = new Size(this.ClientSize.Width - this.searchBox.Right - 1, this.ClientSize.Height - searchBox.Top - 2); ucc.BringToFront(); }
private void _user_FileReceiveError(object sender, FileTransportErrorEventArgs args) { TransportFile file = args.File; LanUser user = _user[args.File.MAC]; this.userListBox.SetMessageResult(user.ID, _user.ID, file.ID, false); }
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 UpdateContacter(UserStateChangeEventArgs args) { LanUser user = args.User; if (user.ID == _user.ID) { //自己就忽略掉,本身也不显示在list里面 return; } userListBox.AddOrUpdateUser(user, args.UpdateState); }
private void LoadContacters() { ContacterMapper mapper = new ContacterMapper(); List <Contacter> cs = mapper.Query(); foreach (Contacter contacter in cs) { LanUser user = new LanUser(); user.Contacter = contacter; _user.Contacters.Add(user); } }
private void ReceiveImage(LanUser user, TransportFile file) { file.SavePath = LanConfig.Instance.GetTempFileName(".png"); FileTransportTcpClient client = new FileTransportTcpClient(_context); client.ProgressChanged += ImageReceiveProgressChanged; client.Completed += new FileTransportEventHandler((sender, args) => { Image image = Image.FromFile(file.SavePath); ImageReceivedEventArgs stateArgs = new ImageReceivedEventArgs(user, file.ID, image); ImageReceived(this, stateArgs); }); client.Error += ImageReceiveError; client.Receive(file); }
private void buttonStart_Click(object sender, System.EventArgs e) { _user = new LanUser(); _user.NickName = textBoxNN.Text; _user.UserEntry += _user_UserEntry; _user.UserExit += _user_UserExit; _user.UserStateChange += _user_UserStateChange; _user.Send += _user_Send; _user.TextMessageReceived += _user_TextMessageReceived; _user.ImageReceived += _user_ImageReceived; _user.FileTransportRequested += _user_FileTransportRequested; _user.FileReceiveProgressChanged += _user_FileTransportProgressChanged; _user.FileSendProgressChanged += _user_FileSendProgressChanged; _user.Listen(); buttonStart.Enabled = false; buttonClose.Enabled = true; }
private long SendImageCore(LanUser user, Image image, string imagePath) { string path; if (image != null) { path = LanConfig.Instance.GetTempFileName(".png"); image.Save(path, ImageFormat.Png); } else { path = imagePath; } long len = LanFile.GetFileLength(path); if (len > UdpClientEx.UDP_MAX_BUF_SIZE) { //图像文件过大的话用文件形式发送 return(SendFile(user, path, true)); } else { UdpPacket packet = new UdpPacket(); packet.Address = user.IP; packet.Port = user.Port; packet.ToMAC = user.MAC; packet.Command = UdpPacket.CMD_SEND_IMAGE | UdpPacket.CMD_OPTION_NEED_RESPONSE; packet.FromMAC = this.MAC; UdpPacketImageExtend extend = new UdpPacketImageExtend(); extend.EncryptKey = user.SecurityKeys.Public; extend.Image = image ?? Image.FromFile(path); packet.Extend = extend; _client.Send(packet); if (image != null) { //直接发送图像的话,临时图片就删除 LanFile.Delete(path); } return(packet.ID); } }
public long SendTextMessage(LanUser user, string text) { UdpPacket packet = new UdpPacket(); packet.Address = user.IP; packet.Port = user.Port; packet.ToMAC = user.MAC; packet.Command = UdpPacket.CMD_SEND_TEXT | UdpPacket.CMD_OPTION_NEED_RESPONSE; packet.FromMAC = this.MAC; UdpPacketTextExtend extend = new UdpPacketTextExtend(); extend.EncryptKey = user.SecurityKeys.Public; extend.Text = text; packet.Extend = extend; _client.Send(packet); return(packet.ID); }
private UpdateState UpdateContacter(UdpPacket packet) { UdpPacketStateExtend extend = packet.Extend as UdpPacketStateExtend; LanUser user = this[packet.FromMAC]; UpdateState updateState = 0; if ((packet.Command & UdpPacket.CMD_OPTION_STATE_PUBKEY) != 0) { user.SecurityKeys.Public = extend.PublicKey; updateState |= LanIM.UpdateState.PublicKey; } if ((packet.Command & UdpPacket.CMD_OPTION_STATE_NICKNAME) != 0) { user.NickName = extend.NickName; updateState |= LanIM.UpdateState.NickName; } if ((packet.Command & UdpPacket.CMD_OPTION_STATE_PROFILE_PHOTO) != 0) { ProfilePhotoPool.SetPhoto(user.ID, extend.ProfilePhoto); updateState |= LanIM.UpdateState.Photo; } if ((packet.Command & UdpPacket.CMD_OPTION_STATE_STATUS) != 0) { user.Status = extend.Status; updateState |= LanIM.UpdateState.Status; } if (user.Port != packet.Port) { user.Port = packet.Port; updateState |= LanIM.UpdateState.Port; } if (user.IP != packet.Address) { user.IP = packet.Address; updateState |= LanIM.UpdateState.IP; } return(updateState); }
public LanUser this[string mac] { get { LanUser user = Contacters.Find((u) => { if (u.MAC == mac) { return(true); } return(false); }); if (user == null) { user = new LanUser(); user.MAC = mac; this.Contacters.Add(user); } return(user); } }
private void InitMainUser(SynchronizationContext context) { _user = new LanUser(context); if (LanConfig.Instance.HideStatus) { _user.Status = UserStatus.Offline; } else { _user.Status = UserStatus.Online; } _user.MAC = LanConfig.Instance.MAC; _user.IP = NCIInfo.GetIPAddress(LanConfig.Instance.MAC); _user.BroadcastAddress = LanConfig.Instance.BroadcastAddress; _user.NickName = LanConfig.Instance.NickName; //events _user.UserEntry += _user_UserEntry; _user.UserExit += _user_UserExit; _user.UserStateChange += _user_UserStateChange; _user.Send += _user_Send; _user.TextMessageReceived += _user_TextMessageReceived; _user.ImageReceived += _user_ImageReceived; _user.ImageReceiveError += _user_ImageReceiveError; _user.ImageReceiveProgressChanged += _user_ImageReceiveProgressChanged; _user.FileTransportRequested += _user_FileTransportRequested; _user.FileReceiveProgressChanged += _user_FileReceiveProgressChanged; _user.FileReceiveCompleted += _user_FileReceiveCompleted; _user.FileReceiveError += _user_FileReceiveError; _user.FileSendProgressChanged += _user_FileSendProgressChanged; _user.FileSendCompleted += _user_FileSendCompleted; _user.FileSendError += _user_FileSendError; //加载用户列表 LoadContacters(); }
public UserStateChangeEventArgs(LanUser user, UpdateState updateState) : base(user) { this.UpdateState = updateState; }
public LanIMUserEventArgs(LanUser user) { _user = user; }
public FileTransportRequestedEventArgs(LanUser user, TransportFile file) : base(user) { this._file = file; }
public ImageReceivedEventArgs(LanUser user, long id, Image image) : base(user) { _image = image; }
public TextMessageReceivedEventArgs(LanUser user, long id, string msg) : base(user) { ID = id; Message = msg; }
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("未想定命令"); } }
public long SendImage(LanUser user, Image image) { return(SendImageCore(user, image, null)); }
public long SendImage(LanUser user, string imagePath) { return(SendImageCore(user, null, imagePath)); }