25 |
namespace DotGNU.XmlRpc |
namespace DotGNU.XmlRpc |
26 |
{ |
{ |
27 |
using System; |
using System; |
28 |
using System.Reflection; |
using System.IO; |
29 |
|
using System.Text; |
30 |
using System.Net; |
using System.Net; |
31 |
using System.Net.Sockets; |
using System.Net.Sockets; |
32 |
using System.Web.Services.Protocols; |
using System.Web.Services.Protocols; |
33 |
using System.Text.RegularExpressions; |
using System.Text.RegularExpressions; |
34 |
|
using DotGNU.XmlRpc.Serialization.Formatters; |
35 |
|
|
36 |
public class XmlRpcClientProtocol : HttpSynchronousProtocol |
public class XmlRpcClientProtocol |
37 |
{ |
{ |
|
private XmlRpcSerializer ser; |
|
|
private XmlRpcDeserializer dser; |
|
38 |
private String header; |
private String header; |
39 |
|
private Stream ns; |
40 |
|
private String dest; |
41 |
|
public String Url; |
42 |
|
bool connected = false; |
43 |
|
|
44 |
public XmlRpcClientProtocol(IPHostEntry host) : base (host) |
public XmlRpcClientProtocol() |
45 |
{ |
{ |
46 |
} |
} |
47 |
|
|
48 |
public XmlRpcClientProtocol(MemberInfo type) : base(new IPHostEntry()) |
private void connect() |
49 |
{ |
{ |
50 |
String url = String.Empty; |
String url = String.Empty; |
51 |
String port = String.Empty; |
String port = String.Empty; |
52 |
String dest = String.Empty; |
|
53 |
Object[] attributes = type.GetCustomAttributes(typeof(XmlRpcUrlAttribute), false); |
dest = String.Empty; |
54 |
XmlRpcUrlAttribute xmlrpcUrl = (XmlRpcUrlAttribute)attributes[0]; |
|
55 |
String tmpUrl = xmlrpcUrl.Uri; |
String tmpUrl = Url; |
56 |
int spot = tmpUrl.LastIndexOf("://")+3; |
int spot = tmpUrl.LastIndexOf("://")+3; |
57 |
tmpUrl = tmpUrl.Substring(spot, tmpUrl.Length - spot); |
tmpUrl = tmpUrl.Substring(spot, tmpUrl.Length - spot); |
58 |
spot = tmpUrl.IndexOf("/"); |
spot = tmpUrl.IndexOf("/"); |
70 |
port = "80"; |
port = "80"; |
71 |
url = tmpUrl; |
url = tmpUrl; |
72 |
} |
} |
73 |
header = "POST " + dest + " HTTP/1.0\nContent-Type: text/xml\nContent-Length: "; |
IPHostEntry entry = Dns.Resolve(url); |
74 |
info = Dns.Resolve(url); |
int porti = Convert.ToInt32(port); |
75 |
} |
IPEndPoint ep = new IPEndPoint(entry.AddressList[0], porti ); |
76 |
|
|
77 |
|
// Connect to the remote server. |
78 |
|
Socket socket = new Socket(AddressFamily.InterNetwork, |
79 |
|
SocketType.Stream, |
80 |
|
ProtocolType.Unspecified); |
81 |
|
socket.Connect(ep); |
82 |
|
|
83 |
public XmlRpcResponse Invoke(XmlRpcRequest request) |
ns = new NetworkStream( socket ); |
|
{ |
|
|
|
|
|
ser = new XmlRpcSerializer(); |
|
|
String requestXml = ser.SerializeRequest( request ).ToString(); |
|
|
int state = Send( requestXml ); |
|
|
if(state != -1) |
|
|
{ |
|
|
XmlRpcResponse response = new XmlRpcResponse(receivedASCII); |
|
|
return response; |
|
|
} |
|
|
else |
|
|
{ |
|
|
return null; |
|
|
} |
|
|
} |
|
|
|
|
|
// TODO: check for correct xml |
|
|
internal bool CheckXml(String xml) |
|
|
{ |
|
|
return true; |
|
|
} |
|
84 |
|
|
85 |
public int Send(String data) |
connected = true; |
|
{ |
|
|
if(data == null) |
|
|
{ |
|
|
// throw exception |
|
|
} |
|
|
|
|
|
if(!CheckXml(data)) |
|
|
{ |
|
|
// throw bad xml exception |
|
|
} |
|
|
|
|
|
IPAddress[] ipaddress = info.AddressList; |
|
|
EndPoint ep = new IPEndPoint(ipaddress[0], 80); |
|
|
sock.Connect(ep); |
|
|
data = header + data.Length + "\n\n" + data; |
|
|
if(sock.Connected) |
|
|
{ |
|
|
sock.Send(ASCII.GetBytes(data), ASCII.GetBytes(data).Length, 0); |
|
|
} |
|
|
else |
|
|
{ |
|
|
return -1; |
|
|
} |
|
|
|
|
|
Int32 bytes = sock.Receive(receivedData, receivedData.Length, 0); |
|
|
receivedASCII = GetStream(bytes); |
|
|
sock.Close(); |
|
|
return 0; |
|
86 |
} |
} |
87 |
|
|
88 |
internal String GetStream(Int32 bytes) |
public Object[] Invoke(String method_name, Object[] args ) |
89 |
{ |
{ |
90 |
String str = String.Empty; |
if( !connected ) connect(); |
|
|
|
|
if(bytes == 0) |
|
|
{ |
|
|
// throw exception |
|
|
} |
|
91 |
|
|
92 |
while(bytes > 0) |
MemoryStream ms = new MemoryStream(); |
93 |
{ |
MethodCallFormatter call = new MethodCallFormatter(); |
94 |
bytes = sock.Receive(receivedData, receivedData.Length, 0); |
XmlRpcMethod method = new XmlRpcMethod( method_name ); |
95 |
str = str + ASCII.GetString(receivedData, 0, bytes); |
|
96 |
|
foreach( object obj in args) { |
97 |
|
Console.WriteLine( "Add {0} = {1}", obj.GetType(), obj ); |
98 |
|
method.Add( obj ); |
99 |
|
} |
100 |
|
|
101 |
|
call.Serialize( ms, method ); |
102 |
|
ms.Seek(0,0); |
103 |
|
|
104 |
|
// Got stream to send (ms) |
105 |
|
|
106 |
|
String header = "POST " + dest + " HTTP/1.0\nContent-Type: text/xml\nContent-Length: " + ms.Length.ToString() + "\r\n\r\n"; |
107 |
|
|
108 |
|
StreamWriter hw = new StreamWriter( ns ); |
109 |
|
|
110 |
|
hw.Write( header ); |
111 |
|
hw.Flush(); |
112 |
|
|
113 |
|
int len = 1024; |
114 |
|
int bread; |
115 |
|
byte[] buffer = new byte[len]; |
116 |
|
while( (bread = ms.Read(buffer, 0, len)) > 0) { |
117 |
|
//String s = Encoding.ASCII.GetString(buffer, 0, bread ); |
118 |
|
//Console.Write( "{0}", s ); |
119 |
|
ns.Write(buffer, 0, bread); |
120 |
} |
} |
121 |
|
|
122 |
return str; |
//while( (bread = ns.Read(buffer, 0, len)) > 0) { |
123 |
} |
//String s = Encoding.ASCII.GetString(buffer, 0, bread ); |
124 |
|
//Console.Write( "{0}", s ); |
125 |
} |
// } |
126 |
|
|
127 |
|
// Get Response |
128 |
|
MethodResponseFormatter response = new MethodResponseFormatter(); |
129 |
|
XmlRpcResponse r = (XmlRpcResponse)response.Deserialize( ns ); |
130 |
|
|
131 |
|
Console.WriteLine( "Hmm: {0}", r.ToString() ); |
132 |
|
return r.ToArray(); |
133 |
|
} |
134 |
|
} |
135 |
} |
} |