📄 icebridge.ashx
字号:
<%@ WebHandler Language="c#" class="IceBridge.Bridge" %>
// **********************************************************************
//
// Copyright (c) 2003-2008 ZeroC, Inc. All rights reserved.
//
// This copy of Chat Demo is licensed to you under the terms
// described in the CHAT_DEMO_LICENSE file included in this// distribution.
//
// **********************************************************************
using System;
using System.Diagnostics;
using System.Web;
using System.Collections.Specialized;
using System.Collections.Generic;
namespace IceBridge
{
sealed class ReplyStatus
{
public const byte replyOK = 0;
public const byte replyUserException = 1;
public const byte replyObjectNotExist = 2;
public const byte replyFacetNotExist = 3;
public const byte replyOperationNotExist = 4;
public const byte replyUnknownLocalException = 5;
public const byte replyUnknownUserException = 6;
public const byte replyUnknownException = 7;
}
sealed class Protocol
{
//
// Size of the Ice http tunnelling protocol header
//
// Magic number (4 bytes)
// Protocol version major (Byte)
// Protocol version minor (Byte)
// Encoding version major (Byte)
// Encoding version minor (Byte)
//
internal const int headerSize = 8;
//
// The magic number at the front of each message
//
internal static readonly byte[] magic
= new byte[] { (byte)0x49, (byte)0x48, (byte)0x54, (byte)0x50 }; // 'I', 'H', 'T', 'P'
//
// The current Ice protocol and encoding version
//
internal const byte protocolMajor = 1;
internal const byte protocolMinor = 0;
internal const byte encodingMajor = 1;
internal const byte encodingMinor = 0;
internal static readonly byte[] requestHdr = new byte[]
{
Protocol.magic[0], Protocol.magic[1], Protocol.magic[2],
Protocol.magic[3],
Protocol.protocolMajor, Protocol.protocolMinor,
Protocol.encodingMajor, Protocol.encodingMinor,
};
internal static readonly byte[] replyHdr = new byte[]
{
Protocol.magic[0], Protocol.magic[1], Protocol.magic[2],
Protocol.magic[3],
Protocol.protocolMajor, Protocol.protocolMinor,
Protocol.encodingMajor, Protocol.encodingMinor,
};
}
public class Bridge : IHttpHandler
{
private EventLog _log;
public Bridge()
{
_log = new EventLog();
_log.Source = "IceBridge.Bridge";
}
public void
read(System.IO.Stream s, byte[] data, int pos, int remaining)
{
while(remaining > 0)
{
int ret = s.Read(data, pos, remaining);
if(ret == 0)
{
throw new System.IO.IOException();
}
remaining -= ret;
}
}
public string extractPart(string opt, string source) { string str = source; int start = str.IndexOf(opt); if(start == -1) { return ""; } str = str.Substring(start + opt.Length).Trim(); int end = str.IndexOf(' '); if(end == -1) { return str; } else { return str.Substring(0, end).Trim(); } } public void ProcessRequest(HttpContext context)
{
try
{
Ice.Communicator com = (Ice.Communicator)context.Application["com"];
byte[] requestData = new byte[context.Request.ContentLength];
read(context.Request.InputStream, requestData, 0, context.Request.ContentLength);
Ice.InputStream stream = Ice.Util.createInputStream(com, requestData);
byte[] m = stream.readBlob(4);
if(m[0] != Protocol.magic[0] || m[1] != Protocol.magic[1] ||
m[2] != Protocol.magic[2] || m[3] != Protocol.magic[3])
{
Ice.BadMagicException ex = new Ice.BadMagicException();
ex.badMagic = m;
throw ex;
}
byte pMajor = stream.readByte();
byte pMinor = stream.readByte();
if(pMajor != Protocol.protocolMajor)
{
Ice.UnsupportedProtocolException e = new Ice.UnsupportedProtocolException();
e.badMajor = pMajor < 0 ? pMajor + 255 : pMajor;
e.badMinor = pMinor < 0 ? pMinor + 255 : pMinor;
e.major = Protocol.protocolMajor;
e.minor = Protocol.protocolMinor;
throw e;
}
byte eMajor = stream.readByte();
byte eMinor = stream.readByte();
if(eMajor != Protocol.encodingMajor)
{
Ice.UnsupportedEncodingException e = new Ice.UnsupportedEncodingException();
e.badMajor = eMajor < 0 ? eMajor + 255 : eMajor;
e.badMinor = eMinor < 0 ? eMinor + 255 : eMinor;
e.major = Protocol.encodingMajor;
e.minor = Protocol.encodingMinor;
throw e;
}
String op = stream.readString();
Ice.ObjectPrx proxy = stream.readProxy();
Ice.OperationMode mode = (Ice.OperationMode)stream.readByte();
if(mode != Ice.OperationMode.Normal && mode != Ice.OperationMode.Nonmutating &&
mode != Ice.OperationMode.Idempotent)
{
throw new Ice.MarshalException("unexpected mode received: " + mode);
}
string filterHost = com.getProperties().getProperty("IceBridge.Filter.Host"); string filterPort = com.getProperties().getProperty("IceBridge.Filter.Port"); if(filterHost.Length > 0 | filterPort.Length > 0) { Ice.Endpoint[] endpoints = proxy.ice_getEndpoints(); for(int i = 0; i < endpoints.Length; i++) { string endpoint = endpoints[i].ToString(); if(filterHost.Length > 0) { string host = extractPart("-h", endpoint); if(!host.Equals(filterHost)) { context.Response.StatusCode = 500; return; } } if(filterPort.Length > 0) { string port = extractPart("-p", endpoint); if(!port.Equals(filterPort)) { context.Response.StatusCode = 500; return; } } } } Dictionary<string, string> ctx = null;
int sz = stream.readSize();
while(sz-- > 0)
{
string first = stream.readString();
string second = stream.readString();
if(ctx == null)
{
ctx = new Dictionary<string, string>();
}
ctx[first] = second;
}
Ice.OutputStream os = Ice.Util.createOutputStream(com);
byte replyStatus = 0;
try
{
stream.startEncapsulation();
sz = stream.getEncapsulationSize();
byte[] inParams = stream.readBlob(sz);
byte[] data;
bool ok = proxy.ice_invoke(op, mode, inParams, out data, ctx);
if(ok)
{
replyStatus = ReplyStatus.replyOK;
}
else
{
replyStatus = ReplyStatus.replyUserException;
}
os.startEncapsulation();
os.writeBlob(data);
os.endEncapsulation();
}
catch(Ice.RequestFailedException ex)
{
if(ex.operation == null || ex.operation.Length == 0)
{
ex.operation = op;
}
if(ex is Ice.ObjectNotExistException)
{
replyStatus = ReplyStatus.replyObjectNotExist;
}
else if(ex is Ice.FacetNotExistException)
{
replyStatus = ReplyStatus.replyFacetNotExist;
}
else if(ex is Ice.OperationNotExistException)
{
replyStatus = ReplyStatus.replyOperationNotExist;
}
else
{
Debug.Assert(false);
}
//
// TODO: Ice.Identity was not compiled with streaming
// support
//
// ex.id.write__(os);
os.writeString(ex.id.name);
os.writeString(ex.id.category);
if(ex.facet == null || ex.facet.Length == 0)
{
os.writeStringSeq(null);
}
else
{
string[] facetPath2 = { ex.facet };
os.writeStringSeq(facetPath2);
}
os.writeString(ex.operation);
}
catch(Ice.UnknownLocalException ex)
{
replyStatus = ReplyStatus.replyUnknownLocalException;
os.writeString(ex.unknown);
}
catch(Ice.UnknownUserException ex)
{
replyStatus = ReplyStatus.replyUnknownUserException;
os.writeString(ex.unknown);
}
catch(Ice.UnknownException ex)
{
replyStatus = ReplyStatus.replyUnknownException;
os.writeString(ex.unknown);
}
catch(Ice.LocalException ex)
{
replyStatus = ReplyStatus.replyUnknownLocalException;
os.writeString(ex.ToString());
}
catch(Ice.UserException ex)
{
Debug.Assert(false); // Impossible
}
catch(System.Exception ex)
{
replyStatus = ReplyStatus.replyUnknownException;
os.writeString(ex.ToString());
}
byte[] replyData = os.finished();
context.Response.ContentType = "application/binary";
context.Response.OutputStream.Write(Protocol.replyHdr, 0, Protocol.replyHdr.Length);
context.Response.OutputStream.Write(BitConverter.GetBytes(replyData.Length + 1), 0, 4);
context.Response.OutputStream.WriteByte(replyStatus);
context.Response.OutputStream.Write(replyData, 0, replyData.Length);
}
catch(Exception ex)
{
Console.WriteLine("Bridging error: " + ex.ToString());
Debug.Write("Bridging error: " + ex.ToString());
_log.WriteEntry("Bridging Error: " + ex.ToString());
context.Response.StatusCode = 500;
}
}
public bool IsReusable
{
get
{
return true;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -