Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.

Commit

Permalink
아직 테스트는 하지 못했음.
Browse files Browse the repository at this point in the history
  • Loading branch information
jacking75 committed Dec 23, 2019
1 parent a1673d0 commit 915c42f
Show file tree
Hide file tree
Showing 21 changed files with 229 additions and 504 deletions.
6 changes: 3 additions & 3 deletions FreeNet/DefaultMessageResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ namespace FreeNet
/// - header : 총 크기(2바이트), 패킷ID( 2바이트), 패킷 타입(1바이트)
/// - body : 메시지 본문.
/// </summary>
class DefaultMessageResolver : IMessageResolver
public class DefaultMessageResolver : IMessageResolver
{
public static readonly short HEADERSIZE = 5;
public static readonly short HEADER_PACKETID_POS = 2;
public static readonly UInt16 HEADERSIZE = 5;
public static readonly UInt16 HEADER_PACKETID_POS = 2;

// 진행중인 버퍼.
byte[] SecondaryBuffer;
Expand Down
2 changes: 1 addition & 1 deletion FreeNet/DefaultPacketDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void IncomingPacket(bool IsSystem, Session user, Packet packet)
// 여긴 IO스레드에서 호출된다.
// 완성된 패킷을 메시지큐에 넣어준다.

if(IsSystem == false && packet.PopProtocolId() <= (short)NetworkDefine.SYS_NTF_MAX)
if(IsSystem == false && packet.ProtocolId <= (short)NetworkDefine.SYS_NTF_MAX)
{
//TODO: 로그 남기기. 여기서는 로거의 인터페이스만 호출해야 한다. 로거의 구현은 애플리케이션에서 구현한다

Expand Down
1 change: 1 addition & 0 deletions FreeNet/FreeNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

<ItemGroup>
<Compile Remove="BufferManager.cs" />
<Compile Remove="HeartbeatSender.cs" />
</ItemGroup>

</Project>
68 changes: 0 additions & 68 deletions FreeNet/HeartbeatSender.cs

This file was deleted.

2 changes: 0 additions & 2 deletions FreeNet/Listener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

namespace FreeNet
{
//TODO: 이것과 별개 혹은 메소드를 추가하여 PostAccept 기능을 추가한다.

class Listener
{
// 비동기 Accept를 위한 EventArgs.
Expand Down
16 changes: 7 additions & 9 deletions FreeNet/NetworkDefine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,20 @@ namespace FreeNet
public class NetworkDefine
{
#region SYSTEM_PACKET
public const short SYS_NTF_CONNECTED = 1;
public const UInt16 SYS_NTF_CONNECTED = 1;

public const short SYS_NTF_CLOSED = 2;
public const UInt16 SYS_NTF_CLOSED = 2;

public const UInt16 SYS_START_HEARTBEAT = 3;

// 리모트에서 받은 패킷의 경우 이 숫자를 넘어서는 것은 에러
public const short SYS_NTF_MAX = 100;
public const UInt16 SYS_NTF_MAX = 100;
#endregion

// 하트비트 시작. S -> C
public const short SYS_START_HEARTBEAT = 11;

// 하트비트 갱신. C -> S
public const short SYS_UPDATE_HEARTBEAT = 12;
}





}
}
19 changes: 9 additions & 10 deletions FreeNet/NetworkService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ namespace FreeNet

public IPacketDispatcher PacketDispatcher { get; private set; }

public SessionManager UserManager { get; private set; }
public SessionManager SessionMgr { get; private set; }

public ServerOption ServerOpt { get; private set; }

Int64 SequenceId = 0;
UInt64 SequenceId = 0;

ReserveClosingProcess ReserveClosingProc = new ReserveClosingProcess();

Expand All @@ -39,7 +39,7 @@ namespace FreeNet
public NetworkService(ServerOption serverOption, IPacketDispatcher userPacketDispatcher = null)
{
ServerOpt = serverOption;
UserManager = new SessionManager();
SessionMgr = new SessionManager();

if (userPacketDispatcher == null)
{
Expand Down Expand Up @@ -134,7 +134,7 @@ public void Listen(string host, int port, int backlog, SocketOption socketOption

// heartbeat.
byte check_interval = 10;
UserManager.StartHeartbeatChecking(check_interval, check_interval);
SessionMgr.StartHeartbeatChecking(check_interval, check_interval);
}

/// <summary>
Expand All @@ -145,7 +145,7 @@ public void OnConnectCompleted(Socket socket, Session token)
{
token.OnSessionClosed += OnSessionClosed;

UserManager.Add(token);
SessionMgr.Add(token);

// SocketAsyncEventArgsPool에서 빼오지 않고 그때 그때 할당해서 사용한다.
// 풀은 서버에서 클라이언트와의 통신용으로만 쓰려고 만든것이기 때문이다.
Expand All @@ -165,7 +165,8 @@ public void OnConnectCompleted(Socket socket, Session token)
BeginReceive(socket, receive_event_arg, send_event_arg);
}

public Int64 MakeSequenceIdForSession() { return Interlocked.Increment(ref SequenceId); }
// 스레드 세이프 하지 않다
public UInt64 MakeSequenceIdForSession() { return ++SequenceId; }
/// <summary>
/// 새로운 클라이언트가 접속 성공 했을 때 호출됩니다.
/// AcceptAsync의 콜백 매소드에서 호출되며 여러 스레드에서 동시에 호출될 수 있기 때문에 공유자원에 접근할 때는 주의해야 합니다.
Expand All @@ -181,7 +182,7 @@ void OnNewClient(Socket client_socket, object token)
user_token.OnSessionClosed += OnSessionClosed;


UserManager.Add(user_token);
SessionMgr.Add(user_token);

// 플에서 하나 꺼내와 사용한다.
SocketAsyncEventArgs receive_args = this.ReceiveEventArgsPool.Pop();
Expand Down Expand Up @@ -268,8 +269,6 @@ private void ProcessReceive(SocketAsyncEventArgs e)

if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
{
//TODO 받은 데이터를 패킷으로 처리 못하고 남은 것이 있는 경우 어떻게?
// 현재 구현은 receive에서 모든 패킷을 남김 없이 다 처리할 수 있다고 가정하고 있음
token.OnReceive(e.Buffer, e.Offset, e.BytesTransferred);

// Keep receive.
Expand All @@ -289,7 +288,7 @@ private void ProcessReceive(SocketAsyncEventArgs e)

public void OnSessionClosed(Session token)
{
UserManager.Remove(token);
SessionMgr.Remove(token);

// Free the SocketAsyncEventArg so they can be reused by another client
// 버퍼는 반환할 필요가 없다. SocketAsyncEventArg가 버퍼를 물고 있기 때문에
Expand Down
Loading

0 comments on commit 915c42f

Please sign in to comment.