Esempio n. 1
0
 private void HandlePackage(PackageIn pkg)
 {
     try
     {
         EventManager.Dispatch(this, SocketEvent.SOCKET_DATA, pkg);
     }
     catch (Exception error)
     {
         Console.WriteLine("handlePackage: " + error.StackTrace);
     }
 }
Esempio n. 2
0
        private void ReadPackage()
        {
            int dataAvailable = _writeOffset - _readOffset;

            if (_pkgIn != null)
            {
                if (_pkgIn.size <= _pkgIn.inSize + dataAvailable)
                {
                    int lack = _pkgIn.size - _pkgIn.inSize;
                    _pkgIn.Load(_readBuffer, _readOffset, lack);
                    _readOffset  += lack;
                    dataAvailable = _writeOffset - _readOffset;
                    HandlePackage(_pkgIn);
                    _pkgIn = null;
                }
                else
                {
                    _pkgIn.Load(_readBuffer, _readOffset, dataAvailable);
                    _readOffset  += dataAvailable;
                    dataAvailable = 0;
                    HandlePackage(_pkgIn);
                }
            }
            while (dataAvailable >= PackageIn.HEADER_SIZE)
            {
                int len = 0;
                for (int i = 0; i < 4; i++)
                {
                    _pkgLength[i] = _readBuffer[_readOffset + i];
                }
                if (BitConverter.IsLittleEndian)
                {
                    len = BitConverter.ToInt32(EdianConvert.GetConvertEdian(_pkgLength), 0);
                }
                else
                {
                    len = BitConverter.ToInt32(_pkgLength, 0);
                }

                /*
                 * dataAvailable = _writeOffset - _readOffset;
                 * if (dataAvailable < len || len == 0)
                 * {
                 *  break;
                 * }
                 */
                _pkgIn = new PackageIn();
                if (len > dataAvailable)
                {
                    _pkgIn.Load(_readBuffer, _readOffset, dataAvailable);
                    _readOffset += dataAvailable;
                }
                else
                {
                    _pkgIn.Load(_readBuffer, _readOffset, len);
                    _readOffset += len;
                }
                dataAvailable = _writeOffset - _readOffset;
                HandlePackage(_pkgIn);
                if (_pkgIn.size <= _pkgIn.inSize)
                {
                    _pkgIn = null;
                }
            }

            if (dataAvailable > 0)
            {
                lock (_readBuffer)
                {
                    _BlockCopy(_readBuffer, _readOffset, _readBuffer, 0, dataAvailable);
                }
            }
            _readOffset  = 0;
            _writeOffset = dataAvailable;
        }