00001
00009 using System;
00010 using System.Collections.Generic;
00011 using System.Text;
00012
00013 namespace Objects3D
00014 {
00022 public class Network
00023 {
00024 public static String KeyHeader = "KEY";
00025 public static String MapHeader = "MAP";
00026 public static String PlrHeader = "PLR";
00027 public static String WldHeader = "WLD";
00028
00029 public static String AckHeader = "ACK";
00030 public static String NewHeader = "NEW";
00031 public static String NtrHeader = "NTR";
00032
00033 public static String EndHeader = "END";
00034 public static String FinHeader = "FIN";
00035
00047 public static byte[] GetEnd(int ATimeStamp)
00048 {
00049 byte[] LResult = new byte[(sizeof(int) * 1) + (sizeof(byte) * 3)];
00050
00051 byte[] LTimeStamp = BitConverter.GetBytes(ATimeStamp);
00052
00053
00054 LResult[(sizeof(int) * 0) + (sizeof(byte) * 0)] = (byte)'E';
00055 LResult[(sizeof(int) * 0) + (sizeof(byte) * 1)] = (byte)'N';
00056 LResult[(sizeof(int) * 0) + (sizeof(byte) * 2)] = (byte)'D';
00057
00058
00059 LTimeStamp.CopyTo(LResult, (sizeof(int) * 0) + (sizeof(byte) * 3));
00060
00061 return LResult;
00062 }
00063
00076 public static byte[] GetFinish(byte AID, int ATimeStamp)
00077 {
00078 byte[] LResult = new byte[(sizeof(int) * 1) + (sizeof(byte) * 4)];
00079
00080 byte[] LTimeStamp = BitConverter.GetBytes(ATimeStamp);
00081
00082
00083 LResult[(sizeof(int) * 0) + (sizeof(byte) * 0)] = (byte)'F';
00084 LResult[(sizeof(int) * 0) + (sizeof(byte) * 1)] = (byte)'I';
00085 LResult[(sizeof(int) * 0) + (sizeof(byte) * 2)] = (byte)'N';
00086
00087
00088 LTimeStamp.CopyTo(LResult, (sizeof(int) * 0) + (sizeof(byte) * 3));
00089
00090
00091 LResult[(sizeof(int) * 1) + (sizeof(byte) * 3)] = AID;
00092
00093 return LResult;
00094 }
00095
00108 public static bool SetEnd(byte[] AData, ref int ATimeStamp)
00109 {
00110 bool LResult = false;
00111
00112 if (AData.Length >= 3)
00113 {
00114
00115 if (new ASCIIEncoding().GetString(AData, 0, 3).Equals(EndHeader))
00116 {
00117 if (AData.Length >= sizeof(int) + 3)
00118 {
00119
00120 ATimeStamp = BitConverter.ToInt32(AData, 3);
00121
00122 LResult = true;
00123 }
00124 }
00125 }
00126
00127 return LResult;
00128 }
00129
00144 public static bool SetFinish(byte[] AData, ref byte AID, ref int ATimeStamp)
00145 {
00146 bool LResult = false;
00147
00148 if (AData.Length >= 3)
00149 {
00150
00151 if (new ASCIIEncoding().GetString(AData, 0, 3).Equals(FinHeader))
00152 {
00153 if (AData.Length >= (sizeof(int) * 1) + 4)
00154 {
00155
00156 ATimeStamp = BitConverter.ToInt32(AData, (sizeof(int) * 0) + 3);
00157
00158
00159 AID = AData[(sizeof(int) * 1) + 3];
00160
00161 LResult = true;
00162 }
00163 }
00164 }
00165
00166 return LResult;
00167 }
00168
00182 public static byte[] GetAcknowledge(int ARandom, byte AID, int ATimeStamp)
00183 {
00184 byte[] LResult = new byte[(sizeof(int) * 2) + (sizeof(byte) * 4)];
00185
00186 byte[] LRandom = BitConverter.GetBytes(ARandom);
00187 byte[] LTimeStamp = BitConverter.GetBytes(ATimeStamp);
00188
00189
00190 LResult[(sizeof(int) * 0) + (sizeof(byte) * 0)] = (byte)'A';
00191 LResult[(sizeof(int) * 0) + (sizeof(byte) * 1)] = (byte)'C';
00192 LResult[(sizeof(int) * 0) + (sizeof(byte) * 2)] = (byte)'K';
00193
00194
00195 LTimeStamp.CopyTo(LResult, (sizeof(int) * 0) + (sizeof(byte) * 3));
00196
00197
00198 LRandom.CopyTo(LResult, (sizeof(int) * 1) + (sizeof(byte) * 3));
00199 LResult[(sizeof(int) * 2) + (sizeof(byte) * 3)] = AID;
00200
00201 return LResult;
00202 }
00203
00215 public static byte[] GetNew(int ATimeStamp)
00216 {
00217 byte[] LResult = new byte[(sizeof(int) * 1) + (sizeof(byte) * 3)];
00218
00219 byte[] LTimeStamp = BitConverter.GetBytes(ATimeStamp);
00220
00221
00222 LResult[(sizeof(int) * 0) + (sizeof(byte) * 0)] = (byte)'N';
00223 LResult[(sizeof(int) * 0) + (sizeof(byte) * 1)] = (byte)'E';
00224 LResult[(sizeof(int) * 0) + (sizeof(byte) * 2)] = (byte)'W';
00225
00226
00227 LTimeStamp.CopyTo(LResult, (sizeof(int) * 0) + (sizeof(byte) * 3));
00228
00229 return LResult;
00230 }
00231
00244 public static byte[] GetEnter(int ARandom, int ATimeStamp)
00245 {
00246 byte[] LResult = new byte[(sizeof(int) * 2) + (sizeof(byte) * 3)];
00247
00248 byte[] LRandom = BitConverter.GetBytes(ARandom);
00249 byte[] LTimeStamp = BitConverter.GetBytes(ATimeStamp);
00250
00251
00252 LResult[(sizeof(int) * 0) + (sizeof(byte) * 0)] = (byte)'N';
00253 LResult[(sizeof(int) * 0) + (sizeof(byte) * 1)] = (byte)'T';
00254 LResult[(sizeof(int) * 0) + (sizeof(byte) * 2)] = (byte)'R';
00255
00256
00257 LTimeStamp.CopyTo(LResult, (sizeof(int) * 0) + (sizeof(byte) * 3));
00258
00259
00260 LRandom.CopyTo(LResult, (sizeof(int) * 1) + (sizeof(byte) * 3));
00261
00262 return LResult;
00263 }
00264
00279 public static bool SetAcknowledge(byte[] AData, ref int ARandom, ref byte AID, ref int ATimeStamp)
00280 {
00281 bool LResult = false;
00282
00283 if (AData.Length >= 3)
00284 {
00285
00286 if (new ASCIIEncoding().GetString(AData, 0, 3).Equals(AckHeader))
00287 {
00288 if (AData.Length >= (sizeof(int) * 2) + 4)
00289 {
00290
00291 ATimeStamp = BitConverter.ToInt32(AData, (sizeof(int) * 0) + 3);
00292
00293
00294 ARandom = BitConverter.ToInt32(AData, (sizeof(int) * 1) + 3);
00295 AID = AData[(sizeof(int) * 2) + 3];
00296
00297 LResult = true;
00298 }
00299 }
00300 }
00301
00302 return LResult;
00303 }
00304
00317 public static bool SetNew(byte[] AData, ref int ATimeStamp)
00318 {
00319 bool LResult = false;
00320
00321 if (AData.Length >= 3)
00322 {
00323
00324 if (new ASCIIEncoding().GetString(AData, 0, 3).Equals(NewHeader))
00325 {
00326 if (AData.Length >= sizeof(int) + 3)
00327 {
00328
00329 ATimeStamp = BitConverter.ToInt32(AData, 3);
00330
00331 LResult = true;
00332 }
00333 }
00334 }
00335
00336 return LResult;
00337 }
00338
00352 public static bool SetEnter(byte[] AData, ref int ARandom, ref int ATimeStamp)
00353 {
00354 bool LResult = false;
00355
00356 if (AData.Length >= 3)
00357 {
00358
00359 if (new ASCIIEncoding().GetString(AData, 0, 3).Equals(NtrHeader))
00360 {
00361 if (AData.Length >= (sizeof(int) * 2) + 3)
00362 {
00363
00364 ATimeStamp = BitConverter.ToInt32(AData, (sizeof(int) * 0) + 3);
00365
00366
00367 ARandom = BitConverter.ToInt32(AData, (sizeof(int) * 1) + 3);
00368
00369 LResult = true;
00370 }
00371 }
00372 }
00373
00374 return LResult;
00375 }
00376
00389 public static byte[] GetController(Controller AController, int ATimeStamp)
00390 {
00391 byte[] LResult = null;
00392
00393 if (AController != null)
00394 {
00395 LResult = new byte[(Controller.ByteSize * 1) + (sizeof(int) * 1) + (sizeof(byte) * 3)];
00396
00397 byte[] LController = AController.GetBytes();
00398 byte[] LTimeStamp = BitConverter.GetBytes(ATimeStamp);
00399
00400
00401 LResult[(Controller.ByteSize * 0) + (sizeof(int) * 0) + (sizeof(byte) * 0)] = (byte)'K';
00402 LResult[(Controller.ByteSize * 0) + (sizeof(int) * 0) + (sizeof(byte) * 1)] = (byte)'E';
00403 LResult[(Controller.ByteSize * 0) + (sizeof(int) * 0) + (sizeof(byte) * 2)] = (byte)'Y';
00404
00405
00406 LTimeStamp.CopyTo(LResult, (Controller.ByteSize * 0) + (sizeof(int) * 0) + (sizeof(byte) * 3));
00407
00408
00409 LController.CopyTo(LResult, (Controller.ByteSize * 0) + (sizeof(int) * 1) + (sizeof(byte) * 3));
00410 }
00411
00412 return LResult;
00413 }
00414
00427 public static byte[] GetCubes(Cube[] ACubes, int ATimeStamp)
00428 {
00429 int LPos = 0;
00430 byte[] LResult = null;
00431
00432 if (ACubes != null)
00433 {
00434 LResult = new byte[(Cube.ByteSize * ACubes.Length) + (sizeof(int) * 1) + (sizeof(byte) * 4)];
00435
00436 byte[] LTimeStamp = BitConverter.GetBytes(ATimeStamp);
00437
00438
00439 LResult[(Cube.ByteSize * 0) + (sizeof(int) * 0) + (sizeof(byte) * 0)] = (byte)'W';
00440 LResult[(Cube.ByteSize * 0) + (sizeof(int) * 0) + (sizeof(byte) * 1)] = (byte)'L';
00441 LResult[(Cube.ByteSize * 0) + (sizeof(int) * 0) + (sizeof(byte) * 2)] = (byte)'D';
00442
00443
00444 LTimeStamp.CopyTo(LResult, (Cube.ByteSize * 0) + (sizeof(int) * 0) + (sizeof(byte) * 3));
00445
00446
00447 LResult[(Cube.ByteSize * 0) + (sizeof(int) * 1) + (sizeof(byte) * 3)] = (byte)ACubes.Length;
00448
00449
00450 LPos = (Cube.ByteSize * 0) + (sizeof(int) * 1) + (sizeof(byte) * 4);
00451 foreach (Cube LCube in ACubes)
00452 {
00453 LCube.GetBytes().CopyTo(LResult, LPos);
00454 LPos += Cube.ByteSize;
00455 }
00456 }
00457
00458 return LResult;
00459 }
00460
00473 public static byte[] GetPlayers(Player[] APlayers, int ATimeStamp)
00474 {
00475 int LPos = 0;
00476 byte[] LResult = null;
00477
00478 if (APlayers != null)
00479 {
00480 LResult = new byte[(Player.ByteSize * APlayers.Length) + (sizeof(int) * 1) + (sizeof(byte) * 4)];
00481
00482 byte[] LTimeStamp = BitConverter.GetBytes(ATimeStamp);
00483
00484
00485 LResult[(Player.ByteSize * 0) + (sizeof(int) * 0) + (sizeof(byte) * 0)] = (byte)'P';
00486 LResult[(Player.ByteSize * 0) + (sizeof(int) * 0) + (sizeof(byte) * 1)] = (byte)'L';
00487 LResult[(Player.ByteSize * 0) + (sizeof(int) * 0) + (sizeof(byte) * 2)] = (byte)'R';
00488
00489
00490 LTimeStamp.CopyTo(LResult, (Player.ByteSize * 0) + (sizeof(int) * 0) + (sizeof(byte) * 3));
00491
00492
00493 LResult[(Player.ByteSize * 0) + (sizeof(int) * 1) + (sizeof(byte) * 3)] = (byte)APlayers.Length;
00494
00495
00496 LPos = (Player.ByteSize * 0) + (sizeof(int) * 1) + (sizeof(byte) * 4);
00497 foreach (Player LPlayer in APlayers)
00498 {
00499 LPlayer.GetBytes().CopyTo(LResult, LPos);
00500 LPos += Player.ByteSize;
00501 }
00502 }
00503
00504 return LResult;
00505 }
00506
00519 public static byte[] GetServerCubes(ServerCube[] AServerCubes, int ATimeStamp)
00520 {
00521 int LPos = 0;
00522 byte[] LResult = null;
00523
00524 if (AServerCubes != null)
00525 {
00526 LResult = new byte[(ServerCube.ByteSize * AServerCubes.Length) + (sizeof(int) * 1) + (sizeof(byte) * 4)];
00527
00528 byte[] LTimeStamp = BitConverter.GetBytes(ATimeStamp);
00529
00530
00531 LResult[(ServerCube.ByteSize * 0) + (sizeof(int) * 0) + (sizeof(byte) * 0)] = (byte)'M';
00532 LResult[(ServerCube.ByteSize * 0) + (sizeof(int) * 0) + (sizeof(byte) * 1)] = (byte)'A';
00533 LResult[(ServerCube.ByteSize * 0) + (sizeof(int) * 0) + (sizeof(byte) * 2)] = (byte)'P';
00534
00535
00536 LTimeStamp.CopyTo(LResult, (ServerCube.ByteSize * 0) + (sizeof(int) * 0) + (sizeof(byte) * 3));
00537
00538
00539 LResult[(ServerCube.ByteSize * 0) + (sizeof(int) * 1) + (sizeof(byte) * 3)] = (byte)AServerCubes.Length;
00540
00541
00542 LPos = (ServerCube.ByteSize * 0) + (sizeof(int) * 1) + (sizeof(byte) * 4);
00543 foreach (ServerCube LServerCube in AServerCubes)
00544 {
00545 LServerCube.GetBytes().CopyTo(LResult, LPos);
00546 LPos += ServerCube.ByteSize;
00547 }
00548 }
00549
00550 return LResult;
00551 }
00552
00565 public static byte[] GetServerCubesAsCubes(ServerCube[] AServerCubes, int ATimeStamp)
00566 {
00567 int LPos = 0;
00568 byte[] LResult = null;
00569
00570 if (AServerCubes != null)
00571 {
00572 LResult = new byte[(Cube.ByteSize * AServerCubes.Length) + (sizeof(int) * 1) + (sizeof(byte) * 4)];
00573
00574 byte[] LTimeStamp = BitConverter.GetBytes(ATimeStamp);
00575
00576
00577 LResult[(Cube.ByteSize * 0) + (sizeof(int) * 0) + (sizeof(byte) * 0)] = (byte)'W';
00578 LResult[(Cube.ByteSize * 0) + (sizeof(int) * 0) + (sizeof(byte) * 1)] = (byte)'L';
00579 LResult[(Cube.ByteSize * 0) + (sizeof(int) * 0) + (sizeof(byte) * 2)] = (byte)'D';
00580
00581
00582 LTimeStamp.CopyTo(LResult, (Cube.ByteSize * 0) + (sizeof(int) * 0) + (sizeof(byte) * 3));
00583
00584
00585 LResult[(Cube.ByteSize * 0) + (sizeof(int) * 1) + (sizeof(byte) * 3)] = (byte)AServerCubes.Length;
00586
00587
00588 LPos = (Cube.ByteSize * 0) + (sizeof(int) * 1) + (sizeof(byte) * 4);
00589 foreach (ServerCube LServerCube in AServerCubes)
00590 {
00591 LServerCube.Cube.GetBytes().CopyTo(LResult, LPos);
00592 LPos += Cube.ByteSize;
00593 }
00594 }
00595
00596 return LResult;
00597 }
00598
00612 public static bool SetController(byte[] AData, ref Controller AController, ref int ATimeStamp)
00613 {
00614 bool LResult = false;
00615
00616 if (AData.Length >= 3)
00617 {
00618
00619 if (new ASCIIEncoding().GetString(AData, 0, 3).Equals(KeyHeader))
00620 {
00621 if (AData.Length >= Controller.ByteSize + sizeof(int) + 3)
00622 {
00623
00624 ATimeStamp = BitConverter.ToInt32(AData, 3);
00625
00626
00627 if (AController == null)
00628 {
00629 AController = new Controller();
00630 }
00631 AController.SetBytes(AData, sizeof(int) + 3);
00632
00633 LResult = true;
00634 }
00635 }
00636 }
00637
00638 return LResult;
00639 }
00640
00654 public static bool SetCubes(byte[] AData, ref Cube[] ACubes, ref int ATimeStamp)
00655 {
00656 bool LResult = false;
00657
00658 if (AData.Length >= 3)
00659 {
00660
00661 if (new ASCIIEncoding().GetString(AData, 0, 3).Equals(WldHeader))
00662 {
00663 if (AData.Length >= sizeof(int) + 4)
00664 {
00665
00666 ATimeStamp = BitConverter.ToInt32(AData, 3);
00667
00668
00669 byte LLength = AData[sizeof(int) + 3];
00670 if (AData.Length >= (Cube.ByteSize * LLength) + sizeof(int) + 4)
00671 {
00672
00673 if ((ACubes == null) || (ACubes.Length != LLength))
00674 {
00675 ACubes = new Cube[LLength];
00676
00677 for (int LIndex = 0; LIndex < LLength; LIndex++)
00678 {
00679 ACubes[LIndex] = new Cube();
00680 }
00681 }
00682
00683 int LPos = sizeof(int) + 4;
00684 foreach (Cube LCube in ACubes)
00685 {
00686 LCube.SetBytes(AData, LPos);
00687 LPos += Cube.ByteSize;
00688 }
00689
00690 LResult = true;
00691 }
00692 }
00693 }
00694 }
00695
00696 return LResult;
00697 }
00698
00712 public static bool SetPlayers(byte[] AData, ref Player[] APlayers, ref int ATimeStamp)
00713 {
00714 bool LResult = false;
00715
00716 if (AData.Length >= 3)
00717 {
00718
00719 if (new ASCIIEncoding().GetString(AData, 0, 3).Equals(PlrHeader))
00720 {
00721 if (AData.Length >= sizeof(int) + 4)
00722 {
00723
00724 ATimeStamp = BitConverter.ToInt32(AData, 3);
00725
00726
00727 byte LLength = AData[sizeof(int) + 3];
00728 if (AData.Length >= (Player.ByteSize * LLength) + sizeof(int) + 4)
00729 {
00730
00731 if ((APlayers == null) || (APlayers.Length != LLength))
00732 {
00733 APlayers = new Player[LLength];
00734
00735 for (int LIndex = 0; LIndex < LLength; LIndex++)
00736 {
00737 APlayers[LIndex] = new Player();
00738 }
00739 }
00740
00741 int LPos = sizeof(int) + 4;
00742 foreach (Player LPlayer in APlayers)
00743 {
00744 LPlayer.SetBytes(AData, LPos);
00745 LPos += Player.ByteSize;
00746 }
00747
00748 LResult = true;
00749 }
00750 }
00751 }
00752 }
00753
00754 return LResult;
00755 }
00756
00770 public static bool SetServerCubes(byte[] AData, ref ServerCube[] AServerCubes, ref int ATimeStamp)
00771 {
00772 bool LResult = false;
00773
00774 if (AData.Length >= 3)
00775 {
00776
00777 if (new ASCIIEncoding().GetString(AData, 0, 3).Equals(MapHeader))
00778 {
00779 if (AData.Length >= sizeof(int) + 4)
00780 {
00781
00782 ATimeStamp = BitConverter.ToInt32(AData, 3);
00783
00784 byte LLength = AData[sizeof(int) + 3];
00785 if (AData.Length >= (ServerCube.ByteSize * LLength) + sizeof(int) + 4)
00786 {
00787
00788 if ((AServerCubes == null) || (AServerCubes.Length != LLength))
00789 {
00790 AServerCubes = new ServerCube[LLength];
00791
00792 for (int LIndex = 0; LIndex < LLength; LIndex++)
00793 {
00794 AServerCubes[LIndex] = new ServerCube();
00795 }
00796 }
00797
00798 int LPos = sizeof(int) + 4;
00799 foreach (ServerCube LServerCube in AServerCubes)
00800 {
00801 LServerCube.SetBytes(AData, LPos);
00802 LPos += ServerCube.ByteSize;
00803 }
00804
00805 LResult = true;
00806 }
00807 }
00808 }
00809 }
00810
00811 return LResult;
00812 }
00813 }
00814 }