/// <summary> /// Validates buffer capacity before writing into it. /// </summary> /// <param name="nextValueLength">length of next bytes sequence to write into buffer.</param> private void ValidateBufferSize(int nextValueLength) { if ((_mOffset + nextValueLength) > _mBuffer.Length) { L2Buffer.Extend(ref _mBuffer, nextValueLength + MDefaultOverflowValue); } }
/// <summary> /// Receive <see cref="AsyncCallback"/> method. /// </summary> /// <exception cref="InvalidOperationException" /> protected override unsafe void ReceiveCallback(IAsyncResult ar) { try { MReceivedLength += MSocket.EndReceive(ar); fixed(byte *buf = MReceiveBuffer) { if (!MHeaderReceived) //get packet capacity { L2Buffer.Extend(ref MReceiveBuffer, 0, *(int *)buf - sizeof(int)); MReceivedLength = 0; MHeaderReceived = true; } if (MReceivedLength == MReceiveBuffer.Length) // all data received { Handle(new Packet(2, MReceiveBuffer)); MReceivedLength = 0; MReceiveBuffer = MDefaultBuffer; MHeaderReceived = false; MSocket.BeginReceive(MReceiveBuffer, 0, 4, 0, ReceiveCallback, null); } else { if (MReceivedLength < MReceiveBuffer.Length) // not all data received { MSocket.BeginReceive(MReceiveBuffer, MReceivedLength, MReceiveBuffer.Length - MReceivedLength, 0, MReceiveCallback, null); } else { throw new InvalidOperationException(); } } } } catch (SocketException se) { Log.Info(string.Format("{0} \r\nError code: {1}", se.ToString(), se.ErrorCode)); CloseConnection(); OnDisconnected?.Invoke(se.ErrorCode, this, ConnectionId); } catch (Exception e) { Log.Error(e); CloseConnection(); OnDisconnected?.Invoke(-1, this, ConnectionId); } }
/// <summary> /// Receive method. /// </summary> //72-2min protected override unsafe void ReceiveCallback(IAsyncResult ar) { try { m_ReceivedLength += m_Socket.EndReceive(ar); //Logger.WriteLine(Source.Debug, "m_ReceivedLength == {0}", m_ReceivedLength); if (m_ReceivedLength == 0) { BeginReceive(); return; } fixed(byte *buf = m_ReceiveBuffer) { if (!m_HeaderReceived) //get packet capacity { L2Buffer.Extend(ref m_ReceiveBuffer, 0, *(( short * )(buf)) - sizeof(short)); m_ReceivedLength = 0; m_HeaderReceived = true; } if (m_ReceivedLength == m_ReceiveBuffer.Length) // all data received { m_Crypt.Decrypt(ref m_ReceiveBuffer, 0, m_ReceiveBuffer.Length); Handle(new Packet(1, m_ReceiveBuffer)); m_ReceivedLength = 0; m_ReceiveBuffer = m_DefaultBuffer; m_HeaderReceived = false; m_Socket.BeginReceive(m_ReceiveBuffer, 0, 0, 0, ReceiveCallback, null); } else if (m_ReceivedLength < m_ReceiveBuffer.Length) // not all data received { m_Socket.BeginReceive(m_ReceiveBuffer, m_ReceivedLength, m_ReceiveBuffer.Length - m_ReceivedLength, 0, m_ReceiveCallback, null); } else { throw new InvalidOperationException(); } } } catch (Exception e) { //if ( e is NullReferenceException ) // user closed connection //{ // UserConnectionsListener.RemoveFromActiveConnections(this); // return; //} Logger.Exception(e); } }
/// <summary> /// Resizes <see cref="Packet"/> buffer to it's actual capacity and appends buffer length to the beginning of <see cref="Packet"/> buffer. /// </summary> /// <param name="headerSize"><see cref="Packet"/> header (opcodes) capacity.</param> public unsafe void Prepare(int headerSize) { _mOffset += headerSize; L2Buffer.Extend(ref _mBuffer, headerSize, _mOffset); fixed(byte *buf = _mBuffer) { if (headerSize == sizeof(short)) { *(short *)buf = (short)_mOffset; } else { *(int *)buf = _mOffset; } } }
/// <summary> /// Session start method override, instead usual ReceiveCallback. /// </summary> private unsafe void SessionReceiveCallback(IAsyncResult ar) { try { m_ReceivedLength += m_Socket.EndReceive(ar); fixed(byte *buf = m_ReceiveBuffer) { //Logger.WriteLine(Source.Debug, "Recieve :\r\n{0}", L2Buffer.ToString(m_ReceiveBuffer)); if (!m_HeaderReceived) //get packet capacity { L2Buffer.Extend(ref m_ReceiveBuffer, 0, *(( short * )(buf)) - sizeof(short)); m_ReceivedLength = 0; m_HeaderReceived = true; } if (m_ReceivedLength == m_ReceiveBuffer.Length) // all data received { Handle(new Packet(1, m_ReceiveBuffer)); m_ReceivedLength = 0; m_ReceiveBuffer = m_DefaultBuffer; m_HeaderReceived = false; m_Socket.BeginReceive(m_ReceiveBuffer, 0, 2, 0, ReceiveCallback, null); } else if (m_ReceivedLength < m_ReceiveBuffer.Length) // not all data received { m_Socket.BeginReceive(m_ReceiveBuffer, m_ReceivedLength, m_ReceiveBuffer.Length - m_ReceivedLength, 0, SessionReceiveCallback, null); } else { throw new InvalidOperationException(); } } } catch (Exception e) { Logger.Exception(e); } }
/// <summary> /// Receive <see cref="AsyncCallback"/> method. /// </summary> /// <exception cref="InvalidOperationException" /> protected override unsafe void ReceiveCallback(IAsyncResult ar) { try { m_ReceivedLength += m_Socket.EndReceive(ar); fixed(byte *buf = m_ReceiveBuffer) { if (!m_HeaderReceived) //get packet capacity { L2Buffer.Extend(ref m_ReceiveBuffer, 0, *(( int * )(buf)) - sizeof(int)); m_ReceivedLength = 0; m_HeaderReceived = true; } if (m_ReceivedLength == m_ReceiveBuffer.Length) // all data received { Handle(new Packet(2, m_ReceiveBuffer)); m_ReceivedLength = 0; m_ReceiveBuffer = m_DefaultBuffer; m_HeaderReceived = false; m_Socket.BeginReceive(m_ReceiveBuffer, 0, 4, 0, ReceiveCallback, null); } else if (m_ReceivedLength < m_ReceiveBuffer.Length) // not all data received { m_Socket.BeginReceive(m_ReceiveBuffer, m_ReceivedLength, m_ReceiveBuffer.Length - m_ReceivedLength, 0, m_ReceiveCallback, null); } else { throw new InvalidOperationException(); } } } catch (SocketException se) { if (OnDisconnected != null) { OnDisconnected(se.ErrorCode, this, ConnectionID); } else { Logger.WriteLine(Source.InnerNetwork, "{0} \r\nError code: {1}", se.ToString(), se.ErrorCode); CloseConnection(); } } catch (Exception e) { Logger.Exception(e); if (OnDisconnected != null) { OnDisconnected(-1, this, ConnectionID); } else { CloseConnection(); } } }