| 1 |
import raknet.client; |
|---|
| 2 |
import raknet.server; |
|---|
| 3 |
|
|---|
| 4 |
import std.string; |
|---|
| 5 |
import std.stdio; |
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
int main() |
|---|
| 9 |
{ |
|---|
| 10 |
Packet *packet; |
|---|
| 11 |
|
|---|
| 12 |
char[512] str; |
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
printf("(C)lient or (S)erver?\n"); |
|---|
| 16 |
gets(str); |
|---|
| 17 |
if (str[0]=='c' || str[0]=='C') |
|---|
| 18 |
{ |
|---|
| 19 |
raknet.client.initialize(); |
|---|
| 20 |
writefln("Init client"); |
|---|
| 21 |
} |
|---|
| 22 |
else |
|---|
| 23 |
{ |
|---|
| 24 |
raknet.server.initialize(); |
|---|
| 25 |
writefln("Init server"); |
|---|
| 26 |
} |
|---|
| 27 |
// above works /////////////////////////////////////// |
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
if (raknet.server.isServer()) |
|---|
| 31 |
{ |
|---|
| 32 |
// Running in server mode on port 60000 |
|---|
| 33 |
// this seems to work only in C |
|---|
| 34 |
if (raknet.server.start(32, 0, 0, 60000)) |
|---|
| 35 |
printf("Starting the server.\n"); |
|---|
| 36 |
else |
|---|
| 37 |
printf("Failed to start the server.\n"); |
|---|
| 38 |
|
|---|
| 39 |
raknet.server.REG_AS_RPC("PrintMessage", &PrintMessage); |
|---|
| 40 |
} |
|---|
| 41 |
else |
|---|
| 42 |
{ |
|---|
| 43 |
// Running in client mode |
|---|
| 44 |
printf("Enter server IP or hit enter for 127.0.0.1\n"); |
|---|
| 45 |
gets(str); |
|---|
| 46 |
// 127.0.0.1 designates the feedback loop so we can test on one computer |
|---|
| 47 |
if (str[0]==0) |
|---|
| 48 |
{ |
|---|
| 49 |
strcpy(str, "127.0.0.1"); |
|---|
| 50 |
|
|---|
| 51 |
for (int i = 0; i < strlen(str); i++) |
|---|
| 52 |
writef(str[i]); |
|---|
| 53 |
|
|---|
| 54 |
writef("\n"); |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
if (raknet.client.connect(str, 60000, 0, 0, 0)) |
|---|
| 58 |
printf("Starting the client.\n"); |
|---|
| 59 |
else |
|---|
| 60 |
printf("Failed to start the client.\n"); |
|---|
| 61 |
|
|---|
| 62 |
raknet.client.REG_AS_RPC("PrintMessage", &PrintMessage); |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
while (1) |
|---|
| 67 |
{ |
|---|
| 68 |
|
|---|
| 69 |
if (raknet.client.isClient()) |
|---|
| 70 |
{ |
|---|
| 71 |
//printf("Enter a string or hit enter to display incoming strings\n"); |
|---|
| 72 |
gets(str); |
|---|
| 73 |
// Two tricky things here. First, you have to remember to send the NULL terminator so you need strlen(str)+1 |
|---|
| 74 |
// Second, if you didn't read the docs you might not realize RPC takes the number of bits rather than the number of bytes. |
|---|
| 75 |
// You have to multiply the number of bytes by 8 |
|---|
| 76 |
if (str[0]) |
|---|
| 77 |
{ |
|---|
| 78 |
raknet.client.RPC("PrintMessage", str, (strlen(str)+1)*8, PacketPriority.HIGH_PRIORITY, PacketReliability.RELIABLE_ORDERED, 0, false); |
|---|
| 79 |
} |
|---|
| 80 |
} |
|---|
| 81 |
|
|---|
| 82 |
if (raknet.server.isServer()) |
|---|
| 83 |
packet=raknet.server.receivePacket(); |
|---|
| 84 |
else |
|---|
| 85 |
packet=raknet.client.receivePacket(); |
|---|
| 86 |
|
|---|
| 87 |
while (packet !is null) |
|---|
| 88 |
{ |
|---|
| 89 |
switch (packet.data[0]) |
|---|
| 90 |
{ |
|---|
| 91 |
case ID_REMOTE_DISCONNECTION_NOTIFICATION: |
|---|
| 92 |
printf("Another client has disconnected.\n"); |
|---|
| 93 |
break; |
|---|
| 94 |
case ID_REMOTE_CONNECTION_LOST: |
|---|
| 95 |
printf("Another client has lost the connection.\n"); |
|---|
| 96 |
break; |
|---|
| 97 |
case ID_REMOTE_NEW_INCOMING_CONNECTION: |
|---|
| 98 |
printf("Another client has connected.\n"); |
|---|
| 99 |
break; |
|---|
| 100 |
case ID_CONNECTION_REQUEST_ACCEPTED: |
|---|
| 101 |
debug printf("Our connection request has been accepted.\n"); |
|---|
| 102 |
//printf("Enter a string to show on the server: "); |
|---|
| 103 |
//cin >> str; |
|---|
| 104 |
// Two tricky things here. First, you have to remember to send the NULL terminator so you need strlen(str)+1 |
|---|
| 105 |
// Second, if you didn't read the docs you might not realize RPC takes the number of bits rather than the number of bytes. |
|---|
| 106 |
// You have to multiply the number of bytes by 8 |
|---|
| 107 |
//rakClientInterface->RPC("PrintMessage", str, (strlen(str)+1)*8, HIGH_PRIORITY, RELIABLE_ORDERED, 0, false); |
|---|
| 108 |
break; |
|---|
| 109 |
|
|---|
| 110 |
case ID_NEW_INCOMING_CONNECTION: |
|---|
| 111 |
printf("A connection is incoming.\n"); |
|---|
| 112 |
break; |
|---|
| 113 |
case ID_NO_FREE_INCOMING_CONNECTIONS: |
|---|
| 114 |
printf("The server is full.\n"); |
|---|
| 115 |
break; |
|---|
| 116 |
case ID_DISCONNECTION_NOTIFICATION: |
|---|
| 117 |
if (raknet.server.isServer()) |
|---|
| 118 |
printf("A client has disconnected.\n"); |
|---|
| 119 |
else |
|---|
| 120 |
printf("We have been disconnected.\n"); |
|---|
| 121 |
break; |
|---|
| 122 |
case ID_CONNECTION_LOST: |
|---|
| 123 |
if (raknet.server.isServer()) |
|---|
| 124 |
printf("A client lost the connection.\n"); |
|---|
| 125 |
else |
|---|
| 126 |
printf("Connection lost.\n"); |
|---|
| 127 |
break; |
|---|
| 128 |
case ID_RECEIVED_STATIC_DATA: |
|---|
| 129 |
debug printf("Got static data.\n"); |
|---|
| 130 |
break; |
|---|
| 131 |
default: |
|---|
| 132 |
printf("Message with identifier %i has arrived.\n", packet.data[0]); |
|---|
| 133 |
break; |
|---|
| 134 |
} |
|---|
| 135 |
|
|---|
| 136 |
if (raknet.server.isServer()) |
|---|
| 137 |
raknet.server.deallocatePacket(packet); |
|---|
| 138 |
else |
|---|
| 139 |
raknet.client.deallocatePacket(packet); |
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
// Stay in the loop as long as there are more packets. |
|---|
| 143 |
if (raknet.server.isServer()) |
|---|
| 144 |
packet=raknet.server.receivePacket(); |
|---|
| 145 |
else |
|---|
| 146 |
packet=raknet.client.receivePacket(); |
|---|
| 147 |
} |
|---|
| 148 |
} |
|---|
| 149 |
|
|---|
| 150 |
if (raknet.client.isClient()) |
|---|
| 151 |
raknet.client.destroy(); |
|---|
| 152 |
else if (raknet.server.isServer()) |
|---|
| 153 |
raknet.server.destroy(); |
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
return 0; |
|---|
| 157 |
} |
|---|
| 158 |
|
|---|
| 159 |
/* |
|---|
| 160 |
version(Windows) |
|---|
| 161 |
{ |
|---|
| 162 |
extern(Windows): |
|---|
| 163 |
} |
|---|
| 164 |
else |
|---|
| 165 |
{ |
|---|
| 166 |
extern(C): |
|---|
| 167 |
} |
|---|
| 168 |
*/ |
|---|
| 169 |
|
|---|
| 170 |
extern(C): |
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
char *gets( char *str ); |
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
void PrintMessage(char *input, int numberOfBitsOfData, PlayerID sender) |
|---|
| 177 |
{ |
|---|
| 178 |
printf("%s\n",input); |
|---|
| 179 |
|
|---|
| 180 |
if (raknet.server.isServer()) |
|---|
| 181 |
{ |
|---|
| 182 |
raknet.server.RPC("PrintMessage", input, numberOfBitsOfData, PacketPriority.HIGH_PRIORITY, PacketReliability.RELIABLE_ORDERED, 0, sender, true, false); |
|---|
| 183 |
} |
|---|
| 184 |
} |
|---|