@@ -147,7 +147,7 @@ object IEnumerator.Current
147147#if DEBUG
148148 private struct IncomingData
149149 {
150- public byte [ ] Data ;
150+ public NetPacket Data ;
151151 public IPEndPoint EndPoint ;
152152 public DateTime TimeWhenGet ;
153153 }
@@ -720,7 +720,7 @@ private void ProcessDelayedPackets()
720720 var incomingData = _pingSimulationList [ i ] ;
721721 if ( incomingData . TimeWhenGet <= time )
722722 {
723- DataReceived ( incomingData . Data , incomingData . Data . Length , incomingData . EndPoint ) ;
723+ DataReceived ( incomingData . Data , incomingData . EndPoint ) ;
724724 _pingSimulationList . RemoveAt ( i ) ;
725725 i -- ;
726726 }
@@ -789,7 +789,7 @@ public void ManualReceive()
789789 ProcessDelayedPackets ( ) ;
790790 }
791791
792- internal void OnMessageReceived ( byte [ ] data , int length , SocketError errorCode , IPEndPoint remoteEndPoint )
792+ internal void OnMessageReceived ( NetPacket packet , SocketError errorCode , IPEndPoint remoteEndPoint )
793793 {
794794 if ( errorCode != 0 )
795795 {
@@ -808,14 +808,11 @@ internal void OnMessageReceived(byte[] data, int length, SocketError errorCode,
808808 int latency = _randomGenerator . Next ( SimulationMinLatency , SimulationMaxLatency ) ;
809809 if ( latency > MinLatencyThreshold )
810810 {
811- byte [ ] holdedData = new byte [ length ] ;
812- Buffer . BlockCopy ( data , 0 , holdedData , 0 , length ) ;
813-
814811 lock ( _pingSimulationList )
815812 {
816813 _pingSimulationList . Add ( new IncomingData
817814 {
818- Data = holdedData ,
815+ Data = packet ,
819816 EndPoint = remoteEndPoint ,
820817 TimeWhenGet = DateTime . UtcNow . AddMilliseconds ( latency )
821818 } ) ;
@@ -828,7 +825,7 @@ internal void OnMessageReceived(byte[] data, int length, SocketError errorCode,
828825 try
829826 {
830827 //ProcessEvents
831- DataReceived ( data , length , remoteEndPoint ) ;
828+ DataReceived ( packet , remoteEndPoint ) ;
832829 }
833830 catch ( Exception e )
834831 {
@@ -963,27 +960,27 @@ private void ProcessConnectRequest(
963960 CreateEvent ( NetEvent . EType . ConnectionRequest , connectionRequest : req ) ;
964961 }
965962
966- private void DataReceived ( byte [ ] reusableBuffer , int count , IPEndPoint remoteEndPoint )
963+ private void DataReceived ( NetPacket packet , IPEndPoint remoteEndPoint )
967964 {
968965 if ( EnableStatistics )
969966 {
970967 Statistics . IncrementPacketsReceived ( ) ;
971- Statistics . AddBytesReceived ( count ) ;
968+ Statistics . AddBytesReceived ( packet . Size ) ;
972969 }
973970
974971 if ( _ntpRequests . Count > 0 )
975972 {
976973 NtpRequest request ;
977974 if ( _ntpRequests . TryGetValue ( remoteEndPoint , out request ) )
978975 {
979- if ( count < 48 )
976+ if ( packet . Size < 48 )
980977 {
981- NetDebug . Write ( NetLogLevel . Trace , "NTP response too short: {}" , count ) ;
978+ NetDebug . Write ( NetLogLevel . Trace , "NTP response too short: {}" , packet . Size ) ;
982979 return ;
983980 }
984981
985- byte [ ] copiedData = new byte [ count ] ;
986- Buffer . BlockCopy ( reusableBuffer , 0 , copiedData , 0 , count ) ;
982+ byte [ ] copiedData = new byte [ packet . Size ] ;
983+ Buffer . BlockCopy ( packet . RawData , 0 , copiedData , 0 , packet . Size ) ;
987984 NtpPacket ntpPacket = NtpPacket . FromServerResponse ( copiedData , DateTime . UtcNow ) ;
988985 try
989986 {
@@ -1005,24 +1002,18 @@ private void DataReceived(byte[] reusableBuffer, int count, IPEndPoint remoteEnd
10051002 }
10061003 }
10071004
1008- int start = 0 ;
10091005 if ( _extraPacketLayer != null )
10101006 {
1011- _extraPacketLayer . ProcessInboundPacket ( remoteEndPoint , ref reusableBuffer , ref start , ref count ) ;
1012- if ( count == 0 )
1007+ int start = 0 ;
1008+ _extraPacketLayer . ProcessInboundPacket ( remoteEndPoint , ref packet . RawData , ref start , ref packet . Size ) ;
1009+ if ( packet . Size == 0 )
10131010 return ;
10141011 }
10151012
1016- //empty packet
1017- if ( reusableBuffer [ start ] == ( byte ) PacketProperty . Empty )
1018- return ;
1019-
1020- //Try read packet
1021- NetPacket packet = NetPacketPool . GetPacket ( count ) ;
1022- if ( ! packet . FromBytes ( reusableBuffer , start , count ) )
1013+ if ( ! packet . Verify ( ) )
10231014 {
1024- NetPacketPool . Recycle ( packet ) ;
10251015 NetDebug . WriteError ( "[NM] DataReceived: bad!" ) ;
1016+ NetPacketPool . Recycle ( packet ) ;
10261017 return ;
10271018 }
10281019
0 commit comments