-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Server Session Remove. Add File Function Describe.
- Loading branch information
1 parent
d95eac7
commit d930fe5
Showing
5 changed files
with
73 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
@@ -52,9 +63,8 @@ private void RcvHeadData(IAsyncResult ar) { | |
} | ||
else { | ||
OnDisConnected(); | ||
skt.Close(); | ||
Clear(); | ||
} | ||
|
||
} | ||
catch (Exception e) { | ||
PETool.LogMsg("RcvHeadError:" + e.Message, LogLevel.Error); | ||
|
@@ -92,7 +102,7 @@ private void RcvBodyData(IAsyncResult ar) { | |
} | ||
else { | ||
OnDisConnected(); | ||
skt.Close(); | ||
Clear(); | ||
} | ||
} | ||
catch (Exception e) { | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -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) { | ||
|
@@ -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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
|