stream_peer.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*************************************************************************/
  2. /* stream_peer.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "stream_peer.h"
  30. Error StreamPeer::_put_data(const DVector<uint8_t>& p_data) {
  31. int len = p_data.size();
  32. if (len==0)
  33. return OK;
  34. DVector<uint8_t>::Read r = p_data.read();
  35. return put_data(&r[0],len);
  36. }
  37. Array StreamPeer::_put_partial_data(const DVector<uint8_t>& p_data) {
  38. Array ret;
  39. int len = p_data.size();
  40. if (len==0) {
  41. ret.push_back(OK);
  42. ret.push_back(0);
  43. return ret;
  44. }
  45. DVector<uint8_t>::Read r = p_data.read();
  46. int sent;
  47. Error err = put_partial_data(&r[0],len,sent);
  48. if (err!=OK) {
  49. sent=0;
  50. }
  51. ret.push_back(err);
  52. ret.push_back(sent);
  53. return ret;
  54. }
  55. Array StreamPeer::_get_data(int p_bytes) {
  56. Array ret;
  57. DVector<uint8_t> data;
  58. data.resize(p_bytes);
  59. if (data.size()!=p_bytes) {
  60. ret.push_back(ERR_OUT_OF_MEMORY);
  61. ret.push_back(DVector<uint8_t>());
  62. return ret;
  63. }
  64. DVector<uint8_t>::Write w = data.write();
  65. Error err = get_data(&w[0],p_bytes);
  66. w = DVector<uint8_t>::Write();
  67. ret.push_back(err);
  68. ret.push_back(data);
  69. return ret;
  70. }
  71. Array StreamPeer::_get_partial_data(int p_bytes) {
  72. Array ret;
  73. DVector<uint8_t> data;
  74. data.resize(p_bytes);
  75. if (data.size()!=p_bytes) {
  76. ret.push_back(ERR_OUT_OF_MEMORY);
  77. ret.push_back(DVector<uint8_t>());
  78. return ret;
  79. }
  80. DVector<uint8_t>::Write w = data.write();
  81. int received;
  82. Error err = get_partial_data(&w[0],p_bytes,received);
  83. w = DVector<uint8_t>::Write();
  84. if (err!=OK) {
  85. data.resize(0);
  86. } else if (received!=data.size()) {
  87. data.resize(received);
  88. }
  89. ret.push_back(err);
  90. ret.push_back(data);
  91. return ret;
  92. }
  93. void StreamPeer::_bind_methods() {
  94. ObjectTypeDB::bind_method(_MD("put_data","data"),&StreamPeer::_put_data);
  95. ObjectTypeDB::bind_method(_MD("put_partial_data","data"),&StreamPeer::_put_partial_data);
  96. ObjectTypeDB::bind_method(_MD("get_data","bytes"),&StreamPeer::_get_data);
  97. ObjectTypeDB::bind_method(_MD("get_partial_data","bytes"),&StreamPeer::_get_partial_data);
  98. }