PolyServer.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. /**
  57. * A network server, accepting incoming connections and keeping track of connected clients.
  58. *
  59. * As the Peer class already provides all connectivity functionality required, the Server class
  60. * merely provides another abstraction layer to treat clients separately from mere connections.
  61. *
  62. * The Server will send the data provided by the ServerWorld class to all clients at defined rate and dispatch events when clients connect, disconnect and send data.
  63. */
  64. class _PolyExport Server : public Peer {
  65. public:
  66. /**
  67. * Constructor.
  68. * @param port The local port to listen for client connections on.
  69. * @param rate How many times per second to send out server data to clients.
  70. * @param world An instance of the server data provider. @see ServerWorld
  71. */
  72. Server(unsigned int port, unsigned int rate, ServerWorld *world = NULL);
  73. ~Server();
  74. void handlePacket(Packet *packet, PeerConnection *connection);
  75. void handleEvent(Event *event);
  76. void handlePeerConnection(PeerConnection *connection);
  77. void DisconnectClient(ServerClient *client);
  78. /**
  79. * Get a connected client from its associated peer connection, if any.
  80. * @param connection The PeerConnection through which we're communicating
  81. * with the client to obtain.
  82. * @return The connected client or NULL if doesn't exist.
  83. */
  84. ServerClient *getConnectedClient(PeerConnection *connection);
  85. /**
  86. * @see Peer::sendReliableData
  87. */
  88. void sendReliableDataToClient(ServerClient *client, char *data, unsigned int size, unsigned short type);
  89. /**
  90. * @see Peer::sendReliableDataToAll
  91. */
  92. void sendReliableDataToAllClients(char *data, unsigned int size, unsigned short type);
  93. protected:
  94. Timer *rateTimer;
  95. ServerWorld *world;
  96. vector<ServerClient*> clients;
  97. };
  98. }