summaryrefslogtreecommitdiff
path: root/Noddybox.WindowsPhone.FTP/ServerDialog.cs
diff options
context:
space:
mode:
authorIan C <ianc@noddybox.co.uk>2012-10-01 20:59:12 +0000
committerIan C <ianc@noddybox.co.uk>2012-10-01 20:59:12 +0000
commite078a2887cf2dc61da49ce302324ff1485f95af9 (patch)
tree800c783a47576f7950497e5c8b7194ea439ae57a /Noddybox.WindowsPhone.FTP/ServerDialog.cs
parentb228a31e12d8f30ed1f4bf03136cad3c41b1de45 (diff)
Added non-working code for an attempt at FTPHEADmaster
Diffstat (limited to 'Noddybox.WindowsPhone.FTP/ServerDialog.cs')
-rw-r--r--Noddybox.WindowsPhone.FTP/ServerDialog.cs178
1 files changed, 178 insertions, 0 deletions
diff --git a/Noddybox.WindowsPhone.FTP/ServerDialog.cs b/Noddybox.WindowsPhone.FTP/ServerDialog.cs
new file mode 100644
index 0000000..4291afb
--- /dev/null
+++ b/Noddybox.WindowsPhone.FTP/ServerDialog.cs
@@ -0,0 +1,178 @@
+// This file is part of the Noddybox.WindowsPhone C# suite.
+//
+// Noddybox.Emulation 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 3 of the License, or
+// (at your option) any later version.
+//
+// Noddybox.Emulation 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 Noddybox.Emulation. If not, see <http://www.gnu.org/licenses/>.
+//
+// Copyright (c) 2012 Ian Cowburn
+//
+using System;
+using System.Net;
+using System.Net.Sockets;
+using System.IO;
+using System.Text;
+using System.Text.RegularExpressions;
+
+namespace Noddybox.WindowsPhone.Net
+{
+ /// <summary>
+ /// Provides a wrapper around a stream to interact with servers
+ /// </summary>
+ 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;
+ }
+
+ }
+}