Menu

[r399]: / phprpc_2.1 / dotNET / pag / pag.cs  Maximize  Restore  History

Download this file

149 lines (141 with data), 5.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* @author Ma Bingyao(andot@ujn.edu.cn)
* @copyright CoolCode.CN
* @package PHPRPC Agent Generator for .NET
* @version 2.1
* @last_update 2006-08-10
* @link http://www.coolcode.cn/?p=199
*/
using System;
using System.Text;
using System.IO;
using System.Net;
namespace pag
{
class Program
{
static void ShowVersion()
{
Console.WriteLine("PHPRPC Agent Generator for .NET 2.1.2006.0810");
Console.WriteLine("CopyRight (C) CoolCode.CN 2006");
Console.WriteLine();
}
static void ShowUsage()
{
Console.WriteLine(" Usage: pag <service url> <class name> [language]");
Console.WriteLine();
Console.WriteLine("Example: pag http://example.hostname.com/server.php MyClient c#");
}
static void ShowError(string error)
{
Console.WriteLine();
Console.WriteLine(error);
}
static Encoding GetEncoding(HttpWebResponse response)
{
string charset = response.GetResponseHeader("Content-Type");
if (charset.StartsWith("text/plain; charset="))
{
charset = charset.Substring(20, charset.Length - 20);
return Encoding.GetEncoding(charset);
}
return Encoding.UTF8;
}
static void genCSharp(string ClassName, string[] functions) {
StreamWriter sw = new StreamWriter(ClassName + ".cs");
sw.WriteLine("using System;");
sw.Write("public class ");
sw.Write(ClassName);
sw.WriteLine(": PHPRPC.Client");
sw.WriteLine("{");
for (int i = 0; i < functions.Length; i++)
{
sw.Write(" public object ");
sw.Write(functions[i]);
sw.WriteLine("(params object[] args)");
sw.WriteLine(" {");
sw.Write(" return this.Invoke(\"");
sw.Write(functions[i]);
sw.WriteLine("\", args);");
sw.WriteLine(" }");
sw.Write(" public object ");
sw.Write(functions[i]);
sw.WriteLine("(ref object[] args)");
sw.WriteLine(" {");
sw.Write(" return this.Invoke(\"");
sw.Write(functions[i]);
sw.WriteLine("\", ref args);");
sw.WriteLine(" }");
}
sw.WriteLine("}");
sw.Close();
}
static void Main(string[] args)
{
ShowVersion();
if (args.Length < 2)
{
ShowUsage();
return;
}
HttpWebRequest request;
HttpWebResponse response;
try
{
request = HttpWebRequest.Create(args[0]) as HttpWebRequest;
response = request.GetResponse() as HttpWebResponse;
}
catch
{
ShowUsage();
ShowError("Wrong URL!");
return;
}
Stream s = response.GetResponseStream();
Encoding encoding = GetEncoding(response);
StreamReader sr = new StreamReader(s, encoding);
string body = sr.ReadLine();
if (body.StartsWith("phprpc_functions"))
{
Console.Write(body.Substring(18, body.Length - 20));
string[] functions = (string[])PHPSerializer.UnSerialize(Convert.FromBase64String(body.Substring(18, body.Length - 20)), typeof(string[]), encoding);
if (args.Length == 2)
{
genCSharp(args[1], functions);
}
else
{
switch (args[2].ToLower())
{
case "cs":
case "c#":
case "csharp":
genCSharp(args[1], functions);
break;
/*
case "vb":
case "vb.net":
case "visualbasic":
genVisualBasic(args[1], functions);
break;
case "j#":
case "jsl":
case "jsharp":
genJSharp(args[1], functions);
break;
*/
default:
ShowError("The language " + args[2] + " isn't supported!");
break;
}
}
}
else
{
ShowUsage();
ShowError("The URL is not a PHPRPC Server!");
}
response.Close();
}
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.