天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

模擬IIS向Silverlight輸出策略文件

  問(wèn)題

  最近的Silverlight開(kāi)發(fā)中,由于部分需求對(duì)實(shí)時(shí)性和數(shù)據(jù)量下載速度有要求,部分WCF服務(wù)配置成了NETTcpBinding,這種方式跟普通的service.svc寄宿IIS不同的是,Silverlight需要的策略文件需要放置在本機(jī)IIS的根下,也就是wwwroot文件夾下,以滿足Silverlight在以TCP協(xié)議調(diào)用本機(jī)WCF服務(wù)時(shí)請(qǐng)求策略文件。(注:Silverlight通過(guò)TCP協(xié)議調(diào)用WCF服務(wù)時(shí),會(huì)以http方式請(qǐng)求主機(jī)的一個(gè)策略文件,地址是http://localhost/clientaccesspolicy.xml)

  這其實(shí)是個(gè)不太好的選擇,程序運(yùn)行的所需的環(huán)境被分成了兩部分,同事的機(jī)器上并未安裝IIS,為了大家開(kāi)發(fā)簡(jiǎn)便,不用在額外安裝IIS,也為了讓程序更加獨(dú)立,我就想能不能寫(xiě)代碼監(jiān)控80端口模擬IIS向Silverlight輸出這個(gè)策略文件。

  解決方法

  有了這個(gè)想法之后,首先想到的是通過(guò)Socket進(jìn)行監(jiān)聽(tīng),因?yàn)榇饲霸贛SDN上看到過(guò)這種方式,但很無(wú)奈,將代碼轉(zhuǎn)移過(guò)來(lái)之后,并未成功。相信做過(guò)Silverlight在Socket方面應(yīng)用的朋友對(duì)下面這個(gè)PolicyServer類很熟悉吧。

using System;
using System.IO;
using System.NET;
using System.NET.Sockets;

namespace PolicyServer
{
// Encapsulate and manage state for a single connection from a client
class PolicyConnection
{
private Socket m_connection;

// buffer to receive the request from the client
private byte[] m_buffer;
private int m_received;

// the policy to return to the client
private byte[] m_policy;

// the request that we're expecting from the client
private static string s_policyRequestString = "<policy-file-request/>";



public PolicyConnection(Socket client, byte[] policy)
{
m_connection
= client;
m_policy
= policy;

m_buffer
= new byte[s_policyRequestString.Length];
m_received
= 0;

try
{
// receive the request from the client
m_connection.BeginReceive(m_buffer, 0, s_policyRequestString.Length, SocketFlags.None, new AsyncCallback(OnReceive), null);
}
catch (SocketException)
{
m_connection.Close();
}
}

// Called when we receive data from the client
private void OnReceive(IAsyncResult res)
{
try
{
m_received
+= m_connection.EndReceive(res);

// if we haven't gotten enough for a full request yet, receive again
if (m_received < s_policyRequestString.Length)
{
m_connection.BeginReceive(m_buffer, m_received, s_policyRequestString.Length
- m_received, SocketFlags.None, new AsyncCallback(OnReceive), null);
return;
}

// make sure the request is valid
string request = System.Text.Encoding.UTF8.GetString(m_buffer, 0, m_received);
if (StringComparer.InvariantCultureIgnoreCase.Compare(request, s_policyRequestString) != 0)
{
m_connection.Close();
return;
}

// send the policy
m_connection.BeginSend(m_policy, 0, m_policy.Length, SocketFlags.None, new AsyncCallback(OnSend), null);
}
catch (SocketException)
{
m_connection.Close();
}
}

// called after sending the policy to the client; close the connection.
public void OnSend(IAsyncResult res)
{
try
{
m_connection.EndSend(res);
}
finally
{
m_connection.Close();
}
}
}

// Listens for connections on port 943 and dispatches requests to a PolicyConnection
class PolicyServer
{
private Socket m_listener;
private byte[] m_policy;

// pass in the path of an XML file containing the socket policy
public PolicyServer(string policyFile)
{
// Load the policy file
FileStream policyStream = new FileStream(policyFile, FileMode.Open);

m_policy
= new byte[policyStream.Length];
policyStream.Read(m_policy,
0, m_policy.Length);

policyStream.Close();

m_listener
= new Socket(AddressFamily.InterNETworkV6, SocketType.Stream, ProtocolType.Tcp);

m_listener.SetSocketOption(SocketOptionLevel.IPv6, (SocketOptionName)
27, 0);

m_listener.Bind(
new IPEndPoint(IPAddress.IPv6Any, 943));
m_listener.Listen(
10);

m_listener.BeginAccept(
new AsyncCallback(OnConnection), null);
}

public void OnConnection(IAsyncResult res)
{
Socket client
= null;

try
{
client
= m_listener.EndAccept(res);
}
catch (SocketException)
{
return;
}

// handle this policy request with a PolicyConnection
PolicyConnection pc = new PolicyConnection(client, m_policy);

// look for more connections
m_listener.BeginAccept(new AsyncCallback(OnConnection), null);
}

public void Close()
{
m_listener.Close();
}
}
public class Program
{
static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine(
"usage: PolicyServer.exe PolicyFile.xml");
return;
}

PolicyServer ps
= new PolicyServer(args[0]);
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
}
}
}

NET技術(shù)模擬IIS向Silverlight輸出策略文件,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 香蕉久久av一区二区三区 | 亚洲精品第二页 | 欧美性暴力变态xxxx | 色婷婷五月综合久久中文字幕 | 大桥未久电影在线 | 久久九九精品国产自在现线拍 | 九热这里只有精品 | 全黄H全肉细节文NP 全黄h全肉细节全文 | 海角国精产品一区一区三区糖心 | 精品无码久久久久久国产百度 | 伊人久久影院大香线蕉 | 收集最新中文国产中文字幕 | 国产成人免费a在线视频app | 欧美丰满熟妇BBB久久久 | a视频在线观看 | 飘雪在线观看免费高清完整版韩国 | 成年人视频免费在线播放 | aaaaaa级特色特黄的毛片 | 2019午夜75福利不卡片在线 | 亚洲一日韩欧美中文字幕在线 | 99视频在线观看免费 | 国产一区二区三区国产精品 | 国产日韩欧美高清免费视频 | 午夜aaaa| 亚洲中文字幕在线精品 | 岛国片免费在线观看 | 九九在线中文字幕无码 | 麻豆成人久久精品二区三区网站 | 大相蕉伊人狼人久草av | 欧美国产精品主播一区 | 欧洲电影巜肉欲丛林 | 色多多涩涩屋下载软件 | 日日夜夜操操操 | 在线天天看片视频免费观看 | 久久精品免费观看久久 | 91九色精品国产免费 | av狼新人开放注册区 | 日本漫画大全无翼乌 | 国产高清免费观看 | 亚洲高清一区二区三区电影 | 精品一区二区三区免费毛片 |