summaryrefslogtreecommitdiff
path: root/CSharp/Ivy/IvyPPC/IvyUDPStreamV4.cs
blob: 8eebecd26a295bfbe85b2e65e5cf17cf2d95f557 (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
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace IvyBus
{
    class IvyUDPStream
    {
        Socket socket;
        BinaryReader input;
		BinaryWriter output;
        byte[] buffer;
        MemoryStream out_stream;
        MemoryStream in_stream;

        /// the protocol version number
        internal const int PROCOCOLVERSION = 4;
		public IvyUDPStream(Socket _socket) 
		{
            socket = _socket;
            buffer = new byte[4096];
            in_stream = new MemoryStream(buffer);
			input = new BinaryReader( in_stream,Encoding.ASCII);
            out_stream = new MemoryStream();
            output = new BinaryWriter(out_stream, Encoding.ASCII);
		}
        /*
         * message Syntax:
         * this is a binary formated message use of network representation
         * 
         * message Format:
                protocol_version, TCP server port , lenAppId, appId,  lenAppNameId, appName
         */

        internal void receiveMsg(out IPEndPoint remote, out int version, out int port, out string appId, out string appName)
        {
            int len;
            IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
            EndPoint tempRemoteEP = (EndPoint)remoteEP;
            remoteEP = null;
            len = socket.ReceiveFrom(buffer, ref tempRemoteEP);
            remote = (IPEndPoint)tempRemoteEP;
            in_stream.Position = 0;
            version = (ushort)IPAddress.NetworkToHostOrder((short)input.ReadUInt16());
            port = (ushort)IPAddress.NetworkToHostOrder((short)input.ReadUInt16());
            int lenAppId = (ushort)IPAddress.NetworkToHostOrder((short)input.ReadUInt16());
            appId = new String(input.ReadChars(lenAppId));
            int lenAppName = (ushort)IPAddress.NetworkToHostOrder((short)input.ReadUInt16());
            appName = new String(input.ReadChars(lenAppName));
        
        }

        internal void sendMsg(IPEndPoint EPhost, int port, string appId, string appName)
        {
            output.Write((ushort)IPAddress.HostToNetworkOrder((short)(PROCOCOLVERSION)));
            output.Write((ushort)IPAddress.HostToNetworkOrder((short)port));
            output.Write((ushort)IPAddress.HostToNetworkOrder((short)appId.Length));
            output.Write(appId.ToCharArray());
            output.Write((ushort)IPAddress.HostToNetworkOrder((short)appName.Length));
            output.Write(appName.ToCharArray());
            output.Flush();

            byte[] hellob = out_stream.GetBuffer();
            socket.SendTo(hellob, (int)out_stream.Length, 0, EPhost);
        }
        internal void Close()
        {
            socket.Close();
        }
    }
}