Beispiel #1
0
        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("未想定命令");
            }
        }
Beispiel #2
0
 private void _user_TextMessageReceived(object sender, TextMessageReceivedEventArgs args)
 {
     OutputLog("接受文本消息: user="******", msg=" + args.Message);
 }
Beispiel #3
0
 private void _user_TextMessageReceived(object sender, TextMessageReceivedEventArgs args)
 {
     this.userListBox.AddReceivedTextMessage(args.User, args.ID, args.Message);
     UpdateUnreadMessageUI();
 }