private static void ConnectToLoadBalancer(IPEndPoint localEndPoint)
        {
            try
            {
                SendMsg       send;
                NetworkStream networkStream;
                log.Write("Connecting with the Load Balancer...........");
                loadBlncSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                loadBlncSock.Bind(localEndPoint);
                loadBlncSock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
                tcpClient = new TcpClient()
                {
                    Client = loadBlncSock
                };
                //LBStream = default(NetworkStream);
                IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(Constants.loadBLNCIP), 9000);
                //IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9000);
                loadBlncSock.Connect(remoteEndPoint);
                networkStream = new NetworkStream(loadBlncSock);
                log.Write("Connected successfully with the Load Balancer, Starting thread for sending Number of connected clients");
                Thread sendSizeThread = new Thread(() => SendClientSize(loadBlncSock));
                sendSizeThread.Start();
                log.Write("Starting thread for Listening from Load Balancer");
                //Thread listenLoadBalnc = new Thread(() => ListenLoadBalnc(networkStream));
                //listenLoadBalnc.Start();

                Thread recServerFromLB = new Thread(() => RecServerFromLB(networkStream));
                recServerFromLB.Start();

                send = new SendMsg();
                send.IntitializeQueue();
            }
            catch (Exception ex)
            {
                log.Write("Exception in ConnectToLoadBalancer " + ex);
                Console.WriteLine("Exception in ConnectToLoadBalancer " + ex);
            }
        }
        private async void Listen()
        {
            NetworkStream networkStream = null;

            try
            {
                networkStream = clientSocket.GetStream();
                clientSocket.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
                Byte[]  buffer = new Byte[4];
                int     msgSize = 0, sizeIndex = 0, remainingSize = 4, startIndex = 0;
                SendMsg send = new SendMsg();
                while (true)
                {
                    socketWait.WaitOne();
                    try
                    {
                        sizeIndex += await networkStream.ReadAsync(buffer, startIndex, remainingSize);
                    }
                    catch (ObjectDisposedException)
                    {
                        log.Write("Socket Connection with client " + clientID + " has crashed. Going to close it EXCEP");
                        networkStream.Close();
                        clientSocket.Close();
                        SendRemoveClientMSG();
                        socketWait.Set();
                        remainingSize = 4;
                        startIndex    = sizeIndex = msgSize = 0;
                        break;
                    }
                    catch (IOException)
                    {
                        log.Write("Socket Connection with client " + clientID + " has crashed. Going to close it EXCEP");
                        networkStream.Close();
                        clientSocket.Close();
                        SendRemoveClientMSG();
                        socketWait.Set();
                        remainingSize = 4;
                        startIndex    = sizeIndex = msgSize = 0;
                        break;
                    }


                    socketWait.Reset();
                    if (sizeIndex > 0 && sizeIndex < 4)
                    {
                        remainingSize = 4 - sizeIndex;
                        startIndex    = sizeIndex;
                        continue;
                    }
                    else if (sizeIndex == 0)
                    {
                        log.Write(" sizeIndex == 0 so closing connection for " + clientID);
                        networkStream.Close();
                        clientSocket.Close();
                        return;
                    }
                    using (MemoryStream mem = new MemoryStream(buffer))
                    {
                        using (BinaryReader BR = new BinaryReader(mem))
                        {
                            msgSize = BR.ReadInt32();
                        }
                    }
                    networkStream.Flush();
                    if (msgSize > 147)
                    {
                        msgSize = 142;
                        log.Write(clientID + " ERROR msgSize " + msgSize + " " + Encoding.Default.GetString(buffer));
                        Console.WriteLine(clientID + " ye WRITTING FROM HANDLE CLIENT LISTEN " + " " + msgSize + " " + Encoding.Default.GetString(buffer));
                    }
                    send.SendMsgToClient(msgSize, networkStream, false);
                    socketWait.Set();
                    remainingSize = 4;
                    startIndex    = sizeIndex = msgSize = 0;
                }
            }
            catch (IOException)
            {
                log.Write("Socket Connection with client " + clientID + " has crashed. Going to close it EXCEP");
                networkStream.Close();
                clientSocket.Close();
                SendRemoveClientMSG();
            }
            catch (Exception ex)
            {
                log.Write("Socket Connection with client " + clientID + " has crashed. Going to close it " + ex);
                Console.WriteLine("Socket Connection with client " + clientID + " has crashed. Going to close it " + ex);
                networkStream.Close();
                clientSocket.Close();
                SendRemoveClientMSG();
            }
        }
Esempio n. 3
0
        private void Listen()
        {
            NetworkStream networkStream = null;

            try
            {
                networkStream = serverSocket.GetStream();
                Byte[]  buffer = new Byte[8];
                Byte[]  sizeBuff = new Byte[4];
                Byte[]  typeBuff = new Byte[4];
                int     msgType, msgSize = 0, sizeIndex = 0, remainingSize = 8, startIndex = 0;
                SendMsg send = new SendMsg();

                log.Write("enterred and started listening");
                while (true)
                {
                    try
                    {
                        sizeIndex += networkStream.Read(buffer, startIndex, remainingSize);
                    }
                    catch (ObjectDisposedException)
                    {
                        log.Write("Socket Connection with Server " + serverID + " has crashed. Local try Catch");
                        lock (ServerMain.disconnectedServerList)
                        {
                            if (!ServerMain.disconnectedServerList.Contains(serverID))
                            {
                                serverSocket.Close();
                                Connected = false;
                                Console.WriteLine("adding listen");
                                ServerMain.disconnectedServerList.Add(serverID);
                                ServerMain.reconnectServerWait.Set();
                            }
                        }
                        startIndex    = sizeIndex = msgSize = 0;
                        remainingSize = 8;
                        break;
                    }
                    catch (IOException)
                    {
                        log.Write("Socket Connection with Server " + serverID + " has crashed. Local try Catch");
                        lock (ServerMain.disconnectedServerList)
                        {
                            if (!ServerMain.disconnectedServerList.Contains(serverID))
                            {
                                serverSocket.Close();
                                Connected = false;
                                Console.WriteLine("adding listen");
                                ServerMain.disconnectedServerList.Add(serverID);
                                ServerMain.reconnectServerWait.Set();
                            }
                        }
                        startIndex    = sizeIndex = msgSize = 0;
                        remainingSize = 8;
                        break;
                    }
                    if (sizeIndex > 0 && sizeIndex < 8)
                    {
                        remainingSize = 8 - sizeIndex;
                        startIndex    = sizeIndex;
                        continue;
                    }
                    else if (sizeIndex == 0)
                    {
                        log.Write("closing connection for " + serverID);
                        networkStream.Close();
                        serverSocket.Close();
                        return;
                    }
                    Array.Copy(buffer, typeBuff, 4);
                    Array.Copy(buffer, 4, sizeBuff, 0, 4);
                    using (MemoryStream mem = new MemoryStream(typeBuff))
                    {
                        using (BinaryReader BR = new BinaryReader(mem))
                        {
                            msgType = BR.ReadInt32();
                        }
                    }
                    using (MemoryStream mem = new MemoryStream(sizeBuff))
                    {
                        using (BinaryReader BR = new BinaryReader(mem))
                        {
                            msgSize = BR.ReadInt32();
                        }
                    }
                    networkStream.Flush();
                    if (msgType == 1)
                    {
                        GetClientList(msgSize, networkStream);
                    }
                    else if (msgType == 2)
                    {
                        send.SendMsgToClient(msgSize, networkStream, true);
                    }
                    else if (msgType == 3)
                    {
                        RecRemoveClientMsg(msgSize, networkStream);
                    }

                    startIndex    = sizeIndex = msgSize = 0;
                    remainingSize = 8;
                }
            }
            catch (ObjectDisposedException)
            {
                log.Write("Socket Connection with Server " + serverID + " has crashed. Going to close it EXCEP");
                Console.WriteLine("Socket Connection with Server " + serverID + " has crashed. Going to close it EXCEP");
                lock (ServerMain.disconnectedServerList)
                {
                    if (!ServerMain.disconnectedServerList.Contains(serverID))
                    {
                        serverSocket.Close();
                        Connected = false;
                        Console.WriteLine("adding listen");
                        ServerMain.disconnectedServerList.Add(serverID);
                        ServerMain.reconnectServerWait.Set();
                    }
                }
            }
            catch (IOException)
            {
                log.Write("Socket Connection with Server " + serverID + " has crashed. Going to close it EXCEP");
                Console.WriteLine("Socket Connection with Server " + serverID + " has crashed. Going to close it EXCEP");
                lock (ServerMain.disconnectedServerList)
                {
                    if (!ServerMain.disconnectedServerList.Contains(serverID))
                    {
                        serverSocket.Close();
                        Connected = false;
                        Console.WriteLine("adding listen");
                        ServerMain.disconnectedServerList.Add(serverID);
                        ServerMain.reconnectServerWait.Set();
                    }
                }
            }
            catch (Exception)
            {
                log.Write("An Exception has occured in Server Listen msg for server " + serverID + " Going to close it ");
                Console.WriteLine("Socket Connection with Server " + serverID + " has crashed. Going to close it EXCEP");
                serverSocket.Close();
            }
        }