Upstream tarball 9882
[amule.git] / src / EMSocket.cpp
blobfbc4376544d83b3b6d0b73341cdfe6dcf1dd0702
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2008 Merkur ( devs@emule-project.net / http://www.emule-project.net )
6 //
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
9 // respective authors.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "EMSocket.h" // Interface declarations.
29 #include <protocol/Protocols.h>
30 #include <protocol/ed2k/Constants.h>
32 #include "Packet.h" // Needed for CPacket
33 #include "amule.h"
34 #include "GetTickCount.h"
35 #include "UploadBandwidthThrottler.h"
36 #include "Logger.h"
37 #include "Preferences.h"
40 const uint32 MAX_SIZE = 2000000;
42 CEMSocket::CEMSocket(const CProxyData *ProxyData)
43 : CEncryptedStreamSocket(wxSOCKET_NOWAIT, ProxyData)
45 // If an interface has been specified,
46 // then we need to bind to it.
47 if (!thePrefs::GetAddress().IsEmpty()) {
48 amuleIPV4Address host;
50 // No need to warn here, in case of failure to
51 // assign the hostname. That is already done
52 // in amule.cpp when starting ...
53 if (host.Hostname(thePrefs::GetAddress())) {
54 SetLocal(host);
58 byConnected = ES_NOTCONNECTED;
59 m_uTimeOut = CONNECTION_TIMEOUT; // default timeout for ed2k sockets
61 // Download (pseudo) rate control
62 downloadLimit = 0;
63 downloadLimitEnable = false;
64 pendingOnReceive = false;
66 // Download partial header
67 pendingHeaderSize = 0;
69 // Download partial packet
70 pendingPacket = NULL;
71 pendingPacketSize = 0;
73 // Upload control
74 sendbuffer = NULL;
75 sendblen = 0;
76 sent = 0;
78 m_currentPacket_is_controlpacket = false;
79 m_currentPackageIsFromPartFile = false;
81 m_numberOfSentBytesCompleteFile = 0;
82 m_numberOfSentBytesPartFile = 0;
83 m_numberOfSentBytesControlPacket = 0;
85 lastCalledSend = ::GetTickCount();
86 lastSent = ::GetTickCount()-1000;
88 m_bAccelerateUpload = false;
90 m_actualPayloadSize = 0;
91 m_actualPayloadSizeSent = 0;
93 m_bBusy = false;
94 m_hasSent = false;
96 lastFinishedStandard = 0;
98 DoingDestroy = false;
102 CEMSocket::~CEMSocket()
104 // need to be locked here to know that the other methods
105 // won't be in the middle of things
107 wxMutexLocker lock(m_sendLocker);
108 byConnected = ES_DISCONNECTED;
111 // now that we know no other method will keep adding to the queue
112 // we can remove ourself from the queue
113 if (theApp->uploadBandwidthThrottler) {
114 theApp->uploadBandwidthThrottler->RemoveFromAllQueues(this);
117 ClearQueues();
119 SetNotify(0);
120 Notify(FALSE);
123 void CEMSocket::Destroy() {
124 if (!DoingDestroy) {
125 DoingDestroy = true;
126 wxSocketClient::Destroy();
131 void CEMSocket::ClearQueues()
133 wxMutexLocker lock(m_sendLocker);
135 DeleteContents(m_control_queue);
138 CStdPacketQueue::iterator it = m_standard_queue.begin();
139 for (; it != m_standard_queue.end(); ++it) {
140 delete it->packet;
142 m_standard_queue.clear();
145 // Download (pseudo) rate control
146 downloadLimit = 0;
147 downloadLimitEnable = false;
148 pendingOnReceive = false;
150 // Download partial header
151 pendingHeaderSize = 0;
153 // Download partial packet
154 delete pendingPacket;
155 pendingPacket = NULL;
156 pendingPacketSize = 0;
158 // Upload control
159 delete[] sendbuffer;
160 sendbuffer = NULL;
161 sendblen = 0;
162 sent = 0;
166 void CEMSocket::OnClose(int WXUNUSED(nErrorCode))
168 // need to be locked here to know that the other methods
169 // won't be in the middle of things
171 wxMutexLocker lock(m_sendLocker);
172 byConnected = ES_DISCONNECTED;
175 // now that we know no other method will keep adding to the queue
176 // we can remove ourself from the queue
177 theApp->uploadBandwidthThrottler->RemoveFromAllQueues(this);
179 ClearQueues();
183 void CEMSocket::OnReceive(int nErrorCode)
185 // the 2 meg size was taken from another place
186 static byte GlobalReadBuffer[MAX_SIZE];
188 if(nErrorCode) {
189 uint32 error = LastError();
190 if (error != wxSOCKET_WOULDBLOCK) {
191 OnError(nErrorCode);
192 return;
196 // Check current connection state
197 if (byConnected == ES_DISCONNECTED) {
198 return;
199 } else {
200 byConnected = ES_CONNECTED; // ES_DISCONNECTED, ES_NOTCONNECTED, ES_CONNECTED
203 // CPU load improvement
204 if(downloadLimitEnable == true && downloadLimit == 0){
205 pendingOnReceive = true;
206 return;
209 // Remark: an overflow can not occur here
210 uint32 readMax = sizeof(GlobalReadBuffer) - pendingHeaderSize;
211 if((downloadLimitEnable == true) && (readMax > downloadLimit)) {
212 readMax = downloadLimit;
216 // We attempt to read up to 2 megs at a time (minus whatever is in our internal read buffer)
217 uint32 ret;
220 wxMutexLocker lock(m_sendLocker);
221 ret = Read(GlobalReadBuffer + pendingHeaderSize, readMax);
222 if (Error() || (ret == 0)) {
223 if (LastError() == wxSOCKET_WOULDBLOCK) {
224 pendingOnReceive = true;
226 return;
231 // Bandwidth control
232 if(downloadLimitEnable == true){
233 // Update limit
234 downloadLimit -= GetRealReceivedBytes();
237 // CPU load improvement
238 // Detect if the socket's buffer is empty (or the size did match...)
239 pendingOnReceive = (ret == readMax);
241 // Copy back the partial header into the global read buffer for processing
242 if(pendingHeaderSize > 0) {
243 memcpy(GlobalReadBuffer, pendingHeader, pendingHeaderSize);
244 ret += pendingHeaderSize;
245 pendingHeaderSize = 0;
248 byte* rptr = GlobalReadBuffer; // floating index initialized with begin of buffer
249 const byte* rend = GlobalReadBuffer + ret; // end of buffer
251 // Loop, processing packets until we run out of them
252 while((rend - rptr >= PACKET_HEADER_SIZE) ||
253 ((pendingPacket != NULL) && (rend - rptr > 0 ))){
255 // Two possibilities here:
257 // 1. There is no pending incoming packet
258 // 2. There is already a partial pending incoming packet
260 // It's important to remember that emule exchange two kinds of packet
261 // - The control packet
262 // - The data packet for the transport of the block
264 // The biggest part of the traffic is done with the data packets.
265 // The default size of one block is 10240 bytes (or less if compressed), but the
266 // maximal size for one packet on the network is 1300 bytes. It's the reason
267 // why most of the Blocks are splitted before to be sent.
269 // Conclusion: When the download limit is disabled, this method can be at least
270 // called 8 times (10240/1300) by the lower layer before a splitted packet is
271 // rebuild and transferred to the above layer for processing.
273 // The purpose of this algorithm is to limit the amount of data exchanged between buffers
275 if(pendingPacket == NULL){
276 pendingPacket = new CPacket(rptr); // Create new packet container.
277 rptr += 6; // Only the header is initialized so far
279 // Bugfix We still need to check for a valid protocol
280 // Remark: the default eMule v0.26b had removed this test......
281 switch (pendingPacket->GetProtocol()){
282 case OP_EDONKEYPROT:
283 case OP_PACKEDPROT:
284 case OP_EMULEPROT:
285 case OP_ED2KV2HEADER:
286 case OP_ED2KV2PACKEDPROT:
287 break;
288 default:
289 delete pendingPacket;
290 pendingPacket = NULL;
291 OnError(ERR_WRONGHEADER);
292 return;
295 // Security: Check for buffer overflow (2MB)
296 if(pendingPacket->GetPacketSize() > sizeof(GlobalReadBuffer)) {
297 delete pendingPacket;
298 pendingPacket = NULL;
299 OnError(ERR_TOOBIG);
300 return;
303 // Init data buffer
304 pendingPacket->AllocDataBuffer();
305 pendingPacketSize = 0;
308 // Bytes ready to be copied into packet's internal buffer
309 wxASSERT(rptr <= rend);
310 uint32 toCopy = ((pendingPacket->GetPacketSize() - pendingPacketSize) < (uint32)(rend - rptr)) ?
311 (pendingPacket->GetPacketSize() - pendingPacketSize) : (uint32)(rend - rptr);
313 // Copy Bytes from Global buffer to packet's internal buffer
314 pendingPacket->CopyToDataBuffer(pendingPacketSize, rptr, toCopy);
315 pendingPacketSize += toCopy;
316 rptr += toCopy;
318 // Check if packet is complet
319 wxASSERT(pendingPacket->GetPacketSize() >= pendingPacketSize);
320 if(pendingPacket->GetPacketSize() == pendingPacketSize) {
321 // Process packet
322 bool bPacketResult = PacketReceived(pendingPacket);
323 delete pendingPacket;
324 pendingPacket = NULL;
325 pendingPacketSize = 0;
327 if (!bPacketResult) {
328 return;
333 // Finally, if there is any data left over, save it for next time
334 wxASSERT(rptr <= rend);
335 wxASSERT(rend - rptr < PACKET_HEADER_SIZE);
336 if(rptr != rend) {
337 // Keep the partial head
338 pendingHeaderSize = rend - rptr;
339 memcpy(pendingHeader, rptr, pendingHeaderSize);
344 void CEMSocket::SetDownloadLimit(uint32 limit)
346 downloadLimit = limit;
347 downloadLimitEnable = true;
349 // CPU load improvement
350 if(limit > 0 && pendingOnReceive == true){
351 OnReceive(0);
356 void CEMSocket::DisableDownloadLimit()
358 downloadLimitEnable = false;
360 // CPU load improvement
361 if (pendingOnReceive == true){
362 OnReceive(0);
368 * Queues up the packet to be sent. Another thread will actually send the packet.
370 * If the packet is not a control packet, and if the socket decides that its queue is
371 * full and forceAdd is false, then the socket is allowed to refuse to add the packet
372 * to its queue. It will then return false and it is up to the calling thread to try
373 * to call SendPacket for that packet again at a later time.
375 * @param packet address to the packet that should be added to the queue
377 * @param delpacket if true, the responsibility for deleting the packet after it has been sent
378 * has been transferred to this object. If false, don't delete the packet after it
379 * has been sent.
381 * @param controlpacket the packet is a controlpacket
383 * @param forceAdd this packet must be added to the queue, even if it is full. If this flag is true
384 * then the method can not refuse to add the packet, and therefore not return false.
386 * @return true if the packet was added to the queue, false otherwise
388 void CEMSocket::SendPacket(CPacket* packet, bool delpacket, bool controlpacket, uint32 actualPayloadSize)
390 //printf("* SendPacket called on socket %p\n", this);
391 wxMutexLocker lock(m_sendLocker);
393 if (byConnected == ES_DISCONNECTED) {
394 //printf("* Disconnected, drop packet\n");
395 if(delpacket) {
396 delete packet;
398 } else {
399 if (!delpacket){
400 packet = new CPacket(*packet);
403 if (controlpacket) {
404 //printf("* Adding a control packet\n");
405 m_control_queue.push_back(packet);
407 // queue up for controlpacket
408 theApp->uploadBandwidthThrottler->QueueForSendingControlPacket(this, HasSent());
409 } else {
410 //printf("* Adding a normal packet to the queue\n");
411 bool first = !((sendbuffer && !m_currentPacket_is_controlpacket) || !m_standard_queue.empty());
412 StandardPacketQueueEntry queueEntry = { actualPayloadSize, packet };
413 m_standard_queue.push_back(queueEntry);
415 // reset timeout for the first time
416 if (first) {
417 lastFinishedStandard = ::GetTickCount();
418 m_bAccelerateUpload = true; // Always accelerate first packet in a block
425 uint64 CEMSocket::GetSentBytesCompleteFileSinceLastCallAndReset()
427 wxMutexLocker lock( m_sendLocker );
429 uint64 sentBytes = m_numberOfSentBytesCompleteFile;
430 m_numberOfSentBytesCompleteFile = 0;
432 return sentBytes;
436 uint64 CEMSocket::GetSentBytesPartFileSinceLastCallAndReset()
438 wxMutexLocker lock( m_sendLocker );
440 uint64 sentBytes = m_numberOfSentBytesPartFile;
441 m_numberOfSentBytesPartFile = 0;
443 return sentBytes;
446 uint64 CEMSocket::GetSentBytesControlPacketSinceLastCallAndReset()
448 wxMutexLocker lock( m_sendLocker );
450 uint64 sentBytes = m_numberOfSentBytesControlPacket;
451 m_numberOfSentBytesControlPacket = 0;
453 return sentBytes;
456 uint64 CEMSocket::GetSentPayloadSinceLastCallAndReset()
458 wxMutexLocker lock( m_sendLocker );
460 uint64 sentBytes = m_actualPayloadSizeSent;
461 m_actualPayloadSizeSent = 0;
463 return sentBytes;
467 void CEMSocket::OnSend(int nErrorCode)
469 if (nErrorCode){
470 OnError(nErrorCode);
471 return;
474 CEncryptedStreamSocket::OnSend(0);
476 wxMutexLocker lock( m_sendLocker );
477 m_bBusy = false;
479 if (byConnected != ES_DISCONNECTED) {
480 byConnected = ES_CONNECTED;
482 if (m_currentPacket_is_controlpacket) {
483 // queue up for control packet
484 theApp->uploadBandwidthThrottler->QueueForSendingControlPacket(this, HasSent());
491 * Try to put queued up data on the socket.
493 * Control packets have higher priority, and will be sent first, if possible.
494 * Standard packets can be split up in several package containers. In that case
495 * all the parts of a split package must be sent in a row, without any control packet
496 * in between.
498 * @param maxNumberOfBytesToSend This is the maximum number of bytes that is allowed to be put on the socket
499 * this call. The actual number of sent bytes will be returned from the method.
501 * @param onlyAllowedToSendControlPacket This call we only try to put control packets on the sockets.
502 * If there's a standard packet "in the way", and we think that this socket
503 * is no longer an upload slot, then it is ok to send the standard packet to
504 * get it out of the way. But it is not allowed to pick a new standard packet
505 * from the queue during this call. Several split packets are counted as one
506 * standard packet though, so it is ok to finish them all off if necessary.
508 * @return the actual number of bytes that were put on the socket.
510 SocketSentBytes CEMSocket::Send(uint32 maxNumberOfBytesToSend, uint32 minFragSize, bool onlyAllowedToSendControlPacket)
512 wxMutexLocker lock(m_sendLocker);
514 //printf("* Attempt to send a packet on socket %p\n", this);
516 if (byConnected == ES_DISCONNECTED) {
517 //printf("* Disconnected socket %p\n", this);
518 SocketSentBytes returnVal = { false, 0, 0 };
519 return returnVal;
520 } else if (m_bBusy && onlyAllowedToSendControlPacket) {
521 //printf("* Busy socket %p\n", this);
522 SocketSentBytes returnVal = { true, 0, 0 };
523 return returnVal;
526 bool anErrorHasOccured = false;
527 uint32 sentStandardPacketBytesThisCall = 0;
528 uint32 sentControlPacketBytesThisCall = 0;
530 if(byConnected == ES_CONNECTED && IsEncryptionLayerReady() && !(m_bBusy && onlyAllowedToSendControlPacket)) {
532 //printf("* Internal attemptto send on %p\n", this);
534 if(minFragSize < 1) {
535 minFragSize = 1;
538 maxNumberOfBytesToSend = GetNextFragSize(maxNumberOfBytesToSend, minFragSize);
540 bool bWasLongTimeSinceSend = (::GetTickCount() - lastSent) > 1000;
542 lastCalledSend = ::GetTickCount();
545 while(sentStandardPacketBytesThisCall + sentControlPacketBytesThisCall < maxNumberOfBytesToSend && anErrorHasOccured == false && // don't send more than allowed. Also, there should have been no error in earlier loop
546 (!m_control_queue.empty() || !m_standard_queue.empty() || sendbuffer != NULL) && // there must exist something to send
547 (onlyAllowedToSendControlPacket == false || // this means we are allowed to send both types of packets, so proceed
548 (sentStandardPacketBytesThisCall + sentControlPacketBytesThisCall > 0 && (sentStandardPacketBytesThisCall + sentControlPacketBytesThisCall) % minFragSize != 0) ||
549 (sendbuffer == NULL && !m_control_queue.empty()) || // There's a control packet in queue, and we are not currently sending anything, so we will handle the control packet next
550 (sendbuffer != NULL && m_currentPacket_is_controlpacket == true) || // We are in the progress of sending a control packet. We are always allowed to send those
551 (sendbuffer != NULL && m_currentPacket_is_controlpacket == false && bWasLongTimeSinceSend && !m_control_queue.empty() && m_standard_queue.empty() && (sentStandardPacketBytesThisCall + sentControlPacketBytesThisCall) < minFragSize) // We have waited to long to clean the current packet (which may be a standard packet that is in the way). Proceed no matter what the value of onlyAllowedToSendControlPacket.
555 // If we are currently not in the progress of sending a packet, we will need to find the next one to send
556 if(sendbuffer == NULL) {
557 CPacket* curPacket = NULL;
558 if(!m_control_queue.empty()) {
559 // There's a control packet to send
560 m_currentPacket_is_controlpacket = true;
561 curPacket = m_control_queue.front();
562 m_control_queue.pop_front();
563 } else if(!m_standard_queue.empty() /*&& onlyAllowedToSendControlPacket == false*/) {
564 // There's a standard packet to send
565 m_currentPacket_is_controlpacket = false;
566 StandardPacketQueueEntry queueEntry = m_standard_queue.front();
567 m_standard_queue.pop_front();
568 curPacket = queueEntry.packet;
569 m_actualPayloadSize = queueEntry.actualPayloadSize;
571 // remember this for statistics purposes.
572 m_currentPackageIsFromPartFile = curPacket->IsFromPF();
573 } else {
574 // Just to be safe. Shouldn't happen?
575 // if we reach this point, then there's something wrong with the while condition above!
576 wxFAIL;
577 AddDebugLogLineM(true, logGeneral, wxT("EMSocket: Couldn't get a new packet! There's an error in the first while condition in EMSocket::Send()"));
579 SocketSentBytes returnVal = { true, sentStandardPacketBytesThisCall, sentControlPacketBytesThisCall };
580 return returnVal;
583 // We found a packet to send. Get the data to send from the
584 // package container and dispose of the container.
585 sendblen = curPacket->GetRealPacketSize();
586 sendbuffer = curPacket->DetachPacket();
587 sent = 0;
588 delete curPacket;
590 CryptPrepareSendData((byte*)sendbuffer, sendblen);
593 // At this point we've got a packet to send in sendbuffer. Try to send it. Loop until entire packet
594 // is sent, or until we reach maximum bytes to send for this call, or until we get an error.
595 // NOTE! If send would block (returns WOULDBLOCK), we will return from this method INSIDE this loop.
596 while (sent < sendblen &&
597 sentStandardPacketBytesThisCall + sentControlPacketBytesThisCall < maxNumberOfBytesToSend &&
599 onlyAllowedToSendControlPacket == false || // this means we are allowed to send both types of packets, so proceed
600 m_currentPacket_is_controlpacket ||
601 (bWasLongTimeSinceSend && (sentStandardPacketBytesThisCall + sentControlPacketBytesThisCall) < minFragSize) ||
602 (sentStandardPacketBytesThisCall + sentControlPacketBytesThisCall) % minFragSize != 0
603 ) &&
604 anErrorHasOccured == false) {
605 uint32 tosend = sendblen-sent;
606 if(!onlyAllowedToSendControlPacket || m_currentPacket_is_controlpacket) {
607 if (maxNumberOfBytesToSend >= sentStandardPacketBytesThisCall + sentControlPacketBytesThisCall && tosend > maxNumberOfBytesToSend-(sentStandardPacketBytesThisCall + sentControlPacketBytesThisCall))
608 tosend = maxNumberOfBytesToSend-(sentStandardPacketBytesThisCall + sentControlPacketBytesThisCall);
609 } else if(bWasLongTimeSinceSend && (sentStandardPacketBytesThisCall + sentControlPacketBytesThisCall) < minFragSize) {
610 if (minFragSize >= sentStandardPacketBytesThisCall + sentControlPacketBytesThisCall && tosend > minFragSize-(sentStandardPacketBytesThisCall + sentControlPacketBytesThisCall))
611 tosend = minFragSize-(sentStandardPacketBytesThisCall + sentControlPacketBytesThisCall);
612 } else {
613 uint32 nextFragMaxBytesToSent = GetNextFragSize(sentStandardPacketBytesThisCall + sentControlPacketBytesThisCall, minFragSize);
614 if (nextFragMaxBytesToSent >= sentStandardPacketBytesThisCall + sentControlPacketBytesThisCall && tosend > nextFragMaxBytesToSent-(sentStandardPacketBytesThisCall + sentControlPacketBytesThisCall))
615 tosend = nextFragMaxBytesToSent-(sentStandardPacketBytesThisCall + sentControlPacketBytesThisCall);
617 wxASSERT(tosend != 0 && tosend <= sendblen-sent);
619 //DWORD tempStartSendTick = ::GetTickCount();
621 lastSent = ::GetTickCount();
623 uint32 result = CEncryptedStreamSocket::Write(sendbuffer+sent,tosend);
625 if (Error()){
627 uint32 error = LastError();
628 if (error == wxSOCKET_WOULDBLOCK){
629 m_bBusy = true;
631 SocketSentBytes returnVal = { true, sentStandardPacketBytesThisCall, sentControlPacketBytesThisCall };
633 return returnVal; // Send() blocked, onsend will be called when ready to send again
634 } else{
635 // Send() gave an error
636 anErrorHasOccured = true;
638 } else {
639 // we managed to send some bytes. Perform bookkeeping.
640 m_bBusy = false;
641 m_hasSent = true;
643 sent += result;
645 // Log send bytes in correct class
646 if(m_currentPacket_is_controlpacket == false) {
647 sentStandardPacketBytesThisCall += result;
649 if(m_currentPackageIsFromPartFile == true) {
650 m_numberOfSentBytesPartFile += result;
651 } else {
652 m_numberOfSentBytesCompleteFile += result;
654 } else {
655 sentControlPacketBytesThisCall += result;
656 m_numberOfSentBytesControlPacket += result;
661 if (sent == sendblen){
662 // we are done sending the current packet. Delete it and set
663 // sendbuffer to NULL so a new packet can be fetched.
664 delete[] sendbuffer;
665 sendbuffer = NULL;
666 sendblen = 0;
668 if(!m_currentPacket_is_controlpacket) {
669 m_actualPayloadSizeSent += m_actualPayloadSize;
670 m_actualPayloadSize = 0;
672 lastFinishedStandard = ::GetTickCount(); // reset timeout
673 m_bAccelerateUpload = false; // Safe until told otherwise
676 sent = 0;
681 if(onlyAllowedToSendControlPacket && (!m_control_queue.empty() || (sendbuffer != NULL && m_currentPacket_is_controlpacket))) {
682 // enter control packet send queue
683 // we might enter control packet queue several times for the same package,
684 // but that costs very little overhead. Less overhead than trying to make sure
685 // that we only enter the queue once.
686 //printf("* Requeueing control packet on %p\n", this);
687 theApp->uploadBandwidthThrottler->QueueForSendingControlPacket(this, HasSent());
690 //printf("* Finishing send debug on %p\n",this);
692 SocketSentBytes returnVal = { !anErrorHasOccured, sentStandardPacketBytesThisCall, sentControlPacketBytesThisCall };
694 return returnVal;
698 uint32 CEMSocket::GetNextFragSize(uint32 current, uint32 minFragSize)
700 if(current % minFragSize == 0) {
701 return current;
702 } else {
703 return minFragSize*(current/minFragSize+1);
709 * Decides the (minimum) amount the socket needs to send to prevent timeout.
711 * @author SlugFiller
713 uint32 CEMSocket::GetNeededBytes()
715 uint32 sendgap;
717 uint64 timetotal;
718 uint64 timeleft;
719 uint64 sizeleft, sizetotal;
722 wxMutexLocker lock(m_sendLocker);
724 if (byConnected == ES_DISCONNECTED) {
725 return 0;
728 if (!((sendbuffer && !m_currentPacket_is_controlpacket) || !m_standard_queue.empty())) {
729 // No standard packet to send. Even if data needs to be sent to prevent timout, there's nothing to send.
730 return 0;
733 if (((sendbuffer && !m_currentPacket_is_controlpacket)) && !m_control_queue.empty())
734 m_bAccelerateUpload = true; // We might be trying to send a block request, accelerate packet
736 sendgap = ::GetTickCount() - lastCalledSend;
738 timetotal = m_bAccelerateUpload?45000:90000;
739 timeleft = ::GetTickCount() - lastFinishedStandard;
740 if (sendbuffer && !m_currentPacket_is_controlpacket) {
741 sizeleft = sendblen-sent;
742 sizetotal = sendblen;
743 } else {
744 sizeleft = sizetotal = m_standard_queue.front().packet->GetRealPacketSize();
748 if (timeleft >= timetotal)
749 return sizeleft;
750 timeleft = timetotal-timeleft;
751 if (timeleft*sizetotal >= timetotal*sizeleft) {
752 // don't use 'GetTimeOut' here in case the timeout value is high,
753 if (sendgap > SEC2MS(20))
754 return 1; // Don't let the socket itself time out - Might happen when switching from spread(non-focus) slot to trickle slot
755 return 0;
757 uint64 decval = timeleft*sizetotal/timetotal;
758 if (!decval)
759 return sizeleft;
760 if (decval < sizeleft)
761 return sizeleft-decval+1; // Round up
762 else
763 return 1;
768 * Removes all packets from the standard queue that don't have to be sent for the socket to be able to send a control packet.
770 * Before a socket can send a new packet, the current packet has to be finished. If the current packet is part of
771 * a split packet, then all parts of that split packet must be sent before the socket can send a control packet.
773 * This method keeps in standard queue only those packets that must be sent (rest of split packet), and removes everything
774 * after it. The method doesn't touch the control packet queue.
776 void CEMSocket::TruncateQueues()
778 wxMutexLocker lock(m_sendLocker);
780 // Clear the standard queue totally
781 // Please note! There may still be a standardpacket in the sendbuffer variable!
782 CStdPacketQueue::iterator it = m_standard_queue.begin();
783 for (; it != m_standard_queue.end(); ++it) {
784 delete it->packet;
787 m_standard_queue.clear();
791 uint32 CEMSocket::GetTimeOut() const
793 return m_uTimeOut;
797 void CEMSocket::SetTimeOut(uint32 uTimeOut)
799 m_uTimeOut = uTimeOut;
801 // File_checked_for_headers