summaryrefslogtreecommitdiff
path: root/IvyPPC/IvyUDPStreamV3.cs
blob: 63a86797a0220d332cbd52bf6b175defe814ef84 (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace IvyBus
{
    class IvyUDPStreamV3 : IvyUDPStream
    {
        StreamReader input;
        StreamWriter output;

        /// the protocol version number
        internal const int PROCOCOLVERSION = 3;

        public IvyUDPStreamV3(Socket _socket) : base( _socket , PROCOCOLVERSION )
        {
            input = new StreamReader(in_stream, Encoding.ASCII);
            output = new StreamWriter(out_stream, Encoding.ASCII);
        }
        /*
         * message Syntax:
         * this is a text formated message
         * 
         * message Format:
                protocol_version, TCP server port , appId,  appName
         */
        private ushort DeserializeShort()
        {
            int read;
            ushort ret = 0;
            char digit;
            // this will eat next non digit car ie space
            do
            {
                read = input.Read();
                if (read < 0)
                    throw new EndOfStreamException();
                digit = (char)read;
                if ( Char.IsDigit(digit) )
                    ret = (ushort)(ret * 10 + (digit-0x30));
            } while (Char.IsDigit(digit));
            return ret;
        }
        private string DeserializeString(char sep)
        {
            int read;
            char car;
            StringBuilder str = new StringBuilder();
            // this will eat next non digit car ie space
            do
            {
                read = input.Read();
                if (read < 0)
                    throw new EndOfStreamException();
                if (read == 0) break;
                car = (char)read;
                if (car != sep)
                    str.Append(car);
            } while (car != sep);
            return str.ToString();
        }

        internal override void Deserialize(out ushort version, out ushort port, out string appId, out string appName)
        {
            version = 0;
            port = 0;
            appId = "";
            appName = "";
            try {
            version = DeserializeShort();
            port = DeserializeShort();
            //Optionel in V3 protocol depend on client version
            appId = DeserializeString(' ');
            appName = DeserializeString('\n');
            }
            catch( EndOfStreamException )
            {
                // Bad protocol message receive or without appId and appName
            }
            input.DiscardBufferedData();
        }
        private void Serialize(ushort arg, char sep)
        {
            output.Write(arg);
            output.Write(sep);
        }
        private void Serialize(string arg, char sep)
        {
            output.Write(arg);
            output.Write(sep);
        }
        internal override void Serialize(ushort port, string appId, string appName)
        {
            Serialize(PROCOCOLVERSION, ' ');
            Serialize(port,' ');
            Serialize(appId,' '); //No AppId in V3
            Serialize(appName, '\n'); //No Appname in V3
            output.Flush();
        }
    }
}