PolyServer.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyGlobals.h"
  21. #include "PolyPeer.h"
  22. #include "PolyEvent.h"
  23. #include "PolyServerWorld.h"
  24. #include <vector>
  25. using std::vector;
  26. namespace Polycode {
  27. class _PolyExport ServerClientEvent : public Event {
  28. public:
  29. ServerClientEvent() {}
  30. ~ServerClientEvent() {}
  31. char *data;
  32. unsigned int dataSize;
  33. unsigned short dataType;
  34. static const int EVENTBASE_SERVERCLIENTEVENT = 0x780;
  35. static const int EVENT_CLIENT_DATA = EVENTBASE_SERVERCLIENTEVENT+0;
  36. };
  37. class _PolyExport ServerClient : public EventDispatcher {
  38. public:
  39. ServerClient();
  40. ~ServerClient();
  41. void handlePacket(Packet *packet);
  42. unsigned int clientID;
  43. PeerConnection *connection;
  44. };
  45. class _PolyExport ServerEvent : public Event {
  46. public:
  47. ServerEvent(){client = NULL; }
  48. ~ServerEvent(){}
  49. ServerClient *client;
  50. static const int EVENTBASE_SERVEREVENT = 0x700;
  51. static const int EVENT_CLIENT_CONNECTED = EVENTBASE_SERVEREVENT+0;
  52. static const int EVENT_CLIENT_DATA = EVENTBASE_SERVEREVENT+1;
  53. static const int EVENT_CLIENT_DISCONNECTED = EVENTBASE_SERVEREVENT+2;
  54. // Notice also the SERVERCLIENTEVENT above, which starts with 0x780.
  55. };
  56. class _PolyExport Server : public Peer {
  57. public:
  58. Server(unsigned int port, unsigned int rate, ServerWorld *world = NULL);
  59. ~Server();
  60. void DisconnectClient(ServerClient *client);
  61. void handleEvent(Event *event);
  62. void handlePeerConnection(PeerConnection *connection);
  63. ServerClient *getConnectedClient(PeerConnection *connection);
  64. void sendReliableDataToClient(ServerClient *client, char *data, unsigned int size, unsigned short type);
  65. void sendReliableDataToAllClients(char *data, unsigned int size, unsigned short type);
  66. void handlePacket(Packet *packet, PeerConnection *connection);
  67. protected:
  68. Timer *rateTimer;
  69. ServerWorld *world;
  70. vector<ServerClient*> clients;
  71. };
  72. }