summaryrefslogtreecommitdiff
path: root/ServerDialog.cs
diff options
context:
space:
mode:
authorIan C <ianc@noddybox.co.uk>2005-01-21 01:03:55 +0000
committerIan C <ianc@noddybox.co.uk>2005-01-21 01:03:55 +0000
commita3f6697ffb872f553572627c73261cdd1ce55b67 (patch)
tree613f2a81953097c6a70e91b0ebbc09108156b91b /ServerDialog.cs
parentfe145c07172128b03f74d377761f33c2af4de960 (diff)
Initial checkin
Diffstat (limited to 'ServerDialog.cs')
-rw-r--r--ServerDialog.cs185
1 files changed, 185 insertions, 0 deletions
diff --git a/ServerDialog.cs b/ServerDialog.cs
new file mode 100644
index 0000000..3b03e7e
--- /dev/null
+++ b/ServerDialog.cs
@@ -0,0 +1,185 @@
+// Noddybox.Net - Provides simple server interaction classes
+// Copyright (C) 2003 Ian Cowburn
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// $Id$
+//
+
+namespace Noddybox.Net
+{
+ using System;
+ using System.Net;
+ using System.Net.Sockets;
+ using System.IO;
+ using System.Text;
+ using System.Text.RegularExpressions;
+
+ /// <summary>
+ /// Provides a wrapper around a stream to interact with servers
+ /// </summary>
+ /// <remarks>
+ /// created by - ianc
+ /// created on - 07/09/2003 22:54:39
+ /// </remarks>
+ public class ServerDialog
+ {
+ // -------------------------------------------------
+ // - PUBLIC MEMBERS -
+ // -------------------------------------------------
+
+ /// <summary>
+ /// Used for returns from the server
+ /// </summary>
+ public class Reply
+ {
+ /// <summary>
+ /// Default constructor - initializes all fields to default values
+ /// </summary>
+ public Reply()
+ {
+ m_code=-1;
+ m_text="";
+ }
+
+ /// <summary>
+ /// The code returned by the server
+ /// </summary>
+ public int Code
+ {
+ get {return m_code;}
+ set {m_code=value;}
+ }
+
+ /// <summary>
+ /// The text returned by the server
+ /// </summary>
+ public string Text
+ {
+ get {return m_text;}
+ set {m_text=value;}
+ }
+
+ private int m_code;
+ private string m_text;
+
+ }
+
+ /// <summary>
+ /// Default constructor - initializes all fields to default values
+ /// </summary>
+ public ServerDialog()
+ {
+ m_encode=new System.Text.ASCIIEncoding();
+ m_code=new Regex("^[0-9][0-9][0-9]");
+ m_code_end=new Regex("^[0-9][0-9][0-9][^-]");
+
+ }
+
+ /// <summary>
+ /// Sends a command to the server and returns the reply
+ /// </summary>
+ public Reply Send(Stream str, string command)
+ {
+ Reply r=new Reply();
+
+ if (command!=null)
+ {
+ byte[] b=ToBytes(command);
+
+ str.Write(b,0,b.Length);
+ }
+
+ GetResult(str,r);
+
+ return r;
+ }
+
+ // -------------------------------------------------
+ // - PRIVATE MEMBERS -
+ // -------------------------------------------------
+
+ private ASCIIEncoding m_encode;
+ private Regex m_code;
+ private Regex m_code_end;
+
+
+
+ private byte[] ToBytes(string s)
+ {
+ return m_encode.GetBytes(s);
+ }
+
+ private string GetLine(Stream str)
+ {
+ byte[] b=new byte[1];
+ int c;
+ string r="";
+
+ while((c=str.ReadByte())!=-1)
+ {
+ b[0]=(byte)c;
+ r+=m_encode.GetString(b);
+
+ if (c==10)
+ {
+ break;
+ }
+ }
+
+ return r;
+ }
+
+ private void GetResult(Stream str, Reply r)
+ {
+ string s;
+ string all="";
+ bool first_line=true;
+
+ do
+ {
+ s=GetLine(str);
+
+ if (first_line)
+ {
+ if (m_code.IsMatch(s))
+ {
+ r.Code=Convert.ToInt32(s.Substring(0,3));
+ }
+ else
+ {
+ throw new NetException(NetException.Type.ALL_NoReplyCode);
+ }
+
+ first_line=false;
+ }
+
+ if (m_code.IsMatch(s))
+ {
+ all+=s.Substring(4);
+ }
+ else
+ {
+ all+=s;
+ }
+
+ } while (!m_code_end.IsMatch(s));
+
+ all=all.TrimEnd(null);
+ r.Text=all;
+ }
+
+ }
+}