Ein Projekt das es ermöglicht Beerpong über das Internet von zwei unabhängigen positionen aus zu spielen. Entstehung im Rahmen einer Praktikumsaufgabe im Fach Interaktion.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BufferPool.js 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*!
  2. * ws: a node.js websocket client
  3. * Copyright(c) 2011 Einar Otto Stangvik <einaros@gmail.com>
  4. * MIT Licensed
  5. */
  6. var util = require('util');
  7. function BufferPool(initialSize, growStrategy, shrinkStrategy) {
  8. if (typeof initialSize === 'function') {
  9. shrinkStrategy = growStrategy;
  10. growStrategy = initialSize;
  11. initialSize = 0;
  12. }
  13. else if (typeof initialSize === 'undefined') {
  14. initialSize = 0;
  15. }
  16. this._growStrategy = (growStrategy || function(db, size) {
  17. return db.used + size;
  18. }).bind(null, this);
  19. this._shrinkStrategy = (shrinkStrategy || function(db) {
  20. return initialSize;
  21. }).bind(null, this);
  22. this._buffer = initialSize ? new Buffer(initialSize) : null;
  23. this._offset = 0;
  24. this._used = 0;
  25. this._changeFactor = 0;
  26. this.__defineGetter__('size', function(){
  27. return this._buffer == null ? 0 : this._buffer.length;
  28. });
  29. this.__defineGetter__('used', function(){
  30. return this._used;
  31. });
  32. }
  33. BufferPool.prototype.get = function(length) {
  34. if (this._buffer == null || this._offset + length > this._buffer.length) {
  35. var newBuf = new Buffer(this._growStrategy(length));
  36. this._buffer = newBuf;
  37. this._offset = 0;
  38. }
  39. this._used += length;
  40. var buf = this._buffer.slice(this._offset, this._offset + length);
  41. this._offset += length;
  42. return buf;
  43. }
  44. BufferPool.prototype.reset = function(forceNewBuffer) {
  45. var len = this._shrinkStrategy();
  46. if (len < this.size) this._changeFactor -= 1;
  47. if (forceNewBuffer || this._changeFactor < -2) {
  48. this._changeFactor = 0;
  49. this._buffer = len ? new Buffer(len) : null;
  50. }
  51. this._offset = 0;
  52. this._used = 0;
  53. }
  54. module.exports = BufferPool;