Guest User

Untitled

a guest
Sep 30th, 2016
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. #include<iostream>
  2. #include<queue>
  3. #include<string>
  4. #include<cstdlib>
  5.  
  6. #include<boost/thread.hpp>
  7. #include<boost/bind.hpp>
  8. #include<boost/asio.hpp>
  9. #include<boost/asio/ip/tcp.hpp>
  10. #include<boost/algorithm/string.hpp>
  11.  
  12. using namespace std;
  13. using namespace boost;
  14. using namespace boost::asio;
  15. using namespace boost::asio::ip;
  16.  
  17. typedef boost::shared_ptr<tcp::socket> socket_ptr;
  18. typedef boost::shared_ptr<string> string_ptr;
  19. typedef boost::shared_ptr< queue<string_ptr> > messageQueue_ptr;
  20.  
  21. io_service service;
  22. messageQueue_ptr messageQueue(new queue<string_ptr>);
  23. tcp::endpoint ep(ip::address::from_string("127.0.0.1"), 8001);
  24. const int inputSize = 256;
  25. string_ptr promptCpy;
  26.  
  27. // Function Prototypes
  28. bool isOwnMessage(string_ptr);
  29. void displayLoop(socket_ptr);
  30. void inboundLoop(socket_ptr, string_ptr);
  31. void writeLoop(socket_ptr, string_ptr);
  32. string* buildPrompt();
  33. // End of Function Prototypes
  34.  
  35. int main(int argc, char** argv)
  36. {
  37. try
  38. {
  39. boost::thread_group threads;
  40. socket_ptr sock(new tcp::socket(service));
  41.  
  42. string_ptr prompt( buildPrompt() );
  43. promptCpy = prompt;
  44.  
  45. sock->connect(ep);
  46.  
  47. cout << "Welcome to the ChatServer\nType \"exit\" to quit" << endl;
  48.  
  49. threads.create_thread(boost::bind(displayLoop, sock));
  50. threads.create_thread(boost::bind(inboundLoop, sock, prompt));
  51. threads.create_thread(boost::bind(writeLoop, sock, prompt));
  52.  
  53. threads.join_all();
  54. }
  55. catch(std::exception& e)
  56. {
  57. cerr << e.what() << endl;
  58. }
  59.  
  60. puts("Press any key to continue...");
  61. getc(stdin);
  62. return EXIT_SUCCESS;
  63. }
  64.  
  65. string* buildPrompt()
  66. {
  67. const int inputSize = 256;
  68. char inputBuf[inputSize] = {0};
  69. char nameBuf[inputSize] = {0};
  70. string* prompt = new string(": ");
  71.  
  72. cout << "Please input a new username: ";
  73. cin.getline(nameBuf, inputSize);
  74. *prompt = (string)nameBuf + *prompt;
  75. boost::algorithm::to_lower(*prompt);
  76.  
  77. return prompt;
  78. }
  79.  
  80. void inboundLoop(socket_ptr sock, string_ptr prompt)
  81. {
  82. int bytesRead = 0;
  83. char readBuf[1024] = {0};
  84.  
  85. for(;;)
  86. {
  87. if(sock->available())
  88. {
  89. bytesRead = sock->read_some(buffer(readBuf, inputSize));
  90. string_ptr msg(new string(readBuf, bytesRead));
  91.  
  92. messageQueue->push(msg);
  93. }
  94.  
  95. boost::this_thread::sleep( boost::posix_time::millisec(1000));
  96. }
  97. }
  98.  
  99. void writeLoop(socket_ptr sock, string_ptr prompt)
  100. {
  101. char inputBuf[inputSize] = {0};
  102. string inputMsg;
  103.  
  104. for(;;)
  105. {
  106. cin.getline(inputBuf, inputSize);
  107. inputMsg = *prompt + (string)inputBuf + '\n';
  108.  
  109. if(!inputMsg.empty())
  110. {
  111. sock->write_some(buffer(inputMsg, inputSize));
  112. }
  113.  
  114. if(inputMsg.find("exit") != string::npos)
  115. exit(1);
  116.  
  117. inputMsg.clear();
  118. memset(inputBuf, 0, inputSize);
  119. }
  120. }
  121.  
  122. void displayLoop(socket_ptr sock)
  123. {
  124. for(;;)
  125. {
  126. if(!messageQueue->empty())
  127. {
  128. if(!isOwnMessage(messageQueue->front()))
  129. {
  130. cout << "\n" + *(messageQueue->front());
  131. }
  132.  
  133. messageQueue->pop();
  134. }
  135.  
  136. boost::this_thread::sleep( boost::posix_time::millisec(1000));
  137. }
  138. }
  139.  
  140. bool isOwnMessage(string_ptr message)
  141. {
  142. if(message->find(*promptCpy) != string::npos)
  143. return true;
  144. else
  145. return false;
  146. }
Add Comment
Please, Sign In to add comment