📄 httpsipconnection.cs
字号:
{
base.State = ConnectionState.Disconnected;
base.LogError(exception);
base.RaiseConnectFailed(new FailedEventArgs(exception));
}
}
private HttpWebRequest CreateHttpWebRequest(string url)
{
HttpWebRequest request = ConnectionFactory.CreateHttpWebRequest(url, base.Context, true, true);
request.Headers.Add("Pragma", this._Pragma);
request.ContentType = "application/oct-stream";
this._WebProxy = request.Proxy;
return request;
}
public override void Dispose()
{
base.Dispose();
this.CloseTimer();
}
private void EndSendMessage(IAsyncResult result)
{
}
private static long GetNextCSeqNumber()
{
return Interlocked.Increment(ref _CSeqNumber);
}
private int GetTimespanForMessage(SipcMessage msg)
{
string method = msg.CSeq.Method;
if (msg is SipcRequest)
{
switch (method)
{
case "R":
if ((msg.CSeq.Number >= 5) && (msg.Expires == null))
{
return -1;
}
return 500;
case "S":
case "SUB":
return 500;
case "A":
return -1;
}
return 0x7d0;
}
if (method == "I")
{
return 0x7d0;
}
return -1;
}
private string GetWebProxyType()
{
return ("HTTP方式 - " + Tools.GetConnectionStateString(base.State));
}
private void HttpSipConnection_StateChanged(object sender, StateChangedEventArgs e)
{
if (e.NewState == ConnectionState.Connected)
{
this.StartTimer();
}
}
private void KeepTimer(int minPeriod, int maxPeriod)
{
if (minPeriod <= this._CurTimeSpan)
{
if (minPeriod >= 0)
{
this._MinPeriod = minPeriod;
}
if (maxPeriod >= 0)
{
this._MaxPeriod = maxPeriod;
}
this._RefTime = DateTime.Now - new TimeSpan(0, 0, 0, 0, this._MinPeriod * 10);
this.UpdateTimer();
}
}
protected override void OnSendSuccessed(SipMessageBase message)
{
base.OnSendSuccessed(message);
int minPeriod = this.GetTimespanForMessage(message.Message);
if (minPeriod >= 0)
{
this.KeepTimer(minPeriod, 0x3a98);
}
}
private void RaiseAllMessageSendFaild()
{
SipMessageBase msg;
while ((msg = this.ReadMessageItem()) != null)
{
string explanation = StringTable.ClientCommLayerString.DefaultSendFaildText;
SipMessageHelper.RaiseSendMessageFailed(msg, null, explanation, new RaiseEventDelegate<SendFailedEventArgs>(this.RaiseSendFailed));
}
}
private SipMessageBase ReadMessageItem()
{
lock (this._SendingMessageList)
{
foreach (Queue<SipMessageBase> queue in this._SendingMessageList)
{
if (queue.get_Count() > 0)
{
return queue.Dequeue();
}
}
return null;
}
}
private void RunningProc(object state)
{
lock (this._syncLockRunningProc)
{
try
{
this.UpdateTimer();
if (string.IsNullOrEmpty(Thread.CurrentThread.Name))
{
Thread.CurrentThread.Name = "Imps.Client.Pc.HttpSipConnection";
}
List<SipMessageBase> list = null;
SipMessageBase base2 = null;
lock (this._syncLockPeekedMessageFlag)
{
while (true)
{
if (!this._sendWindow.CanSendNow())
{
break;
}
base2 = this.ReadMessageItem();
if (base2 == null)
{
break;
}
if (list == null)
{
list = new List<SipMessageBase>();
}
list.Add(base2);
}
this._peekedMessageFlag = true;
}
if (list != null)
{
base.SendMessage(list.ToArray());
}
else
{
this.SendMessage((byte[]) null);
}
}
catch (Exception exception)
{
base.LogError(exception);
this.Close();
}
Thread.Sleep(500);
}
}
public sealed override void Send(SipMessageBase msg)
{
lock (this._syncLockPeekedMessageFlag)
{
this.AppendMessageItem(msg);
if (this._peekedMessageFlag)
{
this._peekedMessageFlag = false;
this._Timer.Change(0, -1);
}
}
}
protected override void SendMessage(byte[] buffer)
{
string url = this.Option.HttpTurnal + "?t=s&i=" + GetNextCSeqNumber();
if (this.Option.AcceptedMaxLength != 0)
{
url = url + "&l=" + this.Option.AcceptedMaxLength;
}
this._SendMessage(url, buffer, 5);
}
private void StartTimer()
{
this.CloseTimer();
this._Timer = new Timer(new TimerCallback(this.RunningProc));
this.KeepTimer(0x7d0, 0x3a98);
}
public override string ToString()
{
return this.GetWebProxyType();
}
private void UpdateTimer()
{
if (this._Timer != null)
{
int dueTime;
TimeSpan span = (TimeSpan) (DateTime.Now - this._RefTime);
int num = ((int) span.TotalMilliseconds) / 10;
if (num < this._MinPeriod)
{
num = this._MinPeriod;
}
if (num > this._MaxPeriod)
{
num = this._MaxPeriod;
}
this._CurTimeSpan = dueTime = num;
this._Timer.Change(dueTime, -1);
}
}
private void WriteConnectionLog()
{
string proxyHost = CommHelper.GetProxyHost(ConnectionFactory.WebProxy);
string text2 = CommHelper.GetProxyPort(ConnectionFactory.WebProxy).ToString();
string detail = "HttpTunnel: " + this.Option.HttpTurnal + " ProxyHost:" + proxyHost + " ProxyPort:" + text2;
string ext = "<HttpTunnel>" + this.Option.HttpTurnal + "</HttpTunnel>\r\n" + "<ProxyHost>" + proxyHost + "</ProxyHost>" + "<ProxyPort>" + text2 + "</ProxyPort>";
ClientLogger.WriteConnection("HttpSipConnection", 0, detail, ext);
}
public override IPEndPoint LocalEndPoint
{
get
{
return new IPEndPoint(IPAddress.Parse("127.0.0.1"), 0x1f41);
}
}
public HttpSipConnectionOption Option
{
get
{
return (base.Option as HttpSipConnectionOption);
}
}
public override IPEndPoint RemoteEndPoint
{
get
{
if (this._IpEndPoint == null)
{
lock (this._IpEndPointLocker)
{
if (this._IpEndPoint == null)
{
try
{
if (string.IsNullOrEmpty(this.Option.HttpTurnal))
{
this._IpEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 0x1f42);
}
else
{
UriBuilder builder = new UriBuilder(this.Option.HttpTurnal);
this._IpEndPoint = new IPEndPoint(Dns.GetHostEntry(builder.Host).AddressList[0], builder.Port);
}
}
catch
{
this._IpEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 0x1f42);
}
}
}
}
return this._IpEndPoint;
}
}
private delegate void AsyncBeginHttpRequest(string uri);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -