tcp_socket_impl.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "tcp_socket_impl.h"
  2. using namespace NAsio;
  3. TSocketOperation::TSocketOperation(TTcpSocket::TImpl& s, TPollType pt, TInstant deadline)
  4. : TFdOperation(s.Fd(), pt, deadline)
  5. , S_(s)
  6. {
  7. }
  8. bool TOperationWrite::Execute(int errorCode) {
  9. if (errorCode) {
  10. H_(errorCode, Written_, *this);
  11. return true; //op. completed
  12. }
  13. TErrorCode ec;
  14. TContIOVector& iov = *Buffs_->GetIOvec();
  15. size_t n = S_.WriteSome(iov, ec);
  16. if (ec && ec.Value() != EAGAIN && ec.Value() != EWOULDBLOCK) {
  17. H_(ec, Written_ + n, *this);
  18. return true;
  19. }
  20. if (n) {
  21. Written_ += n;
  22. iov.Proceed(n);
  23. if (!iov.Bytes()) {
  24. H_(ec, Written_, *this);
  25. return true; //op. completed
  26. }
  27. }
  28. return false; //operation not compleled
  29. }
  30. bool TOperationWriteVector::Execute(int errorCode) {
  31. if (errorCode) {
  32. H_(errorCode, Written_, *this);
  33. return true; //op. completed
  34. }
  35. TErrorCode ec;
  36. size_t n = S_.WriteSome(V_, ec);
  37. if (ec && ec.Value() != EAGAIN && ec.Value() != EWOULDBLOCK) {
  38. H_(ec, Written_ + n, *this);
  39. return true;
  40. }
  41. if (n) {
  42. Written_ += n;
  43. V_.Proceed(n);
  44. if (!V_.Bytes()) {
  45. H_(ec, Written_, *this);
  46. return true; //op. completed
  47. }
  48. }
  49. return false; //operation not compleled
  50. }
  51. bool TOperationReadSome::Execute(int errorCode) {
  52. if (errorCode) {
  53. H_(errorCode, 0, *this);
  54. return true; //op. completed
  55. }
  56. TErrorCode ec;
  57. H_(ec, S_.ReadSome(Buff_, Size_, ec), *this);
  58. return true;
  59. }
  60. bool TOperationRead::Execute(int errorCode) {
  61. if (errorCode) {
  62. H_(errorCode, Read_, *this);
  63. return true; //op. completed
  64. }
  65. TErrorCode ec;
  66. size_t n = S_.ReadSome(Buff_, Size_, ec);
  67. Read_ += n;
  68. if (ec && ec.Value() != EAGAIN && ec.Value() != EWOULDBLOCK) {
  69. H_(ec, Read_, *this);
  70. return true; //op. completed
  71. }
  72. if (n) {
  73. Size_ -= n;
  74. if (!Size_) {
  75. H_(ec, Read_, *this);
  76. return true;
  77. }
  78. Buff_ += n;
  79. } else if (!ec) { // EOF while read not all
  80. H_(ec, Read_, *this);
  81. return true;
  82. }
  83. return false;
  84. }