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.

191 lines
4.6 KiB

1 month ago
/*
Sog
2016 by zouwei
*/
using System;
using System.Net;
using System.Net.Sockets;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Sog
{
public abstract class ClientSession : IMessageTrans, IDisposable
{
public abstract void Connect();
public abstract void Reconnect();
public abstract void Update(long nowMs);
protected abstract void CloseSession();
public NetSession NetSessionObj { get; protected set; }
protected Socket m_socketClient;
protected ClientSessionSettings settings;
protected SessionNetSelectMode netSelectMode;
protected bool m_isConnected;
protected bool m_isConnecting;
protected bool m_cleanUp;
/// <summary>
/// 接收到数据事件
/// </summary>
public event NetEventHandler DataReceived;
protected void OnDataReceived(object sender, SessionEventArgs e)
{
TraceLog.TraceDetail("ClientSession.OnDataReceived recv data, remote {0} session {1} length {2}"
, e.Session.RemoteEndPoint, e.Session.SessionID, e.Message.Header.Length);
if (DataReceived != null)
{
DataReceived(this, e);
}
}
/// <summary>
/// 连接成功事件
/// </summary>
public event NetEventHandler Connected;
protected void OnConnected(SessionEventArgs e)
{
TraceLog.Trace("ClientSession.OnConnected local {0} remote {1} succ"
, m_socketClient.LocalEndPoint, settings.RemoteEndPoint);
if (Connected != null)
{
Connected(this, e);
}
}
/// <summary>
/// 连接失败事件
/// </summary>
public event NetEventHandler ConnectFail;
protected void OnConnectFail(SessionEventArgs e)
{
TraceLog.Trace("ClientSession.OnConnectFail remote {0} fail", settings.RemoteEndPoint);
m_isConnected = false;
m_isConnecting = false;
if (ConnectFail != null)
{
ConnectFail(this, e);
}
}
/// <summary>
/// 连接断开事件
/// </summary>
public event NetEventHandler Disconnected;
protected void OnDisconnected(SessionEventArgs e)
{
TraceLog.Trace("ClientSession.OnDisconnected connection lost {0}", settings.RemoteEndPoint);
if (Disconnected != null)
{
Disconnected(this, e);
}
CloseSession();
}
public ClientSession(ClientSessionSettings clientSettings, SessionNetSelectMode netMode)
{
netSelectMode = netMode;
settings = clientSettings;
}
public void Close()
{
Dispose(true);
}
public void Dispose()
{
Dispose(true);
}
protected virtual void SocketErrorOnUpdate()
{
SessionEventArgs e = new SessionEventArgs();
e.Session = NetSessionObj;
OnDisconnected(e);
CloseSession();
}
protected void ConnectSucc()
{
m_isConnected = true;
m_isConnecting = false;
NetSessionObj.Bind(m_socketClient);
NetSessionObj.LastRecvDataTimeSecond = AppTime.GetNowSysSecond();
SessionEventArgs e = new SessionEventArgs();
e.Session = NetSessionObj;
OnConnected(e);
}
//调用这个直接发送数据
internal void DirectSendNoQueue(MessageData message)
{
NetSessionObj.DirectSend(message);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
FreeResources();
GC.SuppressFinalize(this);
}
}
public void FreeResources()
{
if (m_cleanUp)
{
return;
}
if (netSelectMode == SessionNetSelectMode.Asynchronous)
{
NetSessionObj.AsyncModeDataReceived -= OnDataReceived;
}
CloseSession();
m_cleanUp = true;
}
public void Send(NetSession session, MessageData message)
{
}
public void Send(MessageData message)
{
NetSessionObj.SendMessageToQueue(message);
}
public bool IsConnect()
{
return m_isConnected;
}
}
}