private void OnMessageReceived(BroadcastMessage message)
        {
            lock (@lock) // Prevent that two thread don't call this function at the same time
            {
                Console.WriteLine("Received: " + message);

                SendToEveryone();
                SaveMessage();
            }

            void SendToEveryone()
            {
                // Send to all clients
                foreach (var connectedClient in connectedClients)
                {
                    SendToAccessor(connectedClient);
                }

                void SendToAccessor(PcsClient client)
                {
                    if (message == null)
                    {
                        throw new ArgumentNullException(nameof(message));
                    }

                    client.SendPacket(new BroadcastMessagePacket(message));
                }
            }

            void SaveMessage()
            {
                ftpClient.SaveMessage(message);
            }
        }
        public IEnumerable <BroadcastMessage> GetDailyMessages(string channelName, DateTime day)
        {
            string remotePath = GetMessagePath(channelName, day);

            CreateMissingDirectories(remotePath, true);

            if (!ftpClient.FileExists(remotePath))
            {
                yield break;
            }

            ftpClient.Download(out byte[] buffer, remotePath);

            string fileContent = PcsServer.Encoding.GetString(buffer, 0, buffer.Length);

            var fileMessages = GetTextMessages(fileContent);

            foreach (string fileMessage in fileMessages)
            {
                yield return(BroadcastMessage.FromFileMessage(fileMessage));
            }

            IEnumerable <string> GetTextMessages(string fullText)
            {
                string currentTextMsg = string.Empty;
                bool   readingTextMsg = false;

                foreach (char c in fullText)
                {
                    if (c == ControlChars.StartOfText)
                    {
                        readingTextMsg = true;
                    }
                    else if (c == ControlChars.EndOfText)
                    {
                        readingTextMsg = false;
                        yield return(currentTextMsg);

                        currentTextMsg = string.Empty;
                    }

                    if (readingTextMsg)
                    {
                        currentTextMsg += c;
                    }
                }
            }
        }
Esempio n. 3
0
        public ClientConnectionHandler(PcsClient client,
                                       Action <Member> signIn,
                                       Action <BroadcastMessage> addMessage,
                                       Action <PcsClient, Member> disconnect)
        {
            bool connected = true;

            while (connected)
            {
                try
                {
                    Packet packet = client.ReceivePacket();

                    switch (packet)
                    {
                    case SignInPacket signInPacket:
                    {
                        m_signedInMember = signInPacket.Member;
                        signIn(m_signedInMember);
                        signedIn = true;
                    }
                    break;

                    case MessagePacket messagePacket when signedIn:
                    {
                        var message = new BroadcastMessage(messagePacket.Message, DateTime.Now, m_signedInMember);
                        addMessage(message);
                    }
                    break;

                    case DisconnectPacket _ when signedIn:
                    {
                        connected = false;
                    }
                    break;
                    }
                }
                catch (SocketException) // When An existing connection was forcibly closed by the remote host
                {
                    connected = false;
                }
            }

            client.Disconnect();
            disconnect(client, m_signedInMember);
        }
        public void SaveMessage(BroadcastMessage broadcastMsg)
        {
            if (broadcastMsg == null)
            {
                throw new ArgumentNullException(nameof(broadcastMsg));
            }

            string remotePath = GetMessagePath(broadcastMsg.BaseMessage.ChannelName, broadcastMsg.DateTime);

            CreateMissingDirectories(remotePath, true);

            using (var appendStream = ftpClient.OpenAppend(remotePath))
                using (var writer = new StreamWriter(appendStream, PcsServer.Encoding))
                {
                    string fileMessage = ControlChars.StartOfText + broadcastMsg.ToFileMessage() + ControlChars.EndOfText; // TODO Do something for these variables
                    writer.WriteLine(fileMessage);
                }
        }
 public BroadcastMessagePacket(BroadcastMessage message) : base(Flags.BroadcastMessage)
 {
     BroadcastMessage = message;
 }