C sharp - BinaryReader AutoMapp

 
Vista:
sin imagen de perfil

BinaryReader AutoMapp

Publicado por JJBreaker (2 intervenciones) el 04/02/2017 17:11:11
Buenas tardes, tengo un servidor y un cliente, el servidor recibe mensajes del cliente auto asignados en el servidor así

public override ushort Id => 10100; este seria el primer paquete que recibe el servidor por parte del cliente, este mensaje esta escrito en el archivo HandshakeRequestMessage.cs de la carpeta mensajes y su contenido es:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
public class HandshakeRequestMessage : Message
    {
        /// <summary>
        ///     AppStore.
        /// </summary>
        public int AppStore;
 
        /// <summary>
        ///     Build.
        /// </summary>
        public int Build;
 
        /// <summary>
        ///     Device type.
        /// </summary>
        public int DeviceType;
 
        /// <summary>
        ///     Key version.
        /// </summary>
        public int KeyVersion;
 
        /// <summary>
        ///     Major version.
        /// </summary>
        public int MajorVersion;
 
        /// <summary>
        ///     Hash string.
        /// </summary>
        public string MasterHash;
 
        /// <summary>
        ///     Minor version.
        /// </summary>
        public int MinorVersion;
 
        /// <summary>
        ///     Protocol.
        /// </summary>
        public int Protocol;
 
        /// <summary>
        ///     Gets the ID of the <see cref="HandshakeRequestMessage" />.
        /// </summary>
        public override ushort Id => 10100;
 
        /// <summary>
        ///     Reads the <see cref="HandshakeRequestMessage" /> from the specified <see cref="MessageReader" />.
        /// </summary>
        /// <param name="reader">
        ///     <see cref="MessageReader" /> that will be used to read the <see cref="HandshakeRequestMessage" />.
        /// </param>
        /// <exception cref="ArgumentNullException"><paramref name="reader" /> is null.</exception>
        public override void ReadMessage(MessageReader reader)
        {
            ThrowIfReaderNull(reader);
 
            Protocol = reader.ReadInt32();
            KeyVersion = reader.ReadInt32();
            MajorVersion = reader.ReadInt32();
            Build = reader.ReadInt32();
            MinorVersion = reader.ReadInt32();
            MasterHash = reader.ReadString();
            DeviceType = reader.ReadInt32();
            AppStore = reader.ReadInt32();
        }
 
        /// <summary>
        ///     Writes the <see cref="HandshakeRequestMessage" /> to the specified <see cref="MessageWriter" />.
        /// </summary>
        /// <param name="writer">
        ///     <see cref="MessageWriter" /> that will be used to write the <see cref="HandshakeRequestMessage" />.
        /// </param>
        /// <exception cref="ArgumentNullException"><paramref name="writer" /> is null.</exception>
        public override void WriteMessage(MessageWriter writer)
        {
            ThrowIfWriterNull(writer);
 
            writer.Write(Protocol);
            writer.Write(KeyVersion);
            writer.Write(MajorVersion);
            writer.Write(Build);
            writer.Write(MinorVersion);
            writer.Write(MasterHash);
            writer.Write(DeviceType);
            writer.Write(AppStore);
        }
    }

Hasta aquí todo bien bien, ahora tengo que rellenar los demás mensajes identificando cada mensaje que envía el cliente, para ello e probado todo pero sin éxito, llevo dos meses con esto y no hay forma... estoy muy desesperado... lo que tengo que saber es por ejemplo en el siguiente archivo:

public override ushort Id => 10101; (LoginRequestMessage.cs)

tengo puesto:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/// <summary>
    /// Message that is sent by the client to the server to request
    /// for a login.
    /// </summary>
    public class LoginRequestMessage : Message
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="LoginRequestMessage"/> class.
        /// </summary>
        public LoginRequestMessage()
        {
            // Space
        }
        /// <summary>
        ///     Debug
        /// </summary>
        /// <param name="message"></param>
        [Conditional("DEBUG")]
        public static void DebugPrintTrace(Object message)
        {
            var stackTrace = new StackTrace(true);
            var sf = stackTrace.GetFrame(1);
            var str = sf.GetFileLineNumber() + " "
                      + Path.GetFileNameWithoutExtension(sf.GetFileName()) + ": "
                      + sf.GetMethod().Name + " ";
            Console.WriteLine(str + message.ToString());
        }
 
        //public long Unknown;
 
 
        /// <summary>
        ///  Gets the ID of the <see cref="LoginRequestMessage"/>.
        /// </summary>
        public override ushort Id => 10101;
 
        public static long baseStreamLength { get; private set; }
 
        /// <summary>
        /// Reads the <see cref="LoginRequestMessage"/> from the specified <see cref="MessageReader"/>.
        /// </summary>
        /// <param name="reader">
        /// <see cref="MessageReader"/> that will be used to read the <see cref="LoginRequestMessage"/>.
        /// </param>
        /// <exception cref="ArgumentNullException"><paramref name="reader"/> is null.</exception>
        public override void ReadMessage(MessageReader reader)
        {
            baseStreamLength = reader.BaseStream.Length;
            ThrowIfReaderNull(reader);
            long baseStreamPos = reader.BaseStream.Position;
            DebugPrintTrace("arrayLength -> " + baseStreamLength); //244
            DebugPrintTrace("baseStreamPos -> " + baseStreamPos);
 
            try
            {
                for (int i = 0; i < baseStreamLength; i++)
                {
                DebugPrintTrace(i + " Unknown -> " + reader.ReadInt64());
 
                DebugPrintTrace("baseStreamPos -> " + baseStreamPos);
                }
            }
                catch (Exception ex)
                {
                    DebugPrintTrace(ex.ToString());
                }
                DebugPrintTrace("BaseStream.Position final -> " + reader.BaseStream.Position); //241
 
        }

Donde

DebugPrintTrace(i + " Unknown -> " + reader.ReadInt64());

me salen 29 lecturas de tipo long, hay mas pero no lee mas puesto que esas lecturas algunas son int64, otras int32, bool, byte y string y para que las lea todas tengo que averiguar en cada reader que tipo es en el orden y tipo correcto, para despues darles el nombre a cada uno...

la pregunta., hay algún código que me lo haga automáticamente la conversión o me autodetecte el TypeCode de cada reader? probé a guardar todos los bytes recibidos en un nuevo stream y despues convertirlos uno por uno a los varios tipos descritos anteriormente pero no funciono, también probé con serialize y deserealize pero no entiendo muy bien como funciona esto aun... muchas gracias de antemano por vuestro tiempo y ayuda.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

BinaryReader AutoMapp

Publicado por JJBreaker (2 intervenciones) el 11/02/2017 08:10:02
Ya consegui la forma es lenta y tediosa pero funciona... igualmente si alguien encuentra alguna otra forma mas rapida sera bienvenida :)

1
2
3
4
5
for (long i = Reader.BaseStream.Position; i < Reader.BaseStream.Length; i++)
{
Console.WriteLine(this.Reader.ReadChar() + " pos:" + Reader.BaseStream.Position));
break;
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar