Skip to content

Commit

Permalink
Fix Session Offline Bug.
Browse files Browse the repository at this point in the history
Add Server Session Remove.
Add File Function Describe.
  • Loading branch information
PlaneZhong committed Oct 30, 2018
1 parent d95eac7 commit d930fe5
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 22 deletions.
11 changes: 10 additions & 1 deletion PESocket/PEMsg.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
namespace PENet {
/****************************************************
文件:PEMsg.cs
作者:Plane
邮箱: [email protected]
日期:2018/10/30 11:20
功能:消息定义类
*****************************************************/

namespace PENet {

using System;

[Serializable]
Expand Down
27 changes: 15 additions & 12 deletions PESocket/PEPkg.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
using System;
/****************************************************
文件:PEPkg.cs
作者:Plane
邮箱: [email protected]
日期:2018/10/30 11:20
功能:网络消息包
*****************************************************/

namespace PENet
{
class PEPkg
{
using System;

namespace PENet {
class PEPkg {
public int headLen = 4;
public byte[] headBuff = null;
public int headIndex = 0;
Expand All @@ -12,23 +18,20 @@ class PEPkg
public byte[] bodyBuff = null;
public int bodyIndex = 0;

public PEPkg()
{
public PEPkg() {
headBuff = new byte[4];
}

public void InitBodyBuff()
{
public void InitBodyBuff() {
bodyLen = BitConverter.ToInt32(headBuff, 0);
bodyBuff = new byte[bodyLen];
}

public void ResetData()
{
public void ResetData() {
headIndex = 0;
bodyLen = 0;
bodyBuff = null;
bodyIndex = 0;
}
}
}
}
29 changes: 24 additions & 5 deletions PESocket/PESession.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
using System;
/****************************************************
文件:PESession.cs
作者:Plane
邮箱: [email protected]
日期:2018/10/30 11:20
功能:网络会话管理
*****************************************************/

using System;
using System.Net.Sockets;

namespace PENet {
public abstract class PESession<T> where T : PEMsg {
private Socket skt;
private Action closeCB;

#region Recevie
public void StartRcvData(Socket skt) {
public void StartRcvData(Socket skt, Action closeCB) {
try {
this.skt = skt;
this.closeCB = closeCB;

OnConnected();

PEPkg pack = new PEPkg();
Expand Down Expand Up @@ -52,9 +63,8 @@ private void RcvHeadData(IAsyncResult ar) {
}
else {
OnDisConnected();
skt.Close();
Clear();
}

}
catch (Exception e) {
PETool.LogMsg("RcvHeadError:" + e.Message, LogLevel.Error);
Expand Down Expand Up @@ -92,7 +102,7 @@ private void RcvBodyData(IAsyncResult ar) {
}
else {
OnDisConnected();
skt.Close();
Clear();
}
}
catch (Exception e) {
Expand Down Expand Up @@ -145,6 +155,15 @@ private void SendCB(IAsyncResult ar) {
}
#endregion

/// <summary>
/// Release Resource
/// </summary>
private void Clear() {
if (closeCB != null) {
closeCB();
}
skt.Close();
}

/// <summary>
/// Connect network
Expand Down
18 changes: 15 additions & 3 deletions PESocket/PESocket.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
using System;
/****************************************************
文件:PESocket.cs
作者:Plane
邮箱: [email protected]
日期:2018/10/30 11:20
功能:PESocekt核心类
*****************************************************/

using System;
using System.Net;
using System.Net.Sockets;
using System.Collections.Generic;
Expand Down Expand Up @@ -36,7 +44,11 @@ void ClientConnectCB(IAsyncResult ar) {
try {
Socket clientSkt = skt.EndAccept(ar);
T session = new T();
session.StartRcvData(clientSkt);
session.StartRcvData(clientSkt, () => {
if (sessionLst.Contains(session)) {
sessionLst.Remove(session);
}
});
sessionLst.Add(session);
}
catch (Exception e) {
Expand Down Expand Up @@ -64,7 +76,7 @@ void ServerConnectCB(IAsyncResult ar) {
try {
skt.EndConnect(ar);
session = new T();
session.StartRcvData(skt);
session.StartRcvData(skt, null);
}
catch (Exception e) {
PETool.LogMsg(e.Message, LogLevel.Error);
Expand Down
10 changes: 9 additions & 1 deletion PESocket/PETool.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
using System;
/****************************************************
文件:PETool.cs
作者:Plane
邮箱: [email protected]
日期:2018/10/30 11:21
功能:工具类
*****************************************************/

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

Expand Down

0 comments on commit d930fe5

Please sign in to comment.