Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- LastReceive = Extensions.Time32.Now;
- Packet Stream = PacketRecycle.Take();
- Stream.Seek(0);
- fixed (byte* ptr = ReceiveBuff.buffer)
- {
- Stream.memcpy(Stream.stream, ptr, Length);
- if (Length < ReceiveBuff.Length())
- {
- fixed (void* next_buffer = &ReceiveBuff.buffer[Length])
- {
- Stream.memcpy(ptr, next_buffer, ReceiveBuff.Length() - Length);
- }
- }
- Stream.Size = Length;
- ReceiveBuff.DelLength(Length);
- }
- Stream.SeekForward(2);
- if (OnReceiveHandler != null)
- OnReceiveHandler.Invoke(this, Stream);
- }
- catch (Exception e) { Console.WriteLine(e.ToString()); }
- }
- return false;
- }
- public bool Receive(int available)
- {
- if (Alive)
- {
- SocketError Socket_Error = SocketError.IsConnected;
- try
- {
- int length = ReceiveBuff.MaxLength() - ReceiveBuff.Length();
- int receive_size = 0;
- if (available > length)
- receive_size = length;
- else
- receive_size = available;
- if (receive_size == 0)
- {
- return false;
- }
- int ret = Connection.Receive(ReceiveBuff.buffer, ReceiveBuff.Length(), receive_size, SocketFlags.None, out Socket_Error);
- if (ret > 0)
- {
- if (Crypto != null)
- {
- fixed (byte* ptr = ReceiveBuff.buffer)
- Crypto.Decrypt(ReceiveBuff.buffer, ReceiveBuff.Length(), ptr, ReceiveBuff.Length(), ret);
- }
- ReceiveBuff.AddLength(ret);
- return true;
- }
- else if (ret == 0)//<= 0 || Socket_Error == SocketError.ConnectionAborted)
- {
- Disconnect();
- return false;
- }
- else//ret < 0
- {
- if (Socket_Error != SocketError.WouldBlock)
- Disconnect();
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e.ToString());
- Console.WriteLine(Socket_Error.ToString());
- }
- }
- return false;
- }
- public int nPutBytes = 0;
- public byte[] packet = null;
- public object SynRoot = new object();
- public void Disconnect()
- {
- lock (SynRoot)
- {
- if (Alive)
- {
- if (Server != null)
- Server.Clients.Remove(this);
- Alive = false;
- if (TimerSubscriptions != null)
- {
- for (int i = 0; i < TimerSubscriptions.Length; i++)
- if (TimerSubscriptions[i] != null)
- TimerSubscriptions[i].Dispose();
- }
- try
- {
- WindowsAPI.ws2_32.shutdown(Connection.Handle, WindowsAPI.ws2_32.ShutDownFlags.SD_BOTH);
- WindowsAPI.ws2_32.closesocket(Connection.Handle);
- Connection.Dispose();
- GC.SuppressFinalize(Connection);
- }
- catch (Exception e)
- {
- MyConsole.SaveException(e);
- }
- finally
- {
- if (OnDisconnect != null)
- OnDisconnect.Invoke(this);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement