#include <network.h>
Public Methods | |
| Network (NetworkAddress addr, double reliability, VoidFunctionPtr readAvail, VoidFunctionPtr writeDone, int callArg) | |
| ~Network () | |
| void | Send (PacketHeader hdr, char *data) |
| PacketHeader | Receive (char *data) |
| void | SendDone () |
| void | CheckPktAvail () |
Private Attributes | |
| NetworkAddress | ident |
| double | chanceToWork |
| int | sock |
| char | sockName [32] |
| VoidFunctionPtr | writeHandler |
| VoidFunctionPtr | readHandler |
| int | handlerArg |
| bool | sendBusy |
| bool | packetAvail |
| PacketHeader | inHdr |
| char | inbox [MaxPacketSize] |
|
||||||||||||||||||||||||
|
Definition at line 28 of file network.cc. References AssignNameToSocket, chanceToWork, FALSE, handlerArg, ident, inHdr, PacketHeader::length, NetworkAddress, NetworkReadPoll, NetworkRecvInt, NetworkTime, OpenSocket, readHandler, sendBusy, sock, sockName, VoidFunctionPtr, and writeHandler.
00030 {
00031 ident = addr;
00032 if (reliability < 0) chanceToWork = 0;
00033 else if (reliability > 1) chanceToWork = 1;
00034 else chanceToWork = reliability;
00035
00036 // set up the stuff to emulate asynchronous interrupts
00037 writeHandler = writeDone;
00038 readHandler = readAvail;
00039 handlerArg = callArg;
00040 sendBusy = FALSE;
00041 inHdr.length = 0;
00042
00043 sock = OpenSocket();
00044 sprintf(sockName, "SOCKET_%d", (int)addr);
00045 AssignNameToSocket(sockName, sock); // Bind socket to a filename
00046 // in the current directory.
00047
00048 // start polling for incoming packets
00049 interrupt->Schedule(NetworkReadPoll, (int)this, NetworkTime, NetworkRecvInt);
00050 }
|
|
|
Definition at line 52 of file network.cc. References CloseSocket, DeAssignNameToSocket, sock, and sockName.
00053 {
00054 CloseSocket(sock);
00055 DeAssignNameToSocket(sockName);
00056 }
|
|
|
Definition at line 62 of file network.cc. References ASSERT, bcopy, DEBUG, PacketHeader::from, ident, inbox, inHdr, PacketHeader::length, MaxPacketSize, MaxWireSize, NetworkReadPoll, NetworkRecvInt, NetworkTime, PollSocket, ReadFromSocket, sock, and PacketHeader::to. Referenced by NetworkReadPoll.
00063 {
00064 // schedule the next time to poll for a packet
00065 interrupt->Schedule(NetworkReadPoll, (int)this, NetworkTime, NetworkRecvInt);
00066
00067 if (inHdr.length != 0) // do nothing if packet is already buffered
00068 return;
00069 if (!PollSocket(sock)) // do nothing if no packet to be read
00070 return;
00071
00072 // otherwise, read packet in
00073 char *buffer = new char[MaxWireSize];
00074 ReadFromSocket(sock, buffer, MaxWireSize);
00075
00076 // divide packet into header and data
00077 inHdr = *(PacketHeader *)buffer;
00078 ASSERT((inHdr.to == ident) && (inHdr.length <= MaxPacketSize));
00079 bcopy(buffer + sizeof(PacketHeader), inbox, inHdr.length);
00080 delete []buffer ;
00081
00082 DEBUG('n', "Network received packet from %d, length %d...\n",
00083 (int) inHdr.from, inHdr.length);
00084 stats->numPacketsRecvd++;
00085
00086 // tell post office that the packet has arrived
00087 (*readHandler)(handlerArg);
00088 }
|
|
|
Definition at line 132 of file network.cc. References bcopy, inbox, inHdr, and PacketHeader::length. Referenced by PostOffice::PostalDelivery.
|
|
||||||||||||
|
Definition at line 105 of file network.cc. References ASSERT, bcopy, chanceToWork, DEBUG, FALSE, PacketHeader::from, ident, PacketHeader::length, MaxPacketSize, MaxWireSize, NetworkSendDone, NetworkSendInt, NetworkTime, Random, sendBusy, SendToSocket, sock, and PacketHeader::to. Referenced by PostOffice::Send.
00106 {
00107 char toName[32];
00108
00109 sprintf(toName, "SOCKET_%d", (int)hdr.to);
00110
00111 ASSERT((sendBusy == FALSE) && (hdr.length > 0)
00112 && (hdr.length <= MaxPacketSize) && (hdr.from == ident));
00113 DEBUG('n', "Sending to addr %d, %d bytes... ", hdr.to, hdr.length);
00114
00115 interrupt->Schedule(NetworkSendDone, (int)this, NetworkTime, NetworkSendInt);
00116
00117 if (Random() % 100 >= chanceToWork * 100) { // emulate a lost packet
00118 DEBUG('n', "oops, lost it!\n");
00119 return;
00120 }
00121
00122 // concatenate hdr and data into a single buffer, and send it out
00123 char *buffer = new char[MaxWireSize];
00124 *(PacketHeader *)buffer = hdr;
00125 bcopy(data, buffer + sizeof(PacketHeader), hdr.length);
00126 SendToSocket(sock, buffer, MaxWireSize, toName);
00127 delete []buffer;
00128 }
|
|
|
Definition at line 92 of file network.cc. References FALSE, and sendBusy. Referenced by NetworkSendDone.
|
|
|
|
|
|
Definition at line 92 of file network.h. Referenced by Network. |
|
|
Definition at line 84 of file network.h. Referenced by CheckPktAvail, Network, and Send. |
|
|
Definition at line 98 of file network.h. Referenced by CheckPktAvail, and Receive. |
|
|
Definition at line 97 of file network.h. Referenced by CheckPktAvail, Network, and Receive. |
|
|
|
|
|
Definition at line 90 of file network.h. Referenced by Network. |
|
|
|
|
|
Definition at line 86 of file network.h. Referenced by CheckPktAvail, Network, Send, and ~Network. |
|
|
|
|
|
Definition at line 88 of file network.h. Referenced by Network. |
1.2.14 written by Dimitri van Heesch,
© 1997-2002