| 858 | } |
| 859 | |
| 860 | void Socket::Send(const char* data, int offset, int length) |
| 861 | { |
| 862 | std::lock_guard<std::recursive_mutex> g(mutex); |
| 863 | activeSendTime = time(0); |
| 864 | |
| 865 | bool noPendingSend = OutputBuffer->buffer.empty(); |
| 866 | bool hasCodec = false; |
| 867 | if (OutputCodec.get()) |
| 868 | { |
| 869 | OutputCodec->update((int8_t*)data, offset, length); |
| 870 | OutputCodec->flush(); |
| 871 | data = OutputBuffer->buffer.data(); |
| 872 | offset = 0; |
| 873 | length = (int)OutputBuffer->buffer.size(); |
| 874 | hasCodec = true; |
| 875 | } |
| 876 | |
| 877 | if (noPendingSend) |
| 878 | { |
| 879 | // try send direct |
| 880 | int rc = ::send(socket, data + offset, length, 0); |
| 881 | if (rc == -1) |
| 882 | { |
| 883 | if (false == platform_ignore_error_for_send()) |
| 884 | { |
| 885 | std::exception senderr("send error"); |
| 886 | this->Close(&senderr); |
| 887 | return; |
| 888 | } |
| 889 | rc = 0; |
| 890 | } |
| 891 | |
| 892 | if (hasCodec) |
| 893 | { |
| 894 | OutputBuffer->buffer.erase(0, rc); |
| 895 | if (false == OutputBuffer->buffer.empty()) |
| 896 | Selector::Instance->Select(This, Selector::OpWrite, 0); |
| 897 | return; |
| 898 | } |
| 899 | if (rc >= length) |
| 900 | { |
| 901 | return; // all send and hasn't Codec |
| 902 | } |
| 903 | // part send |
| 904 | offset += rc; |
| 905 | length -= rc; |
| 906 | OutputBuffer->buffer.append(data + offset, length); |
| 907 | Selector::Instance->Select(This, Selector::OpWrite, 0); |
| 908 | return; |
| 909 | } |
| 910 | // in sending |
| 911 | if (false == hasCodec) // 如果有Codec,那么将要发送的数据已经被处理(update)到buffer中,不需要再次添加。 |
| 912 | OutputBuffer->buffer.append(data + offset, length); |
| 913 | } |
| 914 | |
| 915 | inline void AssignAddressBytes(struct addrinfo* ai, std::string& out) |
| 916 | { |