示例#1
0
        void Listen()
        {
            while (this.continueListening)
            {
                try
                {
                    Byte[] bytes   = new Byte[5];
                    int    n_bytes = 0;
                    try
                    {
                        n_bytes = StreamRead(bytes);
                    }
                    catch (IOException ex)
                    {
                        n_bytes = 0;
                    }

                    if (n_bytes >= 5) //minimum number of bytes for CMD + length to follow
                    {
                        string cmd = System.Text.Encoding.UTF8.GetString(bytes, 0, 3);
                        int    following_length = BitConverter.ToInt16(bytes, 3);

                        byte[] following_bytes = new byte[following_length];
                        if (following_length > 0)
                        {
                            StreamRead(following_bytes);
                        }
                        if (cmd != "PNG")
                        {
                            Console.WriteLine($" >> command recieved from the serveur : {cmd} de taille {following_length} {n_bytes}");
                        }

                        try
                        {
                            NomCommande cmd_type = (NomCommande)Enum.Parse(typeof(NomCommande), cmd);
                            if (cmd != "PNG")
                            {
                                LogWriter.Write($"command recieved: {cmd}, following_length: {following_length}");
                            }
                            MyClient.methods[cmd_type](following_bytes, this);
                        }
                        catch (Exception ex)
                        {
                            //write_in_log
                            LogWriter.Write($"CMD ERROR, CMD: {cmd}, following_length: {following_length}, EX:{ex}");
                            this.Stream.Flush();
                        }
                    }

                    Thread.Sleep(10);
                }
                catch (Exception ex) //à faire: prendre en compte la fermeture innatendue du canal par le serveur
                {
                    this.continueListening = false;
                    LogWriter.Write($"ERROR: Listen crashed:  {ex}");
                    Console.WriteLine(" >> " + ex.ToString());
                }
            }
        }
示例#2
0
        //Serialization

        /// <summary>
        /// Transform a <see cref="NomCommande"/> in a list of <see cref="byte"/>
        /// <para>Can be deserialized by the methods the server</para>
        /// </summary>
        /// <param name="nomCommande"></param>
        /// <returns></returns>
        private static byte[] serializationMessage(NomCommande nomCommande)
        {
            var cmd            = Encoding.UTF8.GetBytes(nomCommande.ToString());
            var message_length = BitConverter.GetBytes((Int16)0);

            byte[] msg = new byte[cmd.Length + message_length.Length];

            cmd.CopyTo(msg, 0);
            message_length.CopyTo(msg, cmd.Length);

            return(msg);
        }
示例#3
0
        static void Listen(NetworkStream stream)
        {
            bool continuer = true;

            while (continuer)
            {
                try
                {
                    Byte[] bytes        = new Byte[5];
                    int    NombreOctets = stream.Read(bytes, 0, bytes.Length);
                    if (NombreOctets >= 5) //minimum number of bytes for CMD + length to follow
                    {
                        string cmd = System.Text.Encoding.UTF8.GetString(bytes, 0, 3);
                        int    following_length = BitConverter.ToInt16(bytes, 3);

                        byte[] following_bytes = new byte[following_length];
                        stream.Read(following_bytes, 0, following_bytes.Length);

                        //Console.WriteLine($" >> command recieved from the serveur : {cmd} de taille {following_length} {NombreOctets}");

                        string      packet_string = System.Text.Encoding.UTF8.GetString(following_bytes, 0, following_bytes.Length);
                        NomCommande cmd_type      = (NomCommande)Enum.Parse(typeof(NomCommande), cmd);

                        if (cmd_type == NomCommande.MSG)
                        {
                            Messaging.RecieveMessage(following_bytes);
                        }
                        else if (cmd_type == NomCommande.OUS)
                        {
                            Messaging.RecieveOtherUsers(following_bytes, ref connected_users);
                        }
                    }
                }
                catch (Exception ex) //à faire: prendre en compte la fermeture innatendue du canal par le serveur
                {
                    continuer = false;
                    Console.WriteLine(" >> " + ex.ToString());
                }
            }
        }