summaryrefslogtreecommitdiff
path: root/Ivy/IvyUDPStream.cs
diff options
context:
space:
mode:
authorfcolin2007-02-01 12:04:16 +0000
committerfcolin2007-02-01 12:04:16 +0000
commit92757a8d629812303ff3665343bd098917cca611 (patch)
treecd995c9863aa6fc4c32ec5ce247e4c3119eb44a3 /Ivy/IvyUDPStream.cs
parent98ab5d0164040427f7c554febae125686284e2a7 (diff)
downloadivy-csharp-92757a8d629812303ff3665343bd098917cca611.zip
ivy-csharp-92757a8d629812303ff3665343bd098917cca611.tar.gz
ivy-csharp-92757a8d629812303ff3665343bd098917cca611.tar.bz2
ivy-csharp-92757a8d629812303ff3665343bd098917cca611.tar.xz
modification structure svn
Diffstat (limited to 'Ivy/IvyUDPStream.cs')
-rw-r--r--Ivy/IvyUDPStream.cs66
1 files changed, 66 insertions, 0 deletions
diff --git a/Ivy/IvyUDPStream.cs b/Ivy/IvyUDPStream.cs
new file mode 100644
index 0000000..0e3f517
--- /dev/null
+++ b/Ivy/IvyUDPStream.cs
@@ -0,0 +1,66 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Net;
+using System.Net.Sockets;
+using System.IO;
+
+namespace IvyBus
+{
+ abstract class IvyUDPStream
+ {
+ Socket socket;
+ byte[] buffer;
+
+ protected MemoryStream out_stream;
+ protected MemoryStream in_stream;
+
+ ushort protocol_version;
+
+ public ushort ProtocolVersion
+ {
+ get { return protocol_version; }
+ }
+
+ public IvyUDPStream(Socket _socket, ushort protocol)
+ {
+ socket = _socket;
+ buffer = new byte[4096];
+ in_stream = new MemoryStream(buffer);
+ out_stream = new MemoryStream();
+ protocol_version = protocol;
+ }
+ internal void Close()
+ {
+ in_stream.Close();
+ out_stream.Close();
+ socket.Shutdown(SocketShutdown.Both);
+ socket.Close();
+ }
+ internal void receiveMsg(out IPEndPoint remote, out ushort version, out ushort 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;
+ in_stream.SetLength(len);
+ in_stream.Seek(0, SeekOrigin.Begin);
+ //Call Deserialization
+ Deserialize( out version, out port, out appId, out appName );
+ }
+ internal void sendMsg(IPEndPoint EPhost, ushort port, string appId, string appName)
+ {
+ // Call Serialisation
+ Serialize(port, appId, appName);
+
+ byte[] hellob = out_stream.GetBuffer();
+ socket.SendTo(hellob, (int)out_stream.Length, 0, EPhost);
+ }
+ abstract internal void Serialize(ushort port, string appId, string appName);
+ abstract internal void Deserialize(out ushort version, out ushort port, out string appId, out string appName);
+
+ }
+}