PageRenderTime 12ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/platform/unix/scaffold/socket.d

http://github.com/wilkie/djehuty
D | 169 lines | 111 code | 43 blank | 15 comment | 11 complexity | f9698b54191d341a834564d6e2738ece MD5 | raw file
  1. /*
  2. * socket.d
  3. *
  4. * This Scaffold holds the Socket implementations for the Linux platform
  5. *
  6. * Author: Dave Wilkinson
  7. *
  8. */
  9. module scaffold.socket;
  10. import platform.vars.socket;
  11. import platform.unix.common;
  12. import core.string;
  13. import core.main;
  14. import core.definitions;
  15. import io.console;
  16. // SOCKET
  17. // phobos (in, at least, GDC 0.24) has incorrect addrinfo
  18. struct addrinfo_fix {
  19. int ai_flags; /* AI_PASSIVE, AI_CANONNAME */
  20. int ai_family; /* PF_xxx */
  21. int ai_socktype; /* SOCK_xxx */
  22. int ai_protocol; /* IPPROTO_xxx for IPv4 and IPv6 */
  23. size_t ai_addrlen; /* length of ai_addr */
  24. sockaddr *ai_addr; /* binary address */
  25. char *ai_canonname; /* canonical name for host */
  26. addrinfo *ai_next; /* next structure in linked list */
  27. }
  28. extern(C) char* gai_strerror(int ecode);
  29. bool SocketOpen(ref SocketPlatformVars sockVars, ref string hostname, ref ushort port)
  30. {
  31. addrinfo_fix* result;
  32. int error;
  33. addrinfo_fix hints;
  34. hints.ai_family = AF_UNSPEC;
  35. hints.ai_protocol = IPPROTO_TCP;
  36. hints.ai_socktype = SOCK_STREAM;
  37. // make C style strings
  38. string portstr = toStr(port) ~ '\0';
  39. hostname = hostname.dup ~ '\0';
  40. error = getaddrinfo( hostname.ptr, portstr.ptr, cast(addrinfo*)&hints, cast(addrinfo**)&result );
  41. if ( 0 != error )
  42. {
  43. // Error Connecting
  44. Console.putln("getaddrinfo, error", portstr.length, ",",portstr);
  45. //printf("%s\n", gai_strerror(error));
  46. return false;
  47. }
  48. sockVars.m_skt = socket(result.ai_family, result.ai_socktype, result.ai_protocol);
  49. if (sockVars.m_skt == -1)
  50. {
  51. //file an error event
  52. Console.putln("socket, error");
  53. return false;
  54. }
  55. int iResult = connect(sockVars.m_skt, result.ai_addr, result.ai_addrlen);
  56. if (iResult == -1)
  57. {
  58. //file an error event
  59. close(sockVars.m_skt);
  60. sockVars.m_skt = 0;
  61. Console.putln("connect, error");
  62. return false;
  63. }
  64. Console.putln("connected");
  65. return true;
  66. }
  67. bool SocketBind(ref SocketPlatformVars sockVars, ref ushort port)
  68. {
  69. return false;
  70. }
  71. bool SocketListen(ref SocketPlatformVars sockVars)
  72. {
  73. return false;
  74. }
  75. bool SocketAccept(ref SocketPlatformVars sockVars)
  76. {
  77. return false;
  78. }
  79. void SocketClose(ref SocketPlatformVars sockVars)
  80. {
  81. shutdown(sockVars.m_skt, 2);
  82. close(sockVars.m_skt);
  83. }
  84. bool SocketRead(ref SocketPlatformVars sockVars, ubyte* buffer, ulong len)
  85. {
  86. ulong progress = 0;
  87. ulong ret = 0;
  88. ulong amt = len;
  89. int cur_amt;
  90. ubyte* cur = buffer;
  91. while (progress < len)
  92. {
  93. cur_amt = cast(int)(amt & 0x7FFFFFFF);
  94. ret = recv(sockVars.m_skt, cur, cur_amt, 0);
  95. if (ret <= 0) { return false; }
  96. progress += ret;
  97. amt -= ret;
  98. cur += ret;
  99. }
  100. return true;
  101. }
  102. ulong SocketReadAvailable(ref SocketPlatformVars sockVars, ubyte* buffer, ulong len)
  103. {
  104. int cur_amt = cast(int)(len & 0x7FFFFFFF);
  105. ulong ret = recv(sockVars.m_skt, buffer, cur_amt, 0);
  106. return ret;
  107. }
  108. bool SocketWrite(ref SocketPlatformVars sockVars, ubyte* buffer, ulong len)
  109. {
  110. ulong progress = 0;
  111. ulong ret = 0;
  112. ulong amt = len;
  113. int cur_amt;
  114. ubyte* cur = buffer;
  115. while (progress < len)
  116. {
  117. cur_amt = cast(int)(amt & 0x7FFFFFFF);
  118. ret = send(sockVars.m_skt, cur, cur_amt, 0);
  119. if (ret <= 0) { return false; }
  120. progress += ret;
  121. amt -= ret;
  122. cur += ret;
  123. if (ret <= 0) { return false; }
  124. }
  125. return true;
  126. }