Files
lserp_cs_5.0/LSTest/LSServerService/LSSocket.cs
T
2026-07-10 15:25:05 +08:00

235 lines
7.9 KiB
C#

namespace LSServerService
{
using LSUtils;
using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
public class LSSocket : LSASyncSocket
{
private int _selDirectoryId = 0;
private string _ServerIP = "";
private uint _ServerPort = 0;
private const int FIONREAD = 1074030207;
private uint m_dwUserId = 0;
private string m_remoteIP;
private int m_remotePort;
public event ParseReceiveDataEventHandle ParseReceiveDataEvent;
public LSSocket()
{
base._socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}
public void clientAdd(Socket s)
{
base._socket = s;
base.ReceiveReady();
}
public void Connect(string strHost, uint nPort)
{
try
{
this.ServerIP = strHost;
this.ServerPort = nPort;
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse(strHost), (int) nPort);
LSWriteLog.WriteTrace("begin connect server ip={0}, port={1} ", new object[] { strHost, nPort });
base._socket.BeginConnect(remoteEP, new AsyncCallback(this.ConnectCallback), base._socket);
}
catch (SocketException exception)
{
PubUtils.WriteLog("LSSocket.Connect SOCKET error: {0}", new object[] { exception });
}
catch (Exception exception2)
{
PubUtils.WriteLog("LSSocket.Connect error: {0}", new object[] { exception2 });
}
}
private void ConnectCallback(IAsyncResult ar)
{
try
{
((Socket) ar.AsyncState).EndConnect(ar);
LSWriteLog.WriteTrace("connected server ip={0} port={1} success", new object[] { this.ServerIP, this.ServerPort });
base.ReceiveReady();
}
catch (SocketException exception)
{
PubUtils.WriteLog("LSSocket.Connect SOCKET errorcode: {0} errormsg: {1}", new object[] { exception.SocketErrorCode, exception });
base.CheckCloseState(exception.ErrorCode);
}
catch (Exception exception2)
{
PubUtils.WriteLog("LSSocket.ConnectCallback error: {0}", new object[] { exception2 });
}
}
protected override void ParseReceiveData(byte[] byteData)
{
this.ParseReceiveDataEvent(this, byteData);
}
protected override void ReceiveCallback(IAsyncResult ar)
{
try
{
LSWriteLog.WriteTrace(this.ToString() + ".ReceiveCallback is running socketid = " + base._socket.Handle, new object[0]);
if (!((base._socket != null) && base._socket.Connected))
{
base.CloseSocket();
return;
}
LSWriteLog.WriteTrace(string.Concat(new object[] { this.ToString(), ".ReceiveCallback socketid = ", base._socket.Handle, " bytesAvailable is ", base._socket.Available.ToString(), " _socketstate=", this._socketstate.ToString() }), new object[0]);
if ((base._socket.Available == 0) && (base._socketstate == 2))
{
return;
}
int count = base._socket.EndReceive(ar);
byte[] dst = new byte[count];
Buffer.BlockCopy(base.StateObj.buffer, 0, dst, 0, count);
if (count > 0)
{
int num3;
base._ReceivedLen += count;
for (int i = 0; i < base._ReceivedLen; i += num3)
{
bool flag = (base._ReceivedLen - i) <= 8;
Label_0158:
if (flag)
{
if ((base._ReceivedLen - i) < base.StateObj.buffer.Length)
{
Buffer.BlockCopy(base.StateObj.buffer, i, base.StateObj.buffer, 0, base._ReceivedLen - i);
}
else
{
base.StateObj.ExtentBuffer();
}
base._ReceivedLen -= i;
base._socket.BeginReceive(base.StateObj.buffer, base._ReceivedLen, base.StateObj.buffer.Length - base._ReceivedLen, SocketFlags.None, new AsyncCallback(this.ReceiveCallback), base._socket);
return;
}
num3 = BitConverter.ToInt32(base.StateObj.buffer, 4 + i);
if ((num3 + i) > base._ReceivedLen)
{
flag = true;
goto Label_0158;
}
byte[] buffer2 = new byte[num3];
Buffer.BlockCopy(base.StateObj.buffer, i, buffer2, 0, num3);
if (num3 > 102400)
{
byte[] buffer3 = new byte[base._ReceivedLen];
Buffer.BlockCopy(base.StateObj.buffer, 0, buffer3, 0, base._ReceivedLen);
PubUtils.WriteLog("received proxy data is unnormal! socketid= {0} and a packet data is :{1} and whole data is :{2}", new object[] { base._socket.Handle, buffer2, buffer3 });
}
else
{
this.ParseReceiveData(buffer2);
}
}
}
else
{
base.CloseSocket();
}
}
catch (SocketException exception)
{
PubUtils.WriteLog("LSSocket.ReceiveCallback SOCKET errorcode: {0} errormsg: {1}", new object[] { exception.SocketErrorCode, exception });
base.CheckCloseState(exception.ErrorCode);
}
catch (Exception exception2)
{
PubUtils.WriteLog("LSSocket.ReceiveCallback error: {0}", new object[] { exception2 });
}
base.ContinueReceive();
}
public void reConnect()
{
base._socketstate = 0;
this.Connect(this.m_remoteIP, (uint) this.m_remotePort);
}
public uint dwUserId
{
get
{
return this.m_dwUserId;
}
set
{
this.m_dwUserId = value;
}
}
public string remoteIP
{
get
{
return this.m_remoteIP;
}
set
{
this.m_remoteIP = value;
}
}
public int remotePort
{
get
{
return this.m_remotePort;
}
set
{
this.m_remotePort = value;
}
}
public int selDirectoryId
{
get
{
return this._selDirectoryId;
}
set
{
this._selDirectoryId = value;
}
}
public string ServerIP
{
get
{
return this._ServerIP;
}
set
{
this._ServerIP = value;
}
}
public uint ServerPort
{
get
{
return this._ServerPort;
}
set
{
this._ServerPort = value;
}
}
public delegate void ParseReceiveDataEventHandle(LSSocket ProxySocket, byte[] byteData);
}
}