summaryrefslogtreecommitdiff
path: root/Ivy/IvyTCPStreamV3.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Ivy/IvyTCPStreamV3.cs')
-rw-r--r--Ivy/IvyTCPStreamV3.cs170
1 files changed, 91 insertions, 79 deletions
diff --git a/Ivy/IvyTCPStreamV3.cs b/Ivy/IvyTCPStreamV3.cs
index 594f9de..7f90a94 100644
--- a/Ivy/IvyTCPStreamV3.cs
+++ b/Ivy/IvyTCPStreamV3.cs
@@ -1,36 +1,45 @@
-using System;
-using System.Net;
-using System.Net.Sockets;
-using System.Text;
-using System.Collections;
-using System.Collections.Specialized;
-using System.IO;
namespace IvyBus
{
+ using System;
+ using System.Net;
+ using System.Net.Sockets;
+ using System.Text;
+ using System.Collections;
+ using System.Collections.Specialized;
+ using System.IO;
+ using IvyBus.Properties;
+
/// <summary>
/// Description résumée de IvyStream.
/// </summary>
internal class IvyTCPStreamV3 : NetworkStream, IvyProtocol
- {
- StreamReader input;
- StreamWriter output;
- IvyProtocol receiver;
-
+ {
/// the protocol separator
- internal const char ARG_START = '\x02';
- internal const char ARG_END = '\x03';
- internal const char MSG_END = '\n';
+ internal const char ArgStart = '\x02';
+ internal const char ArgEnd = '\x03';
+ internal const char MsgEnd = '\n';
+
+ private StreamReader input;
+ private StreamWriter output;
+ private IvyProtocol receiver;
- internal IvyTCPStreamV3(Socket socket, IvyProtocol _receiver) : base ( socket )
+ internal IvyTCPStreamV3(Socket socket, IvyProtocol receiver) : base(socket)
{
- output = new StreamWriter(this, Ivy.ivyEncoding);
- output.NewLine = MSG_END.ToString();
- input = new StreamReader(this, Ivy.ivyEncoding);
- receiver = _receiver;
+ this.output = new StreamWriter(this, Ivy.ivyEncoding);
+ this.output.NewLine = MsgEnd.ToString();
+ this.input = new StreamReader(this, Ivy.ivyEncoding);
+ this.receiver = receiver;
}
+ internal void Close()
+ {
+ this.input.Close();
+ this.output.Close();
+ base.Close();
+ }
+
/* the protocol magic numbers */
- internal enum MessageType : ushort
+ internal enum MessageType : int
{
Bye = 0, /* end of the peer */
AddRegexp = 1, /* the peer adds a regexp */
@@ -43,7 +52,7 @@ namespace IvyBus
Die = 8, /* the peer wants us to quit */
Ping = 9, /* checks the presence of the other */
Pong = 10, /* checks the presence of the other */
- };
+ }
/*
* message Syntax:
@@ -53,37 +62,37 @@ namespace IvyBus
MessageType, id , length, string
*/
- private void sendMsg(MessageType msgType, ushort msgId, string msgData)
+ private void SendMsg(MessageType msgType, int msgId, string msgData)
{
// IOException Should be traited upstairs
- output.Write((ushort)msgType);
- output.Write(' ');
- output.Write(msgId);
- output.Write(ARG_START);
- output.Write(msgData);
- output.Write(MSG_END);
- output.Flush();
-
+ lock (this.output)
+ {
+ this.output.Write((int)msgType);
+ this.output.Write(' ');
+ this.output.Write(msgId);
+ this.output.Write(ArgStart);
+ this.output.Write(msgData);
+ this.output.Write(MsgEnd);
+ this.output.Flush();
+ }
}
- void IvyProtocol.TokenStartRegexp(ushort port, string appName)
+
+ void IvyProtocol.TokenStartRegexp(int port, string appName)
{
- sendMsg(MessageType.StartRegexp, port, appName);
+ this.SendMsg(MessageType.StartRegexp, port, appName);
}
+
void IvyProtocol.TokenEndRegexp()
{
- sendMsg(MessageType.EndRegexp, 0, "");
- }
- void IvyProtocol.TokenApplicationId(ushort priority, string appId)
- {
- // NOt implemented in this protocol version
+ this.SendMsg(MessageType.EndRegexp, 0, string.Empty);
}
- void IvyProtocol.TokenAddBinding(BindingType type, ushort id, string expression)
+ void IvyProtocol.TokenAddBinding(BindingType type, int id, string expression)
{
switch (type)
{
- case BindingType.Regexp:
- sendMsg(MessageType.AddRegexp, id, expression); /* perhaps we should perform some checking here */
+ case BindingType.RegularExpression:
+ this.SendMsg(MessageType.AddRegexp, id, expression); /* perhaps we should perform some checking here */
break;
case BindingType.Simple:
// NO Simple Binding in this protocol
@@ -91,66 +100,69 @@ namespace IvyBus
}
}
- void IvyProtocol.TokenDelBinding(ushort id)
+ void IvyProtocol.TokenDelBinding(int id)
{
- sendMsg(MessageType.DelBinding, id, "");
+ this.SendMsg(MessageType.DelBinding, id, string.Empty);
}
- void IvyProtocol.TokenDirectMsg(ushort id, string message)
+ void IvyProtocol.TokenDirectMsg(int id, string message)
{
- sendMsg(MessageType.DirectMsg, id, message);
+ this.SendMsg(MessageType.DirectMsg, id, message);
}
+
void IvyProtocol.TokenPong(string s)
{
- sendMsg(MessageType.Pong, 0, s);
+ this.SendMsg(MessageType.Pong, 0, s);
}
+
void IvyProtocol.TokenPing(string s)
{
- sendMsg(MessageType.Ping, 0, s);
+ this.SendMsg(MessageType.Ping, 0, s);
}
- void IvyProtocol.TokenBye(ushort id, string message)
+ void IvyProtocol.TokenBye(int id, string message)
{
- sendMsg(MessageType.Bye, id, message);
+ this.SendMsg(MessageType.Bye, id, message);
}
- void IvyProtocol.TokenDie(ushort id, string message)
+ void IvyProtocol.TokenDie(int id, string message)
{
- sendMsg(MessageType.Die, id, message);
+ this.SendMsg(MessageType.Die, id, message);
}
- void IvyProtocol.TokenMsg(ushort key, string[] args)
+ void IvyProtocol.TokenMsg(int key, string[] args)
{
- string delimiter = "" + ARG_END;
+ string delimiter = string.Empty + ArgEnd;
string data = string.Join(delimiter, args);
// a bad protocol implementation in C add a delimiter to the end of each arg
// we must add a delimiter to the end
data += delimiter;
- sendMsg(MessageType.Msg, key, data);
+ this.SendMsg(MessageType.Msg, key, data);
}
- void IvyProtocol.TokenError(ushort key, string arg)
+ void IvyProtocol.TokenError(int key, string arg)
{
- sendMsg(MessageType.Msg, key, arg);
+ this.SendMsg(MessageType.Msg, key, arg);
}
- private ushort DeserializeShort()
+ private int DeserializeInt()
{
int read;
- ushort ret = 0;
+ int ret = 0;
char digit;
// this will eat next non digit char ie space
do
{
- read = input.Read();
+ read = this.input.Read();
if (read < 0)
throw new EndOfStreamException();
digit = (char)read;
if (Char.IsDigit(digit))
- ret = (ushort)(ret * 10 + (digit - 0x30));
+ ret = (int)(ret * 10 + (digit - 0x30));
} while (Char.IsDigit(digit));
return ret;
}
+
private string DeserializeString(char sep)
{
int read;
@@ -168,38 +180,38 @@ namespace IvyBus
return str.ToString();
}
- bool IvyProtocol.receiveMsg()
+ bool IvyProtocol.ReceiveMsg()
{
MessageType msgType = MessageType.Die;
- ushort msgId = 0;
+ int msgId = 0;
string msgData = null;
try
{
- msgType = (MessageType)DeserializeShort();
- msgId = DeserializeShort();
- msgData = DeserializeString(MSG_END);
+ msgType = (MessageType)DeserializeInt();
+ msgId = DeserializeInt();
+ msgData = DeserializeString(MsgEnd);
switch (msgType)
{
case MessageType.Die:
- receiver.TokenDie(msgId, msgData);
+ this.receiver.TokenDie(msgId, msgData);
break;
case MessageType.Bye:
- receiver.TokenBye(msgId, msgData);
+ this.receiver.TokenBye(msgId, msgData);
break;
- case MessageType.AddRegexp:
- receiver.TokenAddBinding(BindingType.Regexp, msgId, msgData);
+ case MessageType.AddRegexp:
+ this.receiver.TokenAddBinding(BindingType.RegularExpression, msgId, msgData);
break;
case MessageType.DelBinding:
- receiver.TokenDelBinding(msgId);
+ this.receiver.TokenDelBinding(msgId);
break;
case MessageType.EndRegexp:
- receiver.TokenEndRegexp();
+ this.receiver.TokenEndRegexp();
break;
case MessageType.Msg:
@@ -207,30 +219,30 @@ namespace IvyBus
// we must remove a delimiter to the end
if ( msgData.Length > 0 )
msgData = msgData.Remove(msgData.Length - 1,1);
- receiver.TokenMsg(msgId, msgData.Split( ARG_END ));
+ this.receiver.TokenMsg(msgId, msgData.Split( ArgEnd ));
break;
case MessageType.Pong:
- receiver.TokenPong(msgData);
+ this.receiver.TokenPong(msgData);
break;
case MessageType.Ping:
- receiver.TokenPing(msgData);
+ this.receiver.TokenPing(msgData);
break;
case MessageType.Error:
- receiver.TokenError(msgId, msgData);
+ this.receiver.TokenError(msgId, msgData);
break;
case MessageType.StartRegexp:
- receiver.TokenStartRegexp(msgId, msgData);
+ this.receiver.TokenStartRegexp(msgId, msgData);
break;
case MessageType.DirectMsg:
- receiver.TokenDirectMsg(msgId, msgData);
+ this.receiver.TokenDirectMsg(msgId, msgData);
break;
default:
- throw new IvyException("protocol error, unknown message type " + msgType);
+ throw new IvyException(Resources.UnknownMessage + msgType);
}
@@ -243,7 +255,7 @@ namespace IvyBus
}
catch (FormatException)
{
- throw new IvyException("protocol error on msgType");
+ throw new IvyException(Resources.ProtocolError);
}