Ok.. I've spent 6 hours straight on getting this to work and I've encountered *alot* of bizzare error messages so bear with me if I seem a bit wierd...
Basically I have a class called Proxy which inherits from WebProxy and ISerializable and implements a
deserialization constructor along with a the GetObjectData method for serialization. I've looked over some 5 tutorials concerning serialization and this is what I've been brewing together:
(It's 3 different files because it's a part of a project I'm working on)
File 1: proxyClass.cs
Code:
// created on 11/3/2006 at 8:39 AM
using System;
using System.Net;
using System.Runtime.Serialization;
namespace AnoDownload
{
[Serializable()]
class Proxy : WebProxy, ISerializable
{
private static int numberOfProxies; //Holds the number of proxy objects (and logicaly) the amount of proxies available to the program
public string address; //holds the url (and port) of the proxy
public string getAddress
{
get
{
return address;
}
}
//Variables associated with possible NetWorkCredential
public string username = null;
public string getUsername
{
get
{
return username;
}
}
public string password = null;
public string getPassword
{
get
{
return password;
}
}
public string domain = null;
public string getDomain
{
get
{
return domain;
}
}
//Variables associated with lockout (cooldown for rapidshare, megaupload, sexupload)
//Info: it doesn't really hold the number of minutes to be locked out it
// holds the time (and date) of the moment where the lockout should've expired
DateTime rapidshareCom = DateTime.MinValue; //MinValue = 1/1/0001 12:00:00 AM ((which won't be something our user has, unless his watch is seriously fucked up.)
DateTime rapidshareDe = DateTime.MinValue;
DateTime megauploadCom = DateTime.MinValue;
DateTime sexuploadCom = DateTime.MinValue;
// Constructors
public Proxy(string proxyUrl, bool bypass) : base(proxyUrl, bypass) //assume that no user credentials are needed
{ //default constructor
numberOfProxies++; //just to keep track of the number of proxies
address = proxyUrl;
//WebProxy proxy = new WebProxy(address, true);
}
public Proxy(string proxyUrl, bool bypass, string inUsername, string inPassword, string inDomain) : base(proxyUrl, bypass) //use overloaded webproxy constructor WebProxy(url, bypass) -- bypass = use direct connections for LAN
{
numberOfProxies++; //just to keep track of the number of proxies
address = proxyUrl;
username = inUsername;
password = inPassword;
domain = inDomain;
//WebProxy Proxy = new WebProxy(address, true);
//Proxy.Credentials = new NetworkCredential(username, password, domain);
this.Credentials = new NetworkCredential(username, password, domain);
}
///////SERIALIZATION IMPLEMENTATION
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//Deserialization constructor
public Proxy(SerializationInfo info, StreamingContext context) : base(info, context)
{
try
{
//Get the values from info and assign them to the appropriate properties
address = (string)info.GetValue("storedAddress", typeof(string));
username = (string)info.GetValue("storedUsername", typeof(string));
password = (string)info.GetValue("storedPassword", typeof(string));
domain = (string)info.GetValue("storedDomain", typeof(string));
}
catch (Exception e)
{
Console.WriteLine("Debug(deserialization constructor): Exception Caught!");
Console.WriteLine("ErrorMsg: " + e);
}
}
//Serialization method:
public new void GetObjectData(SerializationInfo info, StreamingContext context)
{
// Any other custom data which needs to be transferred
// info.AddValue("STORE-NAME", ORIG-VAR-NAME);
//Debug messages about values of variables before serialization should store them
Console.WriteLine("Debug: Serialization method called");
Console.WriteLine("Debug: Value of address: " + address);
Console.WriteLine("Debug: Value of username: " + username);
Console.WriteLine("Debug: Value of password: " + password);
Console.WriteLine("Debug: Value of domain: " + domain);
try
{
info.AddValue("storedAddress", address);
info.AddValue("storedUsername", username);
info.AddValue("storedPassword", password);
info.AddValue("storedDomain", domain);
}
catch (Exception e)
{
Console.WriteLine("Debug(GetObjectData): Exception Caught!");
Console.WriteLine("ErrorMsg: " + e);
}
base.GetObjectData( info, context ); //ctxt
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Class methods
public void setLockoutTimer(string serviceName, double timeOut)
{
//Sets the lockout timer for a service var by taking the current time and adding the number of timeout minutes
switch (serviceName)
{
case "rapidshare.com":
Console.WriteLine("Stub: rapidshare.com lockout timer activated");
rapidshareCom = DateTime.Now.AddMinutes(timeOut);
break;
case "rapidshare.de":
Console.WriteLine("Stub: rapidshare.de lockout timer activated");
rapidshareDe = DateTime.Now.AddMinutes(timeOut);
break;
case "megaupload.com":
Console.WriteLine("Stub: megaupload.com lockout timer activated");
megauploadCom = DateTime.Now.AddMinutes(timeOut);
break;
case "sexupload.com":
Console.WriteLine("Stub: sexupload.com lockout timer activated");
sexuploadCom = DateTime.Now.AddMinutes(timeOut);
break;
default:
Console.WriteLine("Error: serviceName was not an expected service (rapidshare.com/rapidshare.de\nmegaupload.com/sexupload.com");
break;
}
}
public bool isAvailable(string serviceName)
{
//Should return a bool indicating whether or not the proxy is available to use on a specific downloadsite
//what determines this is basically a timecheck against the lockout timer, if the current time less than
//or equal to 0 then the current time is past the lockout timers expiration point and the proxy is free
// The actual selection of a free proxy is left to the ProxyList class which will manage such things.
//bool return value: 1 = True (the proxy is available for this service), 0 = false
//should be less than zero if the proxy is available
double timerDifference = 999;
switch (serviceName)
{
case "rapidshare.com":
Console.WriteLine("Stub: rapidshare.com lockout timer activated");
timerDifference = (rapidshareCom - DateTime.Now).TotalMinutes;
break;
case "rapidshare.de":
Console.WriteLine("Stub: rapidshare.de lockout timer activated");
timerDifference = (rapidshareDe - DateTime.Now).TotalMinutes;
break;
case "megaupload.com":
Console.WriteLine("Stub: megaupload.com lockout timer activated");
timerDifference = (megauploadCom - DateTime.Now).TotalMinutes;
break;
case "sexupload.com":
Console.WriteLine("Stub: sexupload.com lockout timer activated");
timerDifference = (sexuploadCom - DateTime.Now).TotalMinutes;
break;
default:
Console.WriteLine("Error: serviceName was not an expected service (rapidshare.com/rapidshare.de\nmegaupload.com/sexupload.com");
break;
}
if (timerDifference <= 0)
{
return true;
}
else //proxy is not available for this service
{
return false;
}
}
}
}
File 2: proxyList.cs
Code:
// created on 11/8/2006 at 3:04 PM
using System;
using System.Net;
using System.Collections;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using AnoDownload;
namespace AnoDownload
{
class ProxyList
{
protected string fileName;
protected ArrayList proxyList = new ArrayList();
public ProxyList(string inFileName)
{
fileName = inFileName;
load(fileName);
}
/*
public ~ProxyList()
{
Console.WriteLine("destructor..");
//save(fileName);
} */
public void load(string inFileName)
{
/*
FileInfo file = new FileInfo("serializationData2.dat");
Stream fileStream = file.Open(FileMode.Open);
BinaryFormatter reader = new BinaryFormatter();
myDeSerializedArrayList = (ArrayList)reader.Deserialize(fileStream);
*/
FileInfo file = new FileInfo(inFileName);
Stream fileStream = null;
if ( file.Exists )
{
fileStream = file.Open(FileMode.Open);
BinaryFormatter reader = new BinaryFormatter();
proxyList = (ArrayList)reader.Deserialize(fileStream);
fileStream.Close();
}
else
{
Console.WriteLine("File does not exist, skipping load");
}
}
public void save(string inFileName)
{
/*
//Begin serialization process:
FileInfo file = new FileInfo("serializationData2.dat");
Stream fileStream = file.Open(FileMode.Create);
BinaryFormatter writer = new BinaryFormatter();
writer.Serialize(fileStream, myArrayList);
fileStream.Close();
*/
Console.WriteLine("Debug: ProxyList.save method running (Serialization should occur!)");
FileInfo file = new FileInfo(inFileName);
Stream fileStream = null;
if ( file.Exists )
{
fileStream = file.Open(FileMode.Open);
}
else
{
fileStream = file.Open(FileMode.Create);
}
BinaryFormatter writer = new BinaryFormatter();
writer.Serialize(fileStream, proxyList);
fileStream.Close();
}
public void debugPrintProxyList()
{
Console.WriteLine("Debug method: debugPrintProxyList() accessed\n");
for ( int i = 0; i < proxyList.Count; i++)
{
Console.WriteLine("Proxy number: " + i);
Console.WriteLine("Url: " + ((Proxy)proxyList[i]).getAddress );
Console.WriteLine("Username: " + ((Proxy)proxyList[i]).getUsername );
Console.WriteLine("Password: " + ((Proxy)proxyList[i]).getPassword );
Console.WriteLine("Domain: " + ((Proxy)proxyList[i]).getDomain );
Console.WriteLine("-----------------------------------------");
}
}
public void addProxy(string proxyUrl)
{
proxyList.Add( new Proxy(proxyUrl, true) );
}
}
}
File 3: testProxyList.cs
Code:
// created on 11/8/2006 at 4:26 PM
using System;
using AnoDownload;
class MainClass
{
public static void Main(string[] args)
{
ProxyList newlist = new ProxyList("tabt.dat");
//adding 3 proxies
newlist.addProxy("http://127.0.0.1:80");
newlist.addProxy("http://127.0.0.2:80");
newlist.addProxy("http://127.0.0.3:80");
//Print a list of proxies
newlist.debugPrintProxyList();
newlist.save("tabt.dat");
}
}
Now in this state, I tested it on a Windows machine with the MS csc compiler and it flat out worked
but if I try to compile with mono (using either mcs or gmcs) I must comment
Code:
base.GetObjectData( info, context );
out or else it will complain
and say that the WebProxy class doesn't contain a definition for 'GetObjectData' which is bullshit because WebProxy is supposed to inherit from the ISerializable class
MSDN Link confirming that WebProxy inherits from ISerializable:
MSDN Page on WebProxy
Please.. I'm close to just scrapping the whole deal and go back to gaming. I've looked through every tutorial I could and it should be good to go as far as I can see.. But it ain't..
If someone manages to help me out. Then thanks a lot man, you have no idea how helpful you'd be!