Skip to content
Snippets Groups Projects
Commit 64b58a98 authored by ZIMMERLÉ Nathan's avatar ZIMMERLÉ Nathan
Browse files

Classe ChatMessage avec une taille d'en tête définie

parent e43b5713
No related merge requests found
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClientUdp
{
public enum Commande
{
POST, GET, HELP, QUIT, STOPSERVEUR, SUBSCRIBE, SUBSCRIBEv2, UNSUBSCRIBE
};
public enum CommandeType { REQUETE, REPONSE };
class ChatMessage
{
public const int bufferSize = 1500;
public const int headerSize = 35;
public Commande commande; // commande
public CommandeType commandeType; // type (Requête/Réponse)
public int dataSize; // taille de la donnée
public String data; // données de la commande
public String pseudo; // pseudo de l'envoyeur
public ChatMessage(Commande commande, CommandeType type, String data, String pseudo)
{
this.commande = commande;
this.commandeType = type;
this.dataSize = data.Length;
this.data = data;
this.pseudo = pseudo;
}
public ChatMessage(byte[] buffer)
{
commande = (Commande)buffer[0];
commandeType = (CommandeType)buffer[1];
pseudo = Encoding.ASCII.GetString(buffer, 2, 30).TrimEnd(new char[] { '\0' });
dataSize = BitConverter.ToInt32(buffer, 32);
data = Encoding.ASCII.GetString(buffer, 36, dataSize);
}
public byte[] GetBytes()
{
byte[] buffer = new byte[bufferSize]; // Déclaration du buffer
buffer[0] = (byte)commande; // Commande
buffer[1] = (byte)commandeType; // Type de la commande
Encoding.ASCII.GetBytes(pseudo, 0, pseudo.Length, buffer, 2); // Pseudo à 30 bits
byte[] intBuf = BitConverter.GetBytes(dataSize); // Taille de la data
buffer[32] = intBuf[0]; // Int stocké sur 4bits
buffer[33] = intBuf[1];
buffer[34] = intBuf[2];
buffer[35] = intBuf[3];
Encoding.ASCII.GetBytes(data, 0, data.Length, buffer, 36); // Data
return buffer;
}
public static byte[] GetBytes(Commande commande, CommandeType type, String data, String pseudo)
{
ChatMessage chatCommande = new ChatMessage(commande, type, data, pseudo);
return chatCommande.GetBytes();
}
public override string ToString()
{
return "[" + commande + "," + commandeType + ",\"" + pseudo + "\"," + dataSize + ",\"" + data + "\"]";
}
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment