Advertisement
Genral

حل لاج الوحش

Dec 26th, 2023
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. LastReceive = Extensions.Time32.Now;
  2. Packet Stream = PacketRecycle.Take();
  3.  
  4. Stream.Seek(0);
  5.  
  6. fixed (byte* ptr = ReceiveBuff.buffer)
  7. {
  8. Stream.memcpy(Stream.stream, ptr, Length);
  9. if (Length < ReceiveBuff.Length())
  10. {
  11. fixed (void* next_buffer = &ReceiveBuff.buffer[Length])
  12. {
  13. Stream.memcpy(ptr, next_buffer, ReceiveBuff.Length() - Length);
  14. }
  15. }
  16. Stream.Size = Length;
  17.  
  18. ReceiveBuff.DelLength(Length);
  19. }
  20.  
  21. Stream.SeekForward(2);
  22.  
  23. if (OnReceiveHandler != null)
  24. OnReceiveHandler.Invoke(this, Stream);
  25.  
  26. }
  27. catch (Exception e) { Console.WriteLine(e.ToString()); }
  28. }
  29. return false;
  30. }
  31.  
  32. public bool Receive(int available)
  33. {
  34. if (Alive)
  35. {
  36. SocketError Socket_Error = SocketError.IsConnected;
  37. try
  38. {
  39. int length = ReceiveBuff.MaxLength() - ReceiveBuff.Length();
  40.  
  41. int receive_size = 0;
  42. if (available > length)
  43. receive_size = length;
  44. else
  45. receive_size = available;
  46. if (receive_size == 0)
  47. {
  48. return false;
  49. }
  50. int ret = Connection.Receive(ReceiveBuff.buffer, ReceiveBuff.Length(), receive_size, SocketFlags.None, out Socket_Error);
  51.  
  52. if (ret > 0)
  53. {
  54.  
  55. if (Crypto != null)
  56. {
  57. fixed (byte* ptr = ReceiveBuff.buffer)
  58. Crypto.Decrypt(ReceiveBuff.buffer, ReceiveBuff.Length(), ptr, ReceiveBuff.Length(), ret);
  59. }
  60. ReceiveBuff.AddLength(ret);
  61.  
  62. return true;
  63. }
  64. else if (ret == 0)//<= 0 || Socket_Error == SocketError.ConnectionAborted)
  65. {
  66. Disconnect();
  67. return false;
  68. }
  69. else//ret < 0
  70. {
  71. if (Socket_Error != SocketError.WouldBlock)
  72. Disconnect();
  73. }
  74.  
  75.  
  76. }
  77. catch (Exception e)
  78. {
  79. Console.WriteLine(e.ToString());
  80. Console.WriteLine(Socket_Error.ToString());
  81.  
  82. }
  83. }
  84. return false;
  85. }
  86.  
  87.  
  88.  
  89. public int nPutBytes = 0;
  90. public byte[] packet = null;
  91.  
  92. public object SynRoot = new object();
  93. public void Disconnect()
  94. {
  95. lock (SynRoot)
  96. {
  97. if (Alive)
  98. {
  99.  
  100. if (Server != null)
  101. Server.Clients.Remove(this);
  102.  
  103. Alive = false;
  104. if (TimerSubscriptions != null)
  105. {
  106. for (int i = 0; i < TimerSubscriptions.Length; i++)
  107. if (TimerSubscriptions[i] != null)
  108. TimerSubscriptions[i].Dispose();
  109. }
  110. try
  111. {
  112.  
  113. WindowsAPI.ws2_32.shutdown(Connection.Handle, WindowsAPI.ws2_32.ShutDownFlags.SD_BOTH);
  114. WindowsAPI.ws2_32.closesocket(Connection.Handle);
  115. Connection.Dispose();
  116. GC.SuppressFinalize(Connection);
  117. }
  118. catch (Exception e)
  119. {
  120. MyConsole.SaveException(e);
  121. }
  122. finally
  123. {
  124. if (OnDisconnect != null)
  125. OnDisconnect.Invoke(this);
  126. }
  127. }
  128. }
  129. }
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement