+ rv = ServerInstance->SE->Send(this, front.data(), itemlen, 0);
+ if (rv == 0)
+ {
+ SetError("Connection closed");
+ return;
+ }
+ else if (rv < 0)
+ {
+ if (errno == EINTR || SocketEngine::IgnoreError())
+ ServerInstance->SE->ChangeEventMask(this, FD_WANT_FAST_WRITE | FD_WRITE_WILL_BLOCK);
+ else
+ SetError(SocketEngine::LastError());
+ return;
+ }
+ else if (rv < itemlen)
+ {
+ ServerInstance->SE->ChangeEventMask(this, FD_WANT_FAST_WRITE | FD_WRITE_WILL_BLOCK);
+ front = front.substr(rv);
+ sendq_len -= rv;
+ return;
+ }
+ else
+ {
+ sendq_len -= itemlen;
+ sendq.pop_front();
+ if (sendq.empty())
+ ServerInstance->SE->ChangeEventMask(this, FD_WANT_EDGE_WRITE);
+ }
+ }
+#endif
+ }
+ }
+ catch (CoreException& modexcept)
+ {
+ ServerInstance->Logs->Log("SOCKET", DEBUG,"%s threw an exception: %s",
+ modexcept.GetSource(), modexcept.GetReason());
+ }
+ }
+#ifndef DISABLE_WRITEV
+ else
+ {
+ // don't even try if we are known to be blocking
+ if (GetEventMask() & FD_WRITE_WILL_BLOCK)
+ return;
+ // start out optimistic - we won't need to write any more
+ int eventChange = FD_WANT_EDGE_WRITE;
+ while (error.empty() && sendq_len && eventChange == FD_WANT_EDGE_WRITE)
+ {
+ // Prepare a writev() call to write all buffers efficiently
+ int bufcount = sendq.size();
+
+ // cap the number of buffers at MYIOV_MAX
+ if (bufcount > MYIOV_MAX)
+ {
+ bufcount = MYIOV_MAX;
+ }
+
+ int rv_max = 0;
+ iovec* iovecs = new iovec[bufcount];
+ for(int i=0; i < bufcount; i++)
+ {
+ iovecs[i].iov_base = const_cast<char*>(sendq[i].data());
+ iovecs[i].iov_len = sendq[i].length();
+ rv_max += sendq[i].length();
+ }
+ int rv = writev(fd, iovecs, bufcount);
+ delete[] iovecs;
+
+ if (rv == (int)sendq_len)
+ {
+ // it's our lucky day, everything got written out. Fast cleanup.
+ // This won't ever happen if the number of buffers got capped.
+ sendq_len = 0;
+ sendq.clear();
+ }
+ else if (rv > 0)
+ {
+ // Partial write. Clean out strings from the sendq
+ if (rv < rv_max)
+ {
+ // it's going to block now
+ eventChange = FD_WANT_FAST_WRITE | FD_WRITE_WILL_BLOCK;
+ }
+ sendq_len -= rv;
+ while (rv > 0 && !sendq.empty())
+ {
+ std::string& front = sendq.front();
+ if (front.length() <= (size_t)rv)
+ {
+ // this string got fully written out
+ rv -= front.length();
+ sendq.pop_front();
+ }
+ else
+ {
+ // stopped in the middle of this string
+ front = front.substr(rv);
+ rv = 0;
+ }