Esempio n. 1
0
        private string GetUserStreamData(StreamReader netStream, User user)
        {
            // Using a list as we do not not the length of incoming data
            List <char> data = new List <char>(0);

            char[] buffer = new char[1];
            int    readBytes;

            try
            {
                readBytes = netStream.Read(buffer, 0, buffer.Length);

                // Read everything from start to end delimeter
                if (buffer[0] == '\u0002' && readBytes != -1)
                {
                    do
                    {
                        readBytes = netStream.Read(buffer, 0, buffer.Length);
                        data.Add(buffer[0]);
                    } while (!(buffer[0] == '\0'));
                }
                else
                {
                    return(new string(data.ToArray()));
                }

                // Remove terminating char from data
                data.RemoveAt(data.Count - 1);
            } catch (Exception ex) when(ex is IOException || ex is ObjectDisposedException)
            {
                OnUserNotConnectedCheck.Invoke(user);
            }

            return(new string(data.ToArray()));
        }
Esempio n. 2
0
        private void WriteToUserStream(User user, string text)
        {
            // Add delimiters
            StreamWriter writer;

            text = '\u0002' + text + '\0';

            var charsToSend = text.ToCharArray();

            try
            {
                writer = new StreamWriter(user.Connection.GetStream());
                writer.Write(charsToSend, 0, charsToSend.Length);
                writer.Flush();
            } catch (Exception e)
            {
                OnUserNotConnectedCheck.Invoke(user);
            }
        }