diff options
Diffstat (limited to 'ServerDialog.cs')
-rw-r--r-- | ServerDialog.cs | 185 |
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; + } + + } +} |