@ -4,6 +4,7 @@ using System.Net;
using System.Net.Sockets ;
using System.Net.Sockets ;
using System.Threading ;
using System.Threading ;
using System.Threading.Tasks ;
using System.Threading.Tasks ;
using ZeroLevel.Services ;
using ZeroLevel.Services.Pools ;
using ZeroLevel.Services.Pools ;
using ZeroLevel.Services.Serialization ;
using ZeroLevel.Services.Serialization ;
@ -93,7 +94,11 @@ namespace ZeroLevel.Network
public void Request ( Frame frame , Action < byte [ ] > callback , Action < string > fail = null )
public void Request ( Frame frame , Action < byte [ ] > callback , Action < string > fail = null )
{
{
if ( frame = = null ) throw new ArgumentNullException ( nameof ( frame ) ) ;
if ( frame = = null ) throw new ArgumentNullException ( nameof ( frame ) ) ;
if ( frame ! = null & & ! _send_queue . IsAddingCompleted )
var data = NetworkPacketFactory . Reqeust ( MessageSerializer . Serialize ( frame ) , out int id ) ;
Dbg . Timestamp ( ( int ) DbgNetworkEvents . ClientStartPushRequest , id . ToString ( ) ) ;
frame . Release ( ) ;
if ( ! _send_queue . IsAddingCompleted )
{
{
while ( _send_queue . Count > = MAX_SEND_QUEUE_SIZE )
while ( _send_queue . Count > = MAX_SEND_QUEUE_SIZE )
{
{
@ -101,12 +106,13 @@ namespace ZeroLevel.Network
}
}
var sendInfo = _sendinfo_pool . Allocate ( ) ;
var sendInfo = _sendinfo_pool . Allocate ( ) ;
sendInfo . isRequest = true ;
sendInfo . isRequest = true ;
sendInfo . data = NetworkPacketFactory. Reqeust ( MessageSerializer . Serialize ( frame ) , out int id ) ;
sendInfo . data = data ;
sendInfo . identity = id ;
sendInfo . identity = id ;
_requests . RegisterForFrame ( id , callback , fail ) ;
_requests . RegisterForFrame ( id , callback , fail ) ;
_send_queue . Add ( sendInfo ) ;
_send_queue . Add ( sendInfo ) ;
frame . Release ( ) ;
}
}
Dbg . Timestamp ( ( int ) DbgNetworkEvents . ClientCompletePushRequest , id . ToString ( ) ) ;
}
}
public void ForceConnect ( )
public void ForceConnect ( )
@ -117,7 +123,10 @@ namespace ZeroLevel.Network
public void Send ( Frame frame )
public void Send ( Frame frame )
{
{
if ( frame = = null ) throw new ArgumentNullException ( nameof ( frame ) ) ;
if ( frame = = null ) throw new ArgumentNullException ( nameof ( frame ) ) ;
if ( frame ! = null & & ! _send_queue . IsAddingCompleted )
var data = NetworkPacketFactory . Message ( MessageSerializer . Serialize ( frame ) ) ;
frame . Release ( ) ;
if ( ! _send_queue . IsAddingCompleted )
{
{
while ( _send_queue . Count > = MAX_SEND_QUEUE_SIZE )
while ( _send_queue . Count > = MAX_SEND_QUEUE_SIZE )
{
{
@ -126,15 +135,15 @@ namespace ZeroLevel.Network
var info = _sendinfo_pool . Allocate ( ) ;
var info = _sendinfo_pool . Allocate ( ) ;
info . isRequest = false ;
info . isRequest = false ;
info . identity = 0 ;
info . identity = 0 ;
info . data = NetworkPacketFactory. Message ( MessageSerializer . Serialize ( frame ) ) ;
info . data = data ;
_send_queue . Add ( info ) ;
_send_queue . Add ( info ) ;
frame . Release ( ) ;
}
}
}
}
public void Response ( byte [ ] data , int identity )
public void Response ( byte [ ] data , int identity )
{
{
if ( data = = null ) throw new ArgumentNullException ( nameof ( data ) ) ;
if ( data = = null ) throw new ArgumentNullException ( nameof ( data ) ) ;
Dbg . Timestamp ( ( int ) DbgNetworkEvents . ClientStartSendResponse , identity . ToString ( ) ) ;
if ( ! _send_queue . IsAddingCompleted )
if ( ! _send_queue . IsAddingCompleted )
{
{
while ( _send_queue . Count > = MAX_SEND_QUEUE_SIZE )
while ( _send_queue . Count > = MAX_SEND_QUEUE_SIZE )
@ -147,6 +156,7 @@ namespace ZeroLevel.Network
info . data = NetworkPacketFactory . Response ( data , identity ) ;
info . data = NetworkPacketFactory . Response ( data , identity ) ;
_send_queue . Add ( info ) ;
_send_queue . Add ( info ) ;
}
}
Dbg . Timestamp ( ( int ) DbgNetworkEvents . ClientCompleteSendResponse , identity . ToString ( ) ) ;
}
}
public void UseKeepAlive ( TimeSpan period )
public void UseKeepAlive ( TimeSpan period )
@ -196,14 +206,23 @@ namespace ZeroLevel.Network
Router ? . HandleMessage ( MessageSerializer . Deserialize < Frame > ( frame . data ) , this ) ;
Router ? . HandleMessage ( MessageSerializer . Deserialize < Frame > ( frame . data ) , this ) ;
break ;
break ;
case FrameType . Request :
case FrameType . Request :
var response = Router ? . HandleRequest ( MessageSerializer . Deserialize < Frame > ( frame . data ) , this ) ;
{
Dbg . Timestamp ( ( int ) DbgNetworkEvents . ClientStartHandleRequest , frame . identity . ToString ( ) ) ;
Router ? . HandleRequest ( MessageSerializer . Deserialize < Frame > ( frame . data ) , this , response = >
{
if ( response ! = null )
if ( response ! = null )
{
{
this . Response ( response , frame . identity ) ;
this . Response ( response , frame . identity ) ;
}
}
Dbg . Timestamp ( ( int ) DbgNetworkEvents . ClientCompleteHandleRequest , frame . identity . ToString ( ) ) ;
} ) ;
}
break ;
break ;
case FrameType . Response :
case FrameType . Response :
{
Dbg . Timestamp ( ( int ) DbgNetworkEvents . ClientGotResponse , frame . identity . ToString ( ) ) ;
_requests . Success ( frame . identity , frame . data ) ;
_requests . Success ( frame . identity , frame . data ) ;
}
break ;
break ;
}
}
}
}
@ -247,6 +266,8 @@ namespace ZeroLevel.Network
}
}
if ( _clientSocket ! = null )
if ( _clientSocket ! = null )
{
{
Dbg . Timestamp ( ( int ) DbgNetworkEvents . ClientLostConnection , $"{(_clientSocket.RemoteEndPoint as IPEndPoint).Address}:{(_clientSocket.RemoteEndPoint as IPEndPoint).Port}" ) ;
try
try
{
{
_stream ? . Close ( ) ;
_stream ? . Close ( ) ;
@ -351,12 +372,8 @@ namespace ZeroLevel.Network
Thread . Sleep ( 1 ) ;
Thread . Sleep ( 1 ) ;
}
}
EnsureConnection ( ) ;
EnsureConnection ( ) ;
if ( Status = = SocketClientStatus . Working
| | Status = = SocketClientStatus . Initialized )
{
_stream . BeginRead ( _buffer , 0 , DEFAULT_RECEIVE_BUFFER_SIZE , ReceiveAsyncCallback , null ) ;
_stream . BeginRead ( _buffer , 0 , DEFAULT_RECEIVE_BUFFER_SIZE , ReceiveAsyncCallback , null ) ;
}
}
}
catch ( ObjectDisposedException )
catch ( ObjectDisposedException )
{
{
/// Nothing
/// Nothing
@ -422,10 +439,17 @@ namespace ZeroLevel.Network
{
{
if ( frame . isRequest )
if ( frame . isRequest )
{
{
Dbg . Timestamp ( ( int ) DbgNetworkEvents . ClientStartSendRequest , frame . identity . ToString ( ) ) ;
_requests . StartSend ( frame . identity ) ;
_requests . StartSend ( frame . identity ) ;
}
}
_stream . Write ( frame . data , 0 , frame . data . Length ) ;
_stream . Write ( frame . data , 0 , frame . data . Length ) ;
_last_rw_time = DateTime . UtcNow . Ticks ;
_last_rw_time = DateTime . UtcNow . Ticks ;
if ( frame . isRequest )
{
Dbg . Timestamp ( ( int ) DbgNetworkEvents . ClientCompleteSendRequest , frame . identity . ToString ( ) ) ;
}
}
}
catch ( Exception ex )
catch ( Exception ex )
{
{