📄 srvrsock.cs
字号:
}catch(ObjectDisposedException){
}catch(Exception eX){
SrvrRedirectorSocket.CloseConnection(connDets);
SrvrRedirectorSocket.OnServerMessagesEventHandler(new ServerMessagesEventArgs(string.Format("ERR> Exception @ cbHandleIncomingTrafficServer(...); Message: {0}", eX.Message)));
}
}
#endregion
}
#endregion
#region ConnectionDetails Class
public class ConnectionDetails : IDisposable{
#region Vars within this scope
private Socket extSocket = null;
private Socket intSocket = null;
private IPEndPoint extEp = null;
//private IPEndPoint intEp = null;
private byte[] bBufExt = null;
private byte[] bBufInt = null;
private bool bIntSocketOk = false;
private long lBytesSent = 0;
private long lBytesReceived = 0;
private DateTime dtBeginRedirecting;
private DateTime dtEndRedirecting;
private bool disposed = false;
#endregion
#region ConnectionDetails Constructor
public ConnectionDetails(){ }
#endregion
#region ConnectionDetails Destructor
~ConnectionDetails(){
Dispose(true);
}
#endregion
#region Properties
#region ConnectionSocketExternal Get/Set Accessors
public Socket ConnectionSocketExternal{
get{ return this.extSocket; }
set{ this.SetUpConnectionExternalEndPoint(value); }
}
#endregion
#region ConnectionSocketInternal Get/Set Accessors
public Socket ConnectionSocketInternal{
get{ return this.intSocket; }
}
#endregion
#region ConnectionExternalAddress Get Accessor
public IPAddress ConnectionExternalAddress{
get{ return this.extEp.Address; }
}
#endregion
#region ConnectionExternalPort Get Accessor
public int ConnectionExternalPort{
get{ return this.extEp.Port; }
}
#endregion
#region ConnectionExternalBuffer Get/Set Accessor
public byte[] ConnectionExternalBuffer{
get{ return this.bBufExt; }
set{ this.bBufExt = value; }
}
#endregion
#region ConnectionInternalBuffer Get/Set Accessor
public byte[] ConnectionInternalBuffer{
get{ return this.bBufInt; }
set{ this.bBufInt = value; }
}
#endregion
#region ConnectionBytesSent Get/Set Accessor
public long ConnectionBytesSent{
get{ return this.lBytesSent; }
set{ this.lBytesSent = value; }
}
#endregion
#region ConnectionBytesReceived Get/Set Accessor
public long ConnectionBytesReceived{
get{ return this.lBytesReceived; }
set{ this.lBytesReceived = value; }
}
#endregion
#region ConnectionSocketInternalOk Get Accessor
public bool ConnectionSocketInternalOk{
get{ return this.bIntSocketOk; }
}
#endregion
#region ConnectionBegan Get/Set Accessor
public DateTime ConnectionBegan{
get{ return this.dtBeginRedirecting; }
set{ this.dtBeginRedirecting = value; }
}
#endregion
#region ConnectionEnded Get/Set Accessor
public DateTime ConnectionEnded{
get{ return this.dtEndRedirecting; }
set{ this.dtEndRedirecting = value; }
}
#endregion
#endregion
#region SetUpConnectionExternalEndPoint Method
private void SetUpConnectionExternalEndPoint(Socket _sock){
this.extSocket = _sock;
this.extEp = (IPEndPoint)this.extSocket.RemoteEndPoint;
}
#endregion
#region OpenInternalConnection Function
public bool OpenInternalConnection(string DestIPAddr, int DestIPPort){
try{
string hostName = DestIPAddr.ToLower();
IPHostEntry localMachineInfo = null;
try{
if (Helper.IsHostDottedQuad(hostName)){
localMachineInfo = Dns.GetHostByAddress(hostName);
}else{
localMachineInfo = Dns.Resolve(hostName);
}
}catch(SocketException){
SrvrRedirectorSocket.OnServerMessagesEventHandler(new ServerMessagesEventArgs(string.Format(">> Unable to resolve {0}. Terminating.", hostName)));
return false;
}
int nHostAddressIndex = 0;
if (localMachineInfo.AddressList.Length > 1 && Helper.IsHostDottedQuad(hostName)){
for (int nLoopCnt = 0; nLoopCnt < localMachineInfo.AddressList.Length; nLoopCnt++){
if (localMachineInfo.AddressList[nLoopCnt].ToString().Equals(hostName)){
nHostAddressIndex = nLoopCnt;
break;
}
}
}
IPEndPoint myEndPoint = new IPEndPoint(localMachineInfo.AddressList[nHostAddressIndex], DestIPPort);
this.intSocket = new Socket(myEndPoint.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
this.intSocket.Connect(myEndPoint);
this.bIntSocketOk = true;
}catch(SocketException sockEx){
if (sockEx.NativeErrorCode == 10053 || sockEx.NativeErrorCode == 10054){
}else{
if (sockEx.NativeErrorCode == 10061){
SrvrRedirectorSocket.OnServerMessagesEventHandler(new ServerMessagesEventArgs(string.Format("ERR>> Could not connect to [{0}:{1}]", DestIPAddr, DestIPPort)));
}else{
SrvrRedirectorSocket.OnServerMessagesEventHandler(new ServerMessagesEventArgs(string.Format("ERR>> Socket Exception @ OpenInternalConnection(...); #[{0}] Message: {1}", sockEx.NativeErrorCode, sockEx.Message)));
}
}
this.bIntSocketOk = false;
}catch(Exception eX){
SrvrRedirectorSocket.OnServerMessagesEventHandler(new ServerMessagesEventArgs(string.Format("ERR> Exception @ OpenInternalConnection(...); Message: {0}", eX.Message)));
this.bIntSocketOk = false;
}
return this.bIntSocketOk;
}
#endregion
#region Equals Override
public override bool Equals(object obj) {
ConnectionDetails rhs = obj as ConnectionDetails;
if (rhs != null){
if ((this.ConnectionExternalAddress == rhs.ConnectionExternalAddress) &&
(this.ConnectionExternalPort.Equals(rhs.ConnectionExternalPort)) &&
(this.ConnectionSocketExternal.Equals(rhs.ConnectionSocketExternal))){
return true;
}else{
return false;
}
}
return false;
}
#endregion
#region GetHashCode Override
public override int GetHashCode() {
return this.GetHashCode ();
}
#endregion
#region ToString Override
public override string ToString() {
return string.Format("[{0}:{1}]", this.extEp.Address.ToString(), this.extEp.Port);
}
#endregion
#region IDisposable Implementation
public void Dispose(){
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing) {
// Check to see if Dispose has already been called.
if(!this.disposed) {
if (this.intSocket != null){
this.intSocket.Shutdown(SocketShutdown.Both);
this.intSocket.Close();
}
if (this.extSocket != null){
this.extSocket.Shutdown(SocketShutdown.Both);
this.extSocket.Close();
}
this.extEp = null;
this.bBufExt = null;
this.bBufInt = null;
// If disposing equals true, dispose all managed
// and unmanaged resources.
if(disposing) {
}
}
disposed = true;
}
#endregion
}
#endregion
#region ServerMessagesEventArgs Class
public class ServerMessagesEventArgs : System.EventArgs{
public readonly string ServerMessage;
public ServerMessagesEventArgs(string _ServerMessage){
this.ServerMessage = _ServerMessage;
}
}
#endregion
#region StatisticsEventArgs Class
public class StatisticsEventArgs : System.EventArgs{
public readonly string Statistics;
public StatisticsEventArgs(string _Statistics){
this.Statistics = _Statistics;
}
}
#endregion
#region ServerDumpDataEventArgs Class
public class ServerDumpDataEventArgs : System.EventArgs{
public readonly string ServerDumpData;
public ServerDumpDataEventArgs(string _ServerDumpData){
this.ServerDumpData = _ServerDumpData;
}
}
#endregion
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -