Ejemplo n.º 1
0
        public override void Send(MessageType msgType, NetworkStream stream, object options)
        {
            BinaryWriter writer = new BinaryWriter(stream);

            switch (msgType)
            {
            case MessageType.ConnectionAsk:
            {
                byte[] userTable = SimpleFormatters.ObjectToBytes(options);
                int    len       = userTable.Length;
                writer.Write(len);
                writer.Write(userTable);
                break;
            }

            case MessageType.ConnectionUserAdded:
            {
                byte[] userBytes = SimpleFormatters.ObjectToBytes(options);
                int    len       = userBytes.Length;
                writer.Write(len);
                writer.Write(userBytes);
                break;
            }

            case MessageType.ConnectionAccepted:
            case MessageType.LoggedIn:
            case MessageType.LoggedOut:
            case MessageType.IsAlive:
            {
                StreamWriter nickWriter = new StreamWriter(stream);
                nickWriter.WriteLine((string)options);
                nickWriter.Close();
                break;
            }

            default:
                writer.Close();
                break;
            }
        }
Ejemplo n.º 2
0
        public override void Send(MessageType msgType, NetworkStream stream, object options)
        {
            BinaryWriter writer = new BinaryWriter(stream);

            switch (msgType)
            {
            case MessageType.FilesTableAvailable:
            {
                AvailableFiles.Clear();
                AvailableFiles.AddRange((string[])options);

                List <string> temp  = new List <string>();
                string[]      files = (string[])options;
                foreach (string file in files)
                {
                    FileInfo f    = new FileInfo(file);
                    long     size = f.Length;
                    temp.Add(String.Concat(file, " ", size.ToString()));
                }

                byte[] buffer = SimpleFormatters.ObjectToBytes(temp);
                byte[] count  = BitConverter.GetBytes(buffer.Length);
                writer.Write(count, 0, 4);
                writer.Write(buffer, 0, buffer.Length);
                break;
            }

            case MessageType.FilesAsk:
            {
                byte[] buffer = SimpleFormatters.ObjectToBytes(options);
                byte[] count  = BitConverter.GetBytes(buffer.Length);
                writer.Write(count, 0, 4);
                writer.Write(buffer, 0, buffer.Length);
                break;
            }

            case MessageType.FileDataFirst:
            {
                char[] symbols = ((string)options).ToCharArray();
                byte[] buffer  = System.Text.Encoding.Unicode.GetBytes(symbols);
                byte[] count   = BitConverter.GetBytes(buffer.Length);
                writer.Write(count, 0, 4);
                writer.Write(buffer, 0, buffer.Length);
                break;
            }

            case MessageType.FileData:
            {
                byte[] buffer = (byte[])options;
                byte[] count  = BitConverter.GetBytes(buffer.Length);
                writer.Write(count, 0, 4);
                writer.Write(buffer, 0, buffer.Length);

                break;
            }

            case MessageType.FileDataLast:
            {
                break;
            }

            default:
            {
                break;
            }
            }
        }