📄 sipconnectionimp.cs
字号:
namespace Imps.Client.CommLayer
{
using Imps.Client.Base;
using Imps.Client.Base.Comm;
using Imps.Client.CommLayer.Common;
using Imps.Client.CommLayer.HttpSipConnection;
using Imps.Client.CommLayer.TcpSipConnection;
using Imps.Client.Core;
using Imps.Client.Resource;
using Imps.Client.Utils;
using Imps.Utils;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
internal class SipConnectionImp : ISipConnection, IDisposable
{
private ISipConnection _candidateConn;
private List<ConnectionInfo> _ConnectionTypes;
private object _Context;
private string _Host;
private int _Port;
private volatile ConnectionState _State;
private ISipConnection conBase;
private SipCRegAction regAction;
public event EventHandler<FailedEventArgs> ConnectFailed;
public event EventHandler<ConnectionInfoArgs> ReadNextConnectionInfo;
public event EventHandler<SipRequestReceivedEventArgs> RequestReceived;
public event EventHandler<SendFailedEventArgs> SendFailed;
public event EventHandler<StateChangedEventArgs> StateChanged;
public SipConnectionImp()
{
this._ConnectionTypes = new List<ConnectionInfo>();
}
public SipConnectionImp(object context) : this()
{
this._Context = context;
}
public void Close()
{
if (this.conBase != null)
{
this.conBase.Close();
}
}
public void Connect(string host, int port)
{
string explanation;
this.State = ConnectionState.Connecting;
this._Host = host;
this._Port = port;
TryNextType tryType = TryNextType.TryAll;
using (IEnumerator<ConnectionInfo> enumerator = this.GetConnectionTypes().GetEnumerator())
{
while (enumerator.MoveNext())
{
ConnectionInfo info = enumerator.get_Current();
if ((tryType != TryNextType.TryOnlyHttp) || (info.Type == typeof(Imps.Client.CommLayer.HttpSipConnection.HttpSipConnection)))
{
ManualResetEvent waitConnectionResult = new ManualResetEvent(false);
this.TryConnection(host, port, info, delegate (FailedEventArgs args) {
if (args != null)
{
tryType = this.GetTryNextType(info, args.FailedException);
ClientLogger.WriteConnectionFailed(info.Type.Name, args.FailedException.Message);
}
waitConnectionResult.Set();
});
waitConnectionResult.WaitOne(0x2bf20, true);
if (this.State == ConnectionState.Connected)
{
if ((this.conBase != null) && (this.State == ConnectionState.Connected))
{
this.SetSipConnectionEventSink();
}
return;
}
if (((this.regAction != null) && (this.regAction.SipResponseCode == 410)) || (tryType == TryNextType.NotTry))
{
goto Label_011C;
}
}
}
}
Label_011C:
explanation = StringTable.ClientCommLayerString.DefaultErrorText;
this.RaiseConnectFailed(new FailedEventArgs(new SipConnectionException(explanation), explanation));
}
public void Dispose()
{
if (this.conBase != null)
{
this.conBase.Dispose();
}
}
private IEnumerable<ConnectionInfo> GetConnectionTypes()
{
<GetConnectionTypes>d__f _f = new <GetConnectionTypes>d__f(-2);
_f.<>4__this = this;
return _f;
}
private TryNextType GetTryNextType(ConnectionInfo info, Exception error)
{
if ((info.Type == typeof(Imps.Client.CommLayer.TcpSipConnection.TcpSipConnection)) && (error is SocketException))
{
ClientLogger.WriteConnectionFailed("TCP连接失败", error.ToString());
if ((error as SocketException).get_SocketErrorCode() == 0x2742)
{
return TryNextType.NotTry;
}
}
return TryNextType.TryAll;
}
public long NextCallId()
{
if (this.conBase == null)
{
return (long) (-1);
}
return this.conBase.NextCallId();
}
private void RaiseConnectFailed(FailedEventArgs argument)
{
SipMessageHelper.RaiseEventInThreadPool<FailedEventArgs>(new RaiseMethodWrapper<FailedEventArgs>(delegate (FailedEventArgs e) {
EventHandler<FailedEventArgs> connectFailed = this.ConnectFailed;
if (connectFailed != null)
{
if ((this.conBase != null) && (this.conBase.State != ConnectionState.Disconnected))
{
connectFailed.Invoke(this, e);
}
else
{
connectFailed.Invoke(this, e);
}
}
}), argument);
}
private ConnectionInfo RaiseReadNextConnectionInfo()
{
EventHandler<ConnectionInfoArgs> readNextConnectionInfo = this.ReadNextConnectionInfo;
if (readNextConnectionInfo != null)
{
ConnectionInfoArgs args = new ConnectionInfoArgs();
readNextConnectionInfo.Invoke(this, args);
return args.Info;
}
return null;
}
private void RaiseRequestReceived(SipRequestReceivedEventArgs argument)
{
SipMessageHelper.RaiseEventInThreadPool<SipRequestReceivedEventArgs>(new RaiseMethodWrapper<SipRequestReceivedEventArgs>(delegate (SipRequestReceivedEventArgs e) {
EventHandler<SipRequestReceivedEventArgs> requestReceived = this.RequestReceived;
if (requestReceived != null)
{
requestReceived.Invoke(this, e);
}
}), argument);
}
private void RaiseSendFailed(SendFailedEventArgs argument)
{
SipMessageHelper.RaiseEventInThreadPool<SendFailedEventArgs>(new RaiseMethodWrapper<SendFailedEventArgs>(delegate (SendFailedEventArgs e) {
EventHandler<SendFailedEventArgs> sendFailed = this.SendFailed;
if (sendFailed != null)
{
sendFailed.Invoke(this, e);
}
}), argument);
}
private void RaiseStateChanged(StateChangedEventArgs argument)
{
SipMessageHelper.RaiseEventInThreadPool<StateChangedEventArgs>(new RaiseMethodWrapper<StateChangedEventArgs>(delegate (StateChangedEventArgs e) {
EventHandler<StateChangedEventArgs> stateChanged = this.StateChanged;
if (stateChanged != null)
{
stateChanged.Invoke(this, e);
}
}), argument);
}
public void ReConnect()
{
if ((this.State != ConnectionState.Connecting) && (this.State != ConnectionState.Connected))
{
this.Connect(this._Host, this._Port);
}
}
public void RegisterConnectionType(Type type, SipConnectionOption option)
{
this.ConnectionTypes.Add(new ConnectionInfo(type, option));
}
public void Send(SipMessageBase msg)
{
if ((this.conBase != null) && ((this.conBase.State == ConnectionState.Connected) || (this.conBase.State == ConnectionState.Connecting)))
{
this.conBase.Send(msg);
}
else
{
this.RaiseSendFailed(new SendFailedEventArgs(msg, new ApplicationException(StringTable.ClientCommLayerString.NetworkDownSendFaildText)));
}
}
private void SetSipConnectionEventSink()
{
EventHandler<FailedEventArgs> handler = null;
EventHandler<SipRequestReceivedEventArgs> handler2 = null;
EventHandler<SendFailedEventArgs> handler3 = null;
EventHandler<StateChangedEventArgs> handler4 = null;
if (this.conBase.State == ConnectionState.Connected)
{
if (handler == null)
{
handler = new EventHandler<FailedEventArgs>(this, (IntPtr) this.<SetSipConnectionEventSink>b__7);
}
this.conBase.ConnectFailed += handler;
if (handler2 == null)
{
handler2 = new EventHandler<SipRequestReceivedEventArgs>(this, (IntPtr) this.<SetSipConnectionEventSink>b__8);
}
this.conBase.RequestReceived += handler2;
if (handler3 == null)
{
handler3 = new EventHandler<SendFailedEventArgs>(this, (IntPtr) this.<SetSipConnectionEventSink>b__9);
}
this.conBase.SendFailed += handler3;
if (handler4 == null)
{
handler4 = new EventHandler<StateChangedEventArgs>(this, (IntPtr) this.<SetSipConnectionEventSink>b__a);
}
this.conBase.StateChanged += handler4;
}
}
public override string ToString()
{
if (this.InnerSipConnection == null)
{
return StringTable.ClientCommLayerString.NoConnection;
}
return this.InnerSipConnection.ToString();
}
private void TryConnection(string host, int port, ConnectionInfo connectionInfo, ConnectResultDelegate connectResultProc)
{
<>c__DisplayClass17 class2;
TryConnectionProc proc = new TryConnectionProc(connectionInfo, this.Context);
proc.StateChanged += new EventHandler<StateChangedEventArgs>(class2, (IntPtr) this.<TryConnection>b__15);
proc.Failed += new EventHandler<FailedEventArgs>(class2, (IntPtr) this.<TryConnection>b__16);
proc.Connect(host, port, out this._candidateConn);
}
private List<ConnectionInfo> ConnectionTypes
{
get
{
return this._ConnectionTypes;
}
}
public object Context
{
get
{
if (this.conBase != null)
{
return this.conBase.Context;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -