You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

170 lines
5.0 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Net.Sockets;
namespace SogClient
{
public class ConnectedClientSessionUpdate
{
//private List<ClientSession> m_needUpdateSession = new List<ClientSession>();
private Dictionary<Socket, ClientSession> m_clientSockets = new Dictionary<Socket, ClientSession>();
private List<Socket> m_readList = new List<Socket>();
private List<Socket> m_writeList = new List<Socket>();
private List<Socket> m_errorList = new List<Socket>();
private Dictionary<WebSession, WebClientSession> m_webClientSession = new Dictionary<WebSession, WebClientSession>();
private List<WebSession> m_webReadList = new List<WebSession>();
private List<WebSession> m_webWriteList = new List<WebSession>();
public void AddWebSessionToList(WebClientSession webClientSession)
{
if (webClientSession.m_webSession == null)
{
return;
}
m_webClientSession.Add(webClientSession.m_webSession, webClientSession);
}
public void AddToList(ClientSession client)
{
if(client.NetSessionObj.WorkSocket == null)
{
return;
}
m_clientSockets.Add(client.NetSessionObj.WorkSocket, client);
}
public void Update()
{
UpdateSocketSession();
UpdateWebSession();
}
private void UpdateWebSession()
{
foreach (var pair in m_webClientSession)
{
WebSession webSession = pair.Key;
WebClientSession webClientSession = pair.Value;
if (webSession.IsClosed)
{
continue;
}
if (webSession.IsHaveDataRead())
{
m_webReadList.Add(webSession);
}
if (webSession.IsHaveDataSend())
{
m_webWriteList.Add(webSession);
}
if (m_webReadList.Count >= 32 || m_webWriteList.Count >= 32)
{
ProcessWebSession();
}
}
if (m_webReadList.Count > 0 || m_webWriteList.Count > 0)
{
ProcessWebSession();
}
m_webClientSession.Clear();
}
private void ProcessWebSession()
{
foreach (WebSession webSession in m_webReadList)
{
m_webClientSession[webSession].ReceiveMessage();
}
m_webReadList.Clear();
foreach (WebSession webSession in m_webWriteList)
{
webSession.TryCopySendBuffer();
}
m_webWriteList.Clear();
}
private void UpdateSocketSession()
{
m_readList.Clear();
m_writeList.Clear();
m_errorList.Clear();
foreach (var pair in m_clientSockets)
{
Socket socket = pair.Key;
ClientSession client = pair.Value;
NetSession session = client.NetSessionObj;
if (session.IsSocketClosed)
{
// AddSocketToNeedClose(socket);
continue;
}
//写的优化
session.CheckSocketAndSendKeepAlive();
if (session.IsSocketClosed)
{
// AddSocketToNeedClose(socket);
continue;
}
m_readList.Add(socket);
if (session.IsHaveDataNeedSend())
{
m_writeList.Add(socket);
}
m_errorList.Add(socket);
if (m_readList.Count >= 32)
{
Socket.Select(m_readList, m_writeList, m_errorList, 0);
process_select_result();
}
}
if (m_readList.Count > 0)
{
Socket.Select(m_readList, m_writeList, m_errorList, 0);
process_select_result();
}
m_clientSockets.Clear();
}
private void process_select_result()
{
//error
foreach (Socket socket in m_errorList)
{
m_clientSockets[socket].OnUpdateConnectedSocketError();
}
m_errorList.Clear();
//read
foreach (Socket socket in m_readList)
{
m_clientSockets[socket].ReadSocketReadable();
}
m_readList.Clear();
//write
foreach (Socket socket in m_writeList)
{
m_clientSockets[socket].NetSessionObj.WriteWriteableSocket();
}
m_writeList.Clear();
}
}
}