1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
|
/**
* Yudit Unicode Editor Source File
*
* GNU Copyright (C) 2002 Gaspar Sinai <gsinai@yudit.org>
* GNU Copyright (C) 2001 Gaspar Sinai <gsinai@yudit.org>
* GNU Copyright (C) 2000 Gaspar Sinai <gsinai@yudit.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2,
* dated June 1991. See file COPYYING for details.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "SEvent.h"
#include "SEventBSD.h"
#include "SExcept.h"
#include <errno.h>
#include <string.h>
#include <stdio.h>
#ifndef USE_WINAPI
#include <signal.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#endif /* ! USE_WINAPI */
#include <sys/types.h>
#ifndef USE_WINAPI
#include <unistd.h>
#include <sys/time.h>
#else
#include <winsock.h>
#include <time.h>
#endif
#ifdef __sun__
#ifndef __svr4__
extern "C"
{
void bzero(void *s, size_t n);
int gettimeofday(struct timeval *, void *);
int select (int width, fd_set* readfds, fd_set* writefds,
fd_set* exceptfds, struct timeval* timeout);
}
#endif
#endif
#ifdef USE_WINAPI
WSADATA* wsaData=0;
WSADATA wsaDataStruct;
#endif
int sEventBSDSignal =0;
void initSignals (bool init);
static long substract (const struct timeval& from, const struct timeval& what);
/*
* Make these part of the class if multi-instance is needed
*/
class SEventDelegate
{
public:
SEventDelegate(void) {}
~SEventDelegate() {}
struct timeval baseTime;
fd_set readFD;
fd_set writeFD;
};
#ifdef USE_WINAPI
// Hacked for winapi
int gettimeofday (struct timeval* tv, void* tz);
int initsockets(bool init);
#define SBAD_SOCKET 0
#define EWOULDBLOCK TRY_AGAIN
#else
#define SBAD_SOCKET -1
#endif
int selectHack (int readSize, fd_set *ro, int writeSize, fd_set *wo,
int exceptSize, fd_set *ex, struct timeval* t);
int nonblockHack (long id);
/**
* @author: Gaspar Sinai <gsinai@yudit.org>
* @version: 2000-04-23
* This is the default unix event handler implementation.
*/
SClient::SClient (SEventSource* s, SEventTarget* t, ClientType _type) : data()
{
source = s;
target = t;
type = _type;
value = s->getId();
progress = 0;
}
SClient::ClientType
SClient::getType ()
{
return type;
}
SClient::~SClient()
{
}
/**
* This is a single implementation of the event handler
*/
SEventBSD::SEventBSD(void) : SEventHandlerImpl()
{
delegate = new SEventDelegate();
roundRobin = 0;
up = true;
readMax = 0;
writeMax = 0;
initSignals(true);
remakeWriteFd();
remakeReadFd();
gettimeofday (&((SEventDelegate*)delegate)->baseTime, 0);
seedTimer();
CHECK_NEW (delegate);
}
/**
* The tables will clean up.
*/
SEventBSD::~SEventBSD()
{
initSignals(false);
delete (((SEventDelegate*)delegate));
}
/**
* Add a timer to the list of event sources.
* @param tim is the timer event soruce
* @param t is the target of events
*/
void
SEventBSD::addTimer (STimer* tim, SEventTarget* t)
{
if (sourceHashtable.get (tim))
{
fprintf (stderr, "attempted to add save event source (timer) twice.\n");
return;
}
SEventSource* s = tim;
addTimer (s, t);
}
void
SEventBSD::addTimer (SEventSource* s, SEventTarget* t)
{
SClient* c = new SClient (s, t, SClient::TIMER);
CHECK_NEW (c);
/* Check if there is alreay somebody there...*/
targetHashtable.put (t, c, false);
sourceHashtable.put (s, c, false);
clientHashtable.put((const SString&)c->id, c);
struct timeval timenow;
struct timeval timeout;
gettimeofday (&timenow, 0);
// If time is set back
if (timenow.tv_sec < ((SEventDelegate*)delegate)->baseTime.tv_sec)
{
((SEventDelegate*)delegate)->baseTime.tv_sec = timenow.tv_sec;
((SEventDelegate*)delegate)->baseTime.tv_usec = timenow.tv_usec;
}
timeout.tv_sec = timenow.tv_sec; timeout.tv_usec = timenow.tv_usec;
timeout.tv_sec += ((unsigned long)c->value)/1000;
timeout.tv_usec += (((unsigned long)c->value)%1000) * 1000;
timeout.tv_sec += timeout.tv_usec / 1000000;
timeout.tv_usec = timeout.tv_usec % 1000000;
long to = substract (timeout, ((SEventDelegate*)delegate)->baseTime);
unsigned int p = timeoutVector.appendSorted (to);
if (to != timeoutVector[p] && timeoutClient.size() + 1 != timeoutVector.size())
{
fprintf (stderr, "SEventBSD: bad news for timers\n");
}
timeoutClient.insert (p, c);
//fprintf (stderr, "SEventBSD: add timer %u\n", p);
}
/**
* Add a server to the list of event sources.
* @param tim is the server event source
* @param t is the target of events
*/
void
SEventBSD::addServer (SServerStream* st, SEventTarget* t)
{
if (sourceHashtable.get (st))
{
fprintf (stderr, "attempted to add save event source (server) twice.\n");
return;
}
SEventSource* s = st;
SClient* c = new SClient (s, t, SClient::SERVER);
CHECK_NEW (c);
nonblockHack (c->value);
/* Check if there is alreay somebody there...*/
targetHashtable.put (t, c, false);
sourceHashtable.put (s, c, false);
clientHashtable.put ((const SString&)c->id, c);
unsigned int p = readVector.appendSorted (c->value);
readClient.insert (p, c);
remakeReadFd();
}
/**
* Add an input stream to the list of event sources.
* @param ins is the input event source
* @param t is the target of events
*/
void
SEventBSD::addInput (SInputStream* ins, SEventTarget* t)
{
if (sourceHashtable.get (ins))
{
fprintf (stderr, "attempted to add save event source (input) twice.\n");
return;
}
SEventSource* s = ins;
SClient* c = new SClient (s, t, SClient::READER);
CHECK_NEW (c);
nonblockHack (c->value);
/* Check if there is alreay somebody there...*/
targetHashtable.put (t, c, false);
sourceHashtable.put (s, c, false);
clientHashtable.put((const SString&)c->id, c);
unsigned int p = readVector.appendSorted (c->value);
readClient.insert (p, c);
remakeReadFd();
}
/**
* Add a job to the list of event sources.
* @param sjob is the input event source
* @param t is the target of events
*/
void
SEventBSD::addJob (SJob* sjob, SEventTarget* t)
{
if (sourceHashtable.get (sjob))
{
fprintf (stderr, "attempted to add save event source (input) twice.\n");
return;
}
SEventSource* s = sjob;
SClient* c = new SClient (s, t, SClient::JOB);
CHECK_NEW (c);
targetHashtable.put (t, c, false);
sourceHashtable.put (s, c, false);
clientHashtable.put((const SString&)c->id, c);
unsigned int p = jobVector.appendSorted (c->value);
//fprintf (stderr, "SEventBSD: addjob %u - full=%u\n", p, jobClient.size());
if (c->value != jobVector[p] && jobClient.size() + 1
!= jobVector.size())
{
fprintf (stderr, "SEventBSD: bad news for jobs\n");
}
jobClient.insert (p, c);
}
/**
* Add an output stream to the list of event sources.
* @param outs is the input event source
* @param t is the target of events
*/
void
SEventBSD::addOutput (SOutputStream* outs, SEventTarget* t, const SString& m)
{
if (sourceHashtable.get (outs))
{
fprintf (stderr, "attempted to add save event source (output) twice.\n");
return;
}
SEventSource* s = outs;
SClient* c = new SClient (s, t, SClient::WRITER);
CHECK_NEW (c);
c->data = m;
nonblockHack (c->value);
/* Check if there is alreay somebody there...*/
targetHashtable.put (t, c, false);
sourceHashtable.put (s, c, false);
clientHashtable.put((const SString&)c->id,c);
unsigned int p = writeVector.appendSorted (c->value);
writeClient.insert (p, c);
remakeWriteFd();
}
/**
* Remove all wires that connect the target to an event source
* @param t is the target
*/
void
SEventBSD::remove (SEventTarget* t)
{
while (true)
{
SClient* cl = targetHashtable.get (t);
if (cl == 0)
{
//fprintf (stderr, "SEventBSD::remove - target already got removed.\n");
return;
}
remove (cl);
}
}
/**
* Remove all wires that connect the target to an event source
* @param s is the source
*/
void
SEventBSD::remove (SEventSource* s)
{
while (true)
{
SClient* cl = sourceHashtable.get (s);
if (cl == 0)
{
//fprintf (stderr, "SEventBSD::remove - source already got removed.\n");
return;
}
remove (cl);
}
}
/**
* remove the client from sources
* This is used internally.
*/
void
SEventBSD::remove (SClient* c)
{
if (c==0)
{
fprintf (stderr, "SEventBSD::remove tried to remove zero client.\n");
return;
}
if (clientHashtable.get ((const SString&)c->id)==0) return;
int cindex=-1;
switch (c->getType())
{
case SClient::WRITER:
cindex=writeClient.find (c);
//fprintf (stderr, "removing writer %u\n", cindex);
if (cindex<0)
{
/* this should not be an error - it removed itself -*/
//fprintf (stderr, "SEventBSD::remove can not find writer.\n");
remakeWriteFd();
break;
}
writeVector.remove (cindex);
writeClient.remove (cindex);
remakeWriteFd();
break;
case SClient::READER:
case SClient::SERVER:
cindex=readClient.find (c);
//fprintf (stderr, "removing reader %u\n", cindex);
if (cindex<0)
{
/* this should not be an error - it removed itself -*/
//fprintf (stderr, "SEventBSD::remove can not find reader.\n");
//fprintf (stderr, "SEventBSD::readvector:%u readClient:%u.\n",
// readVector.size(), readClient.size());
remakeReadFd();
break;
}
readVector.remove (cindex);
readClient.remove (cindex);
remakeReadFd();
break;
case SClient::TIMER:
cindex=timeoutClient.find (c);
//fprintf (stderr, "removing timer %u\n", cindex);
if (cindex<0)
{
/* this should not be an error - it removed itself -*/
fprintf (stderr, "SEventBSD::remove can not find timer.\n");
break;
}
timeoutVector.remove (cindex);
timeoutClient.remove (cindex);
break;
case SClient::JOB:
cindex=jobClient.find (c);
//fprintf (stderr, "removing job %u\n", cindex);
if (cindex<0)
{
/* this should not be an error - it removed itself -*/
//fprintf (stderr, "SEventBSD::remove can not find job.\n");
break;
}
jobVector.remove (cindex);
jobClient.remove (cindex);
break;
}
if (cindex < 0) return;
/* if there are multiple matches, search the one that
needs to be removed. */
SClientVector addbackTarget;
/** targets **/
while (targetHashtable.get (c->target))
{
SClient* ab = targetHashtable.get (c->target);
if (ab == c)
{
targetHashtable.remove (c->target);
break;
}
addbackTarget.append (ab);
targetHashtable.remove (c->target);
}
unsigned int i;
/* adding back stuff. */
for (i=0; i<addbackTarget.size(); i++)
{
targetHashtable.put (addbackTarget[i]->target,
addbackTarget[i], false);
}
/** sources **/
SClientVector addbackSource;
while (sourceHashtable.get (c->source))
{
SClient* ab = sourceHashtable.get (c->source);
if (ab == c)
{
sourceHashtable.remove (c->source);
break;
}
addbackSource.append (ab);
sourceHashtable.remove (ab->source);
}
for (i=0; i<addbackSource.size(); i++)
{
sourceHashtable.put (addbackSource[i]->source,
addbackSource[i], false);
}
clientHashtable.remove ((const SString&)c->id);
delete c;
return;
}
/**
* This is to remake the fd's
*/
void
SEventBSD::remakeWriteFd()
{
FD_ZERO (&((SEventDelegate*)delegate)->writeFD);
writeMax = 0;
unsigned int i;
for (i = 0; i<writeVector.size(); i++)
{
int fd = (int)writeVector[i];
#ifdef USE_WINAPI
if (fd<=0)
#else
if (fd<0)
#endif
{
fprintf (stderr, "SEventBSD::remakeWriteFd: bad file [%d].\n", fd);
}
else
{
FD_SET (fd, &((SEventDelegate*)delegate)->writeFD);
}
}
if (i>0) writeMax = (int)writeVector[i-1] +1;
}
/**
* Remaking the read fd's
*/
void
SEventBSD::remakeReadFd()
{
FD_ZERO (&((SEventDelegate*)delegate)->readFD);
readMax = 0;
unsigned int i;
for (i = 0; i<readVector.size(); i++)
{
int fd = (int)readVector[i];
#ifdef USE_WINAPI
if (fd<=0)
#else
if (fd<0)
#endif
{
fprintf (stderr, "SEventBSD::remakeReadFd: bad file [%d].\n", fd);
}
else
{
FD_SET (fd, &((SEventDelegate*)delegate)->readFD);
}
}
if (i>0) readMax = (int)readVector[i-1] +1;
}
void
SEventBSD::start()
{
while (next());
}
void
SEventBSD::exit()
{
up = false;
}
/**
* Process a next event. Return false if exit was called.
*/
bool
SEventBSD::next()
{
if (!up)
{
#ifndef USE_WINAPI
fprintf (stderr, "SEventBSD::next - down...\n");
#endif
return false;
}
#ifdef USE_WINAPI
int dummy;
dummy = initsockets (true);
if (dummy < 0)
{
fprintf (stderr, "SEventBSD::next - no socket...\n");
return false;
}
#endif
unsigned int i;
struct timeval timeout;
struct timeval timenow;
struct timeval* tp= 0;
//fprintf (stderr, "readvector=%u writeVector=%u timeoutVector=%u jobVector=%u\n",
//readVector.size(), writeVector.size(), timeoutVector.size(), jobVector.size());
if (readVector.size() == 0 && writeVector.size()==0
&& timeoutVector.size() == 0 && jobVector.size()==0)
{
fprintf (stderr, "SEventBSD::next - nothing to do\n");
return false;
}
int more = true;
// Get the jobs done first
if (jobClient.size() != 0)
{
// This is no rocket science here... Just a job
while (more && jobVector.size())
{
more = false;
roundRobin++;
unsigned int k = roundRobin % jobVector.size();
SClient* c = jobClient[k];
SJob *j= (SJob*)c->source;
SEventTarget *t=c->target;
remove (c);
int status = j->run();
t->done(j);
if (status>0) more = true;
}
}
/* jobs can affect up flag */
if (!up)
{
#ifndef USE_WINAPI
fprintf (stderr, "SEventBSD::next - down...\n");
#endif
return false;
}
// Create a timeout
if (timeoutVector.size() != 0)
{
gettimeofday (&timenow, 0);
// If time is set back
if (timenow.tv_sec < ((SEventDelegate*)delegate)->baseTime.tv_sec)
{
((SEventDelegate*)delegate)->baseTime.tv_sec = timenow.tv_sec;
((SEventDelegate*)delegate)->baseTime.tv_usec = timenow.tv_usec;
}
long diff = timeoutVector[0] - substract (timenow, ((SEventDelegate*)delegate)->baseTime);
unsigned long next = (unsigned long) diff;
if (diff < 0)
{
next = 0;
}
timeout.tv_sec = next/1000;
timeout.tv_usec = (next%1000) * 1000;
tp = &timeout;
}
fd_set ro;
fd_set wo;
memcpy (&ro, &((SEventDelegate*)delegate)->readFD, sizeof (fd_set));
memcpy (&wo, &((SEventDelegate*)delegate)->writeFD, sizeof (fd_set));
//Does not always get the SIGPIPE on LInux Alpha
//fprintf (stderr, "Before select %d %d\n", FD_ISSET (5, &wo), FD_ISSET (5, &ro));
//fprintf (stderr, "s=%u %u %u %u\n",
// readVector.size(), writeVector.size(), timeoutVector.size(),
// jobVector.size());
/*
if (readVector.size() == 0 && writeVector.size() ==0
&& timeoutVector.size() == 0)
{
return (jobVector.size() != 0);
}
*/
int selected = selectHack (readMax, &ro, writeMax, &wo, 0, 0, tp);
//fprintf (stderr, "After select %d %d\n", FD_ISSET (4, &wo), FD_ISSET (5, &ro));
#ifdef USE_WINAPI
/* -2 is windows event */
if (selected == -2)
{
return true;
}
#endif
if (selected == -1)
{
#ifdef USE_WINAPI
int last = WSAGetLastError();
fprintf (stderr, "SEventBSD:select ERROR %d\n", last);
#endif
return false;
}
/*
* Timer event
*/
if (selected == 0)
{
if (timeoutClient.size() ==0)
{
fprintf (stderr, "unexpected timeout in : SEventBSD::next\n");
}
else
{
// FIXME: xomething is wrong with this array.
// maybe it is the sorted nature.
SClient* c = timeoutClient[0];
SEventSource *s=c->source;
SEventTarget *t=c->target;
SString id = SString((const SString&)c->id);
if (clientHashtable.get (id)!=0)
{
if (!t->timeout(s))
{
if (clientHashtable.get (id)!=0)
{
remove (c);
}
}
else
{
if (clientHashtable.get (id)!=0)
{
remove (c);
addTimer (s, t);
}
}
}
else
{
fprintf (stderr, "SEventBSD: miracle happened with timer...\n");
timeoutVector.remove (0);
timeoutClient.remove (0);
}
//return true;
}
}
// See sockets. Lets see reading...
for (i=0; i<readVector.size(); i++)
{
int fd = (int)readVector[i];
if (FD_ISSET (fd, &ro))
{
SClient *c=readClient[i];
SEventSource *s=c->source;
SEventTarget *t=c->target;
if (c->getType()==SClient::SERVER)
{
if (!t->serve (s))
{
remove (c);
}
FD_CLR (fd, &ro);
break;
}
if (t->readable(s)==0)
{
FD_CLR (fd, &ro);
continue;
}
char* buffer = new char[4096];
CHECK_NEW (buffer);
int progress = 0;
int n;
#ifdef USE_WINAPI
n = recv(fd, &buffer[progress], 4096, 0);
#else
n = ::read(fd, &buffer[progress], 4096);
#endif
if (n<0 && (errno == EINTR || errno==EAGAIN || errno==EWOULDBLOCK)) n = 0;
if (n<0)
{
remove (c);
t->error(s);
delete buffer;
break;
}
else if (!sendData(c, buffer, n))
{
remove (c);
delete buffer;
break;
}
delete buffer;
FD_CLR (fd, &ro);
}
}
// See sockets. Lets see writing...
for (i=0; i<writeVector.size(); i++)
{
int fd = (int)writeVector[i];
if (FD_ISSET (fd, &wo))
{
SClient *c=writeClient[i];
SEventSource *s=c->source;
SEventTarget *t=c->target;
// finally...
int n;
sEventBSDSignal = 0;
#ifdef USE_WINAPI
n = ::send(fd, &c->data.array()[c->progress], c->data.size()-c->progress, 0);
#else
n = ::write(fd, &c->data.array()[c->progress], c->data.size()-c->progress);
#endif
if (n<0 && (errno == EINTR || errno==EAGAIN || errno==EWOULDBLOCK)) n = 0;
if (sEventBSDSignal!=0)
{
sEventBSDSignal=0; n=-1;
}
if (n<0)
{
remove (c);
t->error(s);
break;
}
else if ((unsigned int) n == (c->data.size() - c->progress))
{
remove (c);
t->write(s);
break;
}
else
{
c->progress += n;
}
FD_CLR (fd, &wo);
}
}
return true;
}
/**
* Send the whole buffer of new lines to client...
* @return true if client wants our data.
* if size is 0 then we always return true
*/
bool
SEventBSD::sendData(SClient* c, char* buffer, int size)
{
SEventSource* s=c->source;
SEventTarget* t=c->target;
SString data (buffer, size);
return (t->read (s, data));
}
/**
* This is needed if the app is up for more than a year
* to recalculate the timeout values in the queue.
*/
void
SEventBSD::seedTimer()
{
struct timeval oldTime;
oldTime.tv_sec = ((SEventDelegate*)delegate)->baseTime.tv_sec;
oldTime.tv_usec = ((SEventDelegate*)delegate)->baseTime.tv_usec;
gettimeofday (&((SEventDelegate*)delegate)->baseTime, 0);
// Base minus old
//fprintf (stderr, "seed timer\n");
long diff = substract (((SEventDelegate*)delegate)->baseTime, oldTime);
for (unsigned int i=0; i<timeoutVector.size(); i++)
{
long t = timeoutVector[i];
timeoutVector.replace (i, t-diff);
}
}
/**
* Substract the time int what from the time in from
* @return from - what in miliseconds.
*/
static long
substract (const struct timeval& from, const struct timeval& what)
{
// Clock is moving back.
if (what.tv_sec > from.tv_sec)
{
unsigned long what_sec = what.tv_sec-1;
unsigned long what_usec = what.tv_usec + 1000000;
return -((what_sec - from.tv_sec) * 1000 + (what_usec - from.tv_usec) / 1000);
}
// Time is moving forward
else if (what.tv_sec < from.tv_sec)
{
unsigned long from_sec = from.tv_sec-1;
unsigned long from_usec = from.tv_usec + 1000000;
return ((from_sec - what.tv_sec) * 1000 + (from_usec - what.tv_usec) / 1000);
}
// sec is same. Time is moving back
else if (what.tv_usec > from.tv_usec)
{
return -((what.tv_usec - from.tv_usec) /1000);
}
// Time is moving forward
return ((from.tv_usec - what.tv_usec) /1000);
}
#ifdef USE_WINAPI
int (*_windowsSelectHookup)(int readSize, fd_set *ro,
int writeSize, fd_set *wo, int exceptSize, fd_set *ex,
struct timeval* t) = 0;
#endif
/**
* This is a hack to make select work on windows too by Gaspar
*/
int
selectHack (int readSize, fd_set *ro, int writeSize, fd_set *wo,
int exceptSize, fd_set *ex, struct timeval* t)
{
int maxFd = (readSize > writeSize) ? readSize : writeSize;
maxFd = (exceptSize > maxFd) ? exceptSize : maxFd;
#ifdef USE_WINAPI
if (_windowsSelectHookup)
{
return _windowsSelectHookup(readSize, ro, writeSize, wo, exceptSize, ex, t);
}
// Huh, good eh?
if (t!=0 && t->tv_sec==0 && t->tv_usec==0) t=0;
fd_set* my_ro = (readSize==0) ? 0 : ro;
if (readSize == 0 && ro != 0) FD_ZERO (ro);
fd_set* my_wo = (writeSize==0) ? 0 : wo;
if (writeSize == 0 && wo != 0) FD_ZERO (wo);
fd_set* my_ex = (exceptSize==0) ? 0 : ex;
if (exceptSize == 0 && ex != 0) FD_ZERO (ex);
if (my_ro==0 && my_wo ==0)
{
if (t==0) return 0;
DWORD millisec = (DWORD) (t->tv_sec * 1000 + t->tv_usec / 1000);
// Probably this is the only case sensitive in whole windows.
Sleep (millisec);
return 0;
}
return select (maxFd, ro, wo, ex, t);
#else
// Works as it should on unix
return select (maxFd, ro, wo, ex, t);
#endif
}
/**
* This makes socket non-blocking. Really...
*/
int
nonblockHack (long id)
{
#ifdef USE_WINAPI
u_long arg=1;
return ioctlsocket ((int)id, FIONBIO, &arg);
#else
int opts = fcntl((int)id,F_GETFL);
if (opts < 0)
{
//fprintf (stderr, "SEventBSD:nonblockHack[%d] %s\n", errno, strerror (errno));
return -1;
}
opts = (opts | O_NONBLOCK);
if (fcntl((int)id,F_SETFL,opts) < 0)
{
//fprintf (stderr, "SEventBSD:nonblockHack[%d] %s\n", errno, strerror (errno));
return -1;
}
return 0;
/*
int ret;
int arg = 1;
ret = ioctl ((int)id, FIONBIO, (char*) &arg);
if (ret!=0)
{
fprintf (stderr, "SEventBSD:nonblockHack[%d] %s\n", errno, strerror(errno));
}
return ret;
*/
#endif
}
#ifdef USE_WINAPI
/**
* Poor man's getttime by Gaspar. Timezone is gmt.
* @param tz is grossly ignored.
* @raram tv is the returned timeval.
* @return 0
*/
int
gettimeofday (struct timeval* tv, void* tz)
{
time_t now;
time (&now);
time_t onetick = (time_t) (1.0/difftime (now+1, now));
tv->tv_sec = now/onetick;
tv->tv_usec = now%onetick;
return 0;
}
/**
* Try to initialize stuping winsock.
* return 0 on fail.
*/
int
initsockets(bool init)
{
static int state = 0;
if (!init)
{
if (state = 1)
{
closesocket (state);
WSACleanup();
wsaData = 0;
return 1;
}
return 1;
}
if (state == 0)
{
WORD version = MAKEWORD (1,1);
if (WSAStartup (version, &wsaDataStruct)!=0)
{
fprintf (stderr, "SEventBSD.cpp:initsockets can not initialize[%d].\n",
WSAGetLastError());
state = -1;
}
else
{
state=socket (AF_INET, SOCK_STREAM, IPPROTO_IP);
if (state==0)
{
fprintf (stderr, "SEventBSD.cpp: failed to open socket[%d].%s\n", errno, strerror (errno));
state = -1;
}
else
{
u_long arg=1;
//ioctlsocket (state, FIONBIO, &arg);
wsaData = &wsaDataStruct;
state = 1;
}
}
}
return state;
}
#endif
#ifdef USE_WINAPI
void
initSignals(bool init)
{
}
#else
static void signalHandler (int signum);
void
initSignals(bool init)
{
static bool inited = false;
if (inited && init) return;
static struct sigaction oact;
struct sigaction act;
if (init)
{
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
act.sa_handler = signalHandler;
#ifdef SA_INTERRUPT
act.sa_flags |= SA_INTERRUPT;
#endif
sigaction (SIGALRM, &act, &oact);
act.sa_handler = SIG_IGN;
sigaction (SIGPIPE, &act, &oact);
inited = true;
}
else
{
sigaction (SIGPIPE, &oact, &act);
sigaction (SIGALRM, &oact, &act);
inited = false;
}
}
static void
signalHandler (int signum)
{
switch (signum)
{
case SIGPIPE:
fprintf (stderr, "SEventBSD: disconnected - SIGPIPE\n");
break;
case SIGALRM:
fprintf (stderr, "SEventBSD: timeout - SIGALRM\n");
break;
default:
fprintf (stderr, "SEventBSD: signal %d\n", signum);
}
sEventBSDSignal = signum;
return;
}
#endif
|