PageRenderTime 27ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/io/socket.d

http://github.com/wilkie/djehuty
D | 234 lines | 134 code | 76 blank | 24 comment | 16 complexity | 046ceaa6069e667bcbc006a8ce4f7442 MD5 | raw file
  1. module io.socket;
  2. import core.stream;
  3. import core.string;
  4. import core.definitions;
  5. import platform.vars.socket;
  6. import scaffold.socket;
  7. // Section: Core/Streams
  8. // Description: This class wraps networking calls and represents the information stream as a Stream class. This is a low-level implementation of a socket. Note: no rewind or seek operations will have any affect.
  9. class Socket : Stream {
  10. ~this() {
  11. close();
  12. }
  13. // Methods
  14. // Inherited Functionality
  15. alias Stream.write write;
  16. alias Stream.append append;
  17. alias Stream.read read;
  18. // Core Functionality
  19. // Description: Will open a connection with the host on the port given by the parameters.
  20. // hostname: The name of the host to connect.
  21. // port: The port to connect through.
  22. // Returns: Will return true when the connect is made and false if the connection cannot be made.
  23. bool connect(string hostname, ushort port) {
  24. _hostname = hostname.dup;
  25. _port = port;
  26. _pos = null;
  27. _curpos = 0;
  28. bool r = SocketOpen(_pfvars, _hostname, port);
  29. if (!r) {
  30. return false;
  31. }
  32. _inited = true;
  33. return true;
  34. }
  35. // Description: Binds to a port, causes the socket to act as a server.
  36. // port: The port to listen for connection requests.
  37. // Returns: Will return false on failure.
  38. bool bind(ushort port) {
  39. _hostname = null;
  40. _port = port;
  41. _pos = null;
  42. _curpos = 0;
  43. bool r = SocketBind(_pfvars, port);
  44. if (!r) {
  45. return false;
  46. }
  47. _inited = true;
  48. return true;
  49. }
  50. // Description: Will listen to a binded port. Use bind() prior to this. It will not return until a connection is requested from a client.
  51. // Returns: Will return false on failure.
  52. bool listen() {
  53. return SocketListen(_pfvars);
  54. }
  55. // Description: Will accept a connection request from a client. Do this after returning from a Listen() call without failure.
  56. // Returns: Will return false on failure.
  57. bool accept() {
  58. return SocketAccept(_pfvars);
  59. }
  60. // Description: Will close the connection, if open. This is also done upon deconstruction of the class, for instance when it is garbage collected.
  61. void close() {
  62. if (_inited) {
  63. SocketClose(_pfvars);
  64. _inited = false;
  65. _hostname = null;
  66. }
  67. }
  68. // read
  69. override bool read(void* buffer, uint len) {
  70. return SocketRead(_pfvars, cast(ubyte*)buffer, len);
  71. }
  72. override bool read(Stream stream, uint len) {
  73. if (_curpos + len > _length) {
  74. return false;
  75. }
  76. stream.write(this, len);
  77. _curpos += len;
  78. return true;
  79. }
  80. override ulong readAny(void* buffer, uint len) {
  81. if (len == 0) { return 0; }
  82. return SocketReadAvailable(_pfvars, cast(ubyte*)buffer, len);
  83. }
  84. override ulong readAny(Stream stream, uint len) {
  85. if (len == 0) { return 0; }
  86. ubyte buffer[] = new ubyte[len];
  87. len = cast(uint)SocketReadAvailable(_pfvars, buffer.ptr, len);
  88. if (len != 0) {
  89. stream.write(buffer.ptr, len);
  90. }
  91. return len;
  92. }
  93. // write
  94. override bool write(ubyte* bytes, uint len) {
  95. if (len <= 0) { return false;}
  96. SocketWrite(_pfvars, bytes, len);
  97. return true;
  98. }
  99. override bool write(Stream stream, uint len) {
  100. if (len <= 0) { return false;}
  101. ubyte buffer[] = new ubyte[len];
  102. stream.read(&buffer[0], len);
  103. SocketWrite(_pfvars, &buffer[0], len);
  104. return true;
  105. }
  106. // append
  107. override bool append(ubyte* bytes, uint len) {
  108. if (len <= 0) { return false;}
  109. SocketWrite(_pfvars, bytes, len);
  110. return true;
  111. }
  112. override bool append(Stream stream, uint len) {
  113. if (len <= 0) { return false;}
  114. ubyte buffer[] = new ubyte[len];
  115. stream.read(&buffer[0], len);
  116. SocketWrite(_pfvars, &buffer[0], len);
  117. return true;
  118. }
  119. // rewind
  120. override void rewind() {
  121. }
  122. override bool rewind(ulong amount) {
  123. return true;
  124. }
  125. override ulong rewindAny(ulong amount) {
  126. return amount;
  127. }
  128. // skip
  129. override void skip() {
  130. }
  131. override bool skip(ulong amount) {
  132. return true;
  133. }
  134. override ulong skipAny(ulong amount) {
  135. return amount;
  136. }
  137. // Description: Will return the String representing the host currently open, or null for when there is no open socket.
  138. // Returns: The String of the host.
  139. string hostname() {
  140. if (_inited) {
  141. return _hostname.dup;
  142. }
  143. return null;
  144. }
  145. ulong port() {
  146. if (_inited) { return 0; }
  147. return _port;
  148. }
  149. protected:
  150. bool _inited = false;
  151. string _hostname = null;
  152. ulong _port = 0;
  153. SocketPlatformVars _pfvars;
  154. }
  155. class SocketReader : Socket {
  156. }
  157. class SocketWriter : Socket {
  158. }