Пример #1
0
        public bool SendFile(FileMessage fileMsg, Client receiver)
        {
            foreach (Client rcvr in dictClientCallBacks.Keys)
            {
                if (rcvr.Name == receiver.Name)
                {
                    try
                    {
                        string fileSizeText = string.Empty;
                        if (fileMsg.Data.Length < 1024)
                        {
                            fileSizeText = string.Format("{0} bytes", fileMsg.Data.Length);
                        }
                        else if ((fileMsg.Data.Length < 1024 * 1024))
                        {
                            fileSizeText = string.Format("{0:F2} KBytes", (float)fileMsg.Data.Length / 1024);
                        }
                        else
                        {
                            fileSizeText = string.Format("{0:F2} MBytes", (float)fileMsg.Data.Length / (1024 * 1024));
                        }

                        ChatMessage msg = new ChatMessage();
                        msg.Sender = fileMsg.Sender;
                        msg.Content = string.Format("I'm sending file. {0}, Size: {1}", fileMsg.FileName, fileSizeText);

                        IChatCallback rcvrCallback = dictClientCallBacks[rcvr];
                        rcvrCallback.ReceiveWhisper(msg, receiver);
                        rcvrCallback.ReceiverFile(fileMsg, receiver);

                        foreach (Client sender in dictClientCallBacks.Keys)
                        {
                            if (sender.Name == fileMsg.Sender)
                            {
                                IChatCallback sndrCallback = dictClientCallBacks[sender];
                                sndrCallback.ReceiveWhisper(msg, receiver);
                                return true;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        //AppLog.ErrorFormat("{0}(): Failed to send file. Sender: {1}, FilePath: {2}, Receiver: {3}, Exception: {4}",
                                           //new StackFrame().GetMethod().Name, fileMsg.Sender, fileMsg.FileName, receiver.Name, ex);
                    }
                }
            }
            return false;
        }
Пример #2
0
        public void Whisper(ChatMessage msg, Client receiver)
        {
            foreach (Client rec in dictClientCallBacks.Keys)
            {
                if (rec.Name == receiver.Name)
                {
                    try
                    {
                        IChatCallback callback = dictClientCallBacks[rec];
                        callback.ReceiveWhisper(msg, rec);

                        foreach (Client sender in dictClientCallBacks.Keys)
                        {
                            if (sender.Name == msg.Sender)
                            {
                                IChatCallback senderCallback = dictClientCallBacks[sender];
                                senderCallback.ReceiveWhisper(msg, rec);
                                return;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        //AppLog.ErrorFormat("{0}(): Failed to whisper. sender: {1}, Receiver: {2}, Exception: {3}.",
                                           //new StackFrame().GetMethod().Name, msg.Sender, receiver.Name, ex);
                    }
                }
            }
        }
Пример #3
0
        public void Say(ChatMessage msg)
        {
            lock (syncObj)
            {
                Client senderClient = (from c in dictClientCallBacks.Keys
                                       where c.Name == msg.Sender
                                       select c).FirstOrDefault();

                foreach (KeyValuePair<Client, IChatCallback> keyValuePair in dictClientCallBacks)
                {
                    Client client = keyValuePair.Key;
                    if (client.Facility == senderClient.Facility)
                    {
                        IChatCallback callBack = keyValuePair.Value;

                        try
                        {
                            callBack.Receive(msg);
                        }
                        catch (Exception ex)
                        {
                            //AppLog.ErrorFormat("{0}(): Failed to callback. Sender: {1}, Receiver: {2}, content: {3}, Exception: {4}",
                                              // new StackFrame().GetMethod().Name, msg.Sender, client.Name, msg.Content, ex);
                        }
                    }
                }
            }
        }