commit 1434de93cd9af7f8931e7812cfa206fd7d8d61fc
Author: Eliška Mourycová <eliska.mourycova@seznam.cz>
Date:   Wed Apr 14 17:54:15 2021 +0200

    Re #8619. Connection handling improvements + simple parser of sent messages.

diff --git a/ClientConnectionTest/ClientTest/.vs/ClientTest/v16/.suo b/ClientConnectionTest/ClientTest/.vs/ClientTest/v16/.suo
index 063d377..85ef11a 100644
Binary files a/ClientConnectionTest/ClientTest/.vs/ClientTest/v16/.suo and b/ClientConnectionTest/ClientTest/.vs/ClientTest/v16/.suo differ
diff --git a/ClientConnectionTest/ClientTest/ClientTest/Program.cs b/ClientConnectionTest/ClientTest/ClientTest/Program.cs
index 8d2627a..16f10d6 100644
--- a/ClientConnectionTest/ClientTest/ClientTest/Program.cs
+++ b/ClientConnectionTest/ClientTest/ClientTest/Program.cs
@@ -226,15 +226,17 @@ class Program
             //---send the text---
             Console.WriteLine("Sending : " + textToSend);
             nwStream.Write(bytesToSend, 0, bytesToSend.Length);
-			if (textToSend.Equals("bye!"))
-			{
-                break;
-			}
+			
 
             //---read back the text---
             byte[] bytesToRead = new byte[client.ReceiveBufferSize];
             int bytesRead = nwStream.Read(bytesToRead, 0, client.ReceiveBufferSize);
             Console.WriteLine("Received : " + Encoding.ASCII.GetString(bytesToRead, 0, bytesRead));
+
+            if (textToSend.Equals("bye!"))
+            {
+                break;
+            }
         }
         
         client.Close();
diff --git a/ClientConnectionTest/ClientTest/ClientTest/bin/Debug/net5.0/ClientTest.dll b/ClientConnectionTest/ClientTest/ClientTest/bin/Debug/net5.0/ClientTest.dll
index d710165..c4f34be 100644
Binary files a/ClientConnectionTest/ClientTest/ClientTest/bin/Debug/net5.0/ClientTest.dll and b/ClientConnectionTest/ClientTest/ClientTest/bin/Debug/net5.0/ClientTest.dll differ
diff --git a/ClientConnectionTest/ClientTest/ClientTest/bin/Debug/net5.0/ClientTest.pdb b/ClientConnectionTest/ClientTest/ClientTest/bin/Debug/net5.0/ClientTest.pdb
index 93c672a..0388651 100644
Binary files a/ClientConnectionTest/ClientTest/ClientTest/bin/Debug/net5.0/ClientTest.pdb and b/ClientConnectionTest/ClientTest/ClientTest/bin/Debug/net5.0/ClientTest.pdb differ
diff --git a/ClientConnectionTest/ClientTest/ClientTest/obj/Debug/net5.0/ClientTest.csprojAssemblyReference.cache b/ClientConnectionTest/ClientTest/ClientTest/obj/Debug/net5.0/ClientTest.csprojAssemblyReference.cache
index 9ffa8f8..b8faec7 100644
Binary files a/ClientConnectionTest/ClientTest/ClientTest/obj/Debug/net5.0/ClientTest.csprojAssemblyReference.cache and b/ClientConnectionTest/ClientTest/ClientTest/obj/Debug/net5.0/ClientTest.csprojAssemblyReference.cache differ
diff --git a/ClientConnectionTest/ClientTest/ClientTest/obj/Debug/net5.0/ClientTest.dll b/ClientConnectionTest/ClientTest/ClientTest/obj/Debug/net5.0/ClientTest.dll
index d710165..c4f34be 100644
Binary files a/ClientConnectionTest/ClientTest/ClientTest/obj/Debug/net5.0/ClientTest.dll and b/ClientConnectionTest/ClientTest/ClientTest/obj/Debug/net5.0/ClientTest.dll differ
diff --git a/ClientConnectionTest/ClientTest/ClientTest/obj/Debug/net5.0/ClientTest.pdb b/ClientConnectionTest/ClientTest/ClientTest/obj/Debug/net5.0/ClientTest.pdb
index 93c672a..0388651 100644
Binary files a/ClientConnectionTest/ClientTest/ClientTest/obj/Debug/net5.0/ClientTest.pdb and b/ClientConnectionTest/ClientTest/ClientTest/obj/Debug/net5.0/ClientTest.pdb differ
diff --git a/Server/.vs/ServerApp/v16/.suo b/Server/.vs/ServerApp/v16/.suo
index 6b802ff..4b6da0c 100644
Binary files a/Server/.vs/ServerApp/v16/.suo and b/Server/.vs/ServerApp/v16/.suo differ
diff --git a/Server/ServerApp/Connection/SocketListener.cs b/Server/ServerApp/Connection/SocketListener.cs
index 7c9ce76..c9fe8de 100644
--- a/Server/ServerApp/Connection/SocketListener.cs
+++ b/Server/ServerApp/Connection/SocketListener.cs
@@ -31,7 +31,7 @@ namespace Connection
         {
         }
 
-        public static void StartListening()
+        public void StartListening()
         {
             // Data buffer for incoming data.
             byte[] bytes = new Byte[1024];
@@ -78,10 +78,11 @@ namespace Connection
 
         }
 
-        public static void AcceptCallback(IAsyncResult ar)
+        public void AcceptCallback(IAsyncResult ar)
         {
             // Signal the main thread to continue.
             allDone.Set();
+            Console.WriteLine("Accepted a connection.");
 
             // Get the socket that handles the client request.
             Socket listener = (Socket)ar.AsyncState;
@@ -94,8 +95,12 @@ namespace Connection
             
         }
 
-        public static void ReadCallback(IAsyncResult ar)
+        public void ReadCallback(IAsyncResult ar)
         {
+
+            
+
+
             String content = String.Empty;
 
             // Retrieve the state object and the handler socket
@@ -103,7 +108,14 @@ namespace Connection
             StateObject state = (StateObject)ar.AsyncState;
             Socket handler = state.workSocket;
 
-            // Read data from the client socket. 
+            if (!SocketConnected(handler))
+			{
+                Console.WriteLine("Closing connection - unexpected...");
+                CloseConnection(handler);
+                return;
+            }
+
+                // Read data from the client socket. 
             int bytesRead = handler.EndReceive(ar);
 
             if (bytesRead > 0)
@@ -119,9 +131,22 @@ namespace Connection
                     // All the data has been read from the 
                     // client. Display it on the console.
                     Console.WriteLine("Read {0} bytes from socket. \n Data : {1}", content.Length, content);
+
+                    content = ParseMessageAndBuildAnswer(content);
+                    if (content.Equals("bye!"))
+                    {
+                        Console.WriteLine("Closing connection - expected...");
+                        //handler.Shutdown(SocketShutdown.Both);
+                        //handler.Close();
+                        CloseConnection(handler);
+                        return;
+                    }
+
                     // Echo the data back to the client.
                     Send(handler, content);
+					
 
+                    state.sb.Clear();
                     handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
                 }
                 else
@@ -132,7 +157,7 @@ namespace Connection
             }
         }
 
-        private static void Send(Socket handler, String data)
+        private void Send(Socket handler, String data)
         {
             // Convert the string data to byte data using ASCII encoding.
             byte[] byteData = Encoding.ASCII.GetBytes(data);
@@ -141,7 +166,7 @@ namespace Connection
             handler.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), handler);
         }
 
-        private static void SendCallback(IAsyncResult ar)
+        private void SendCallback(IAsyncResult ar)
         {
             try
             {
@@ -152,6 +177,7 @@ namespace Connection
                 int bytesSent = handler.EndSend(ar);
                 Console.WriteLine("Sent {0} bytes to client.", bytesSent);
 
+
                 //handler.Shutdown(SocketShutdown.Both);
                 //handler.Close();
 
@@ -161,5 +187,55 @@ namespace Connection
                 Console.WriteLine(e.ToString());
             }
         }
+
+
+
+
+
+
+
+        private string ParseMessageAndBuildAnswer(string message)
+		{
+            string ans = "";
+
+			if (message.Equals("hello!"))
+			{
+                ans = "hello to you too.";
+			}
+            else if (message.Equals("weather!"))
+			{
+                ans = "weather will be nice";
+            }
+            else if (message.Equals("bye!"))
+			{
+                ans = "bye!";
+			}
+            else
+			{
+                // here bye too, if we want to disconnect clients who don't follow protocol
+                ans = "bye!";//"i didnt understand that";
+
+            }
+
+            return ans;
+		}
+
+
+        private void CloseConnection(Socket s)
+		{
+            s.Shutdown(SocketShutdown.Both);
+            s.Close();
+        }
+
+        private bool SocketConnected(Socket s)
+        {
+            bool part1 = s.Poll(1000, SelectMode.SelectRead);
+            bool part2 = (s.Available == 0);
+            if (part1 && part2)
+                return false;
+            else
+                return true;
+        }
     }
+   
 }
diff --git a/Server/ServerApp/Program.cs b/Server/ServerApp/Program.cs
index 7e23f02..7cbf781 100644
--- a/Server/ServerApp/Program.cs
+++ b/Server/ServerApp/Program.cs
@@ -36,7 +36,8 @@ namespace ServerApp
 
 
             // test - connection:
-            AsynchronousSocketListener.StartListening();
+            AsynchronousSocketListener asl = new AsynchronousSocketListener();
+            asl.StartListening();
            
 
             Console.ReadLine();
diff --git a/Server/ServerApp/bin/Debug/ServerApp.exe b/Server/ServerApp/bin/Debug/ServerApp.exe
index 76e523e..949dd33 100644
Binary files a/Server/ServerApp/bin/Debug/ServerApp.exe and b/Server/ServerApp/bin/Debug/ServerApp.exe differ
diff --git a/Server/ServerApp/bin/Debug/ServerApp.pdb b/Server/ServerApp/bin/Debug/ServerApp.pdb
index 98db335..148683d 100644
Binary files a/Server/ServerApp/bin/Debug/ServerApp.pdb and b/Server/ServerApp/bin/Debug/ServerApp.pdb differ
diff --git a/Server/ServerApp/obj/Debug/ServerApp.csprojAssemblyReference.cache b/Server/ServerApp/obj/Debug/ServerApp.csprojAssemblyReference.cache
index 2ed24bb..31a4ac6 100644
Binary files a/Server/ServerApp/obj/Debug/ServerApp.csprojAssemblyReference.cache and b/Server/ServerApp/obj/Debug/ServerApp.csprojAssemblyReference.cache differ
diff --git a/Server/ServerApp/obj/Debug/ServerApp.exe b/Server/ServerApp/obj/Debug/ServerApp.exe
index 76e523e..949dd33 100644
Binary files a/Server/ServerApp/obj/Debug/ServerApp.exe and b/Server/ServerApp/obj/Debug/ServerApp.exe differ
diff --git a/Server/ServerApp/obj/Debug/ServerApp.pdb b/Server/ServerApp/obj/Debug/ServerApp.pdb
index 98db335..148683d 100644
Binary files a/Server/ServerApp/obj/Debug/ServerApp.pdb and b/Server/ServerApp/obj/Debug/ServerApp.pdb differ
