コード例 #1
0
        private static void RecServerFromLB(NetworkStream networkStream)
        {
            byte[]         buffer = new byte[30];
            string         dataFromClient;
            IList <string> msgSplit;
            string         serverIp;
            int            port;

            try
            {
                while (true)
                {
                    networkStream.Read(buffer, 0, 30);
                    dataFromClient = Encoding.ASCII.GetString(buffer);
                    dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
                    msgSplit       = dataFromClient.Split(':');
                    serverIp       = msgSplit[0];
                    port           = Int32.Parse(msgSplit[1]) + 2;
                    log.Write("Starting Listening from Server " + serverIp + ":" + port);
                    TcpClient serverSocket = new TcpClient();
                    serverSocket.Connect(serverIp, port);
                    HandleServer server = new HandleServer {
                        ServerID = Interlocked.Increment(ref sereverounter), ServerSocket = serverSocket, Sender = true, Port = port, ServerIP = serverIp
                    };
                    server.StartServer();
                    serverList.Add(server.ServerID, server);
                }
            }
            catch (Exception ex)
            {
                log.Write("Exception in RecServerFromLB " + ex);
                Console.WriteLine("Exception in RecServerFromLB " + ex);
            }
        }
コード例 #2
0
        private static void FetchServers(TcpListener serverSocket)
        {
            bool newServer;

            try
            {
                while (true)
                {
                    newServer = true;
                    TcpClient serverTCPSocket = default(TcpClient);
                    Console.WriteLine("Started Listening .....");
                    serverTCPSocket = serverSocket.AcceptTcpClient();


                    IList <string> msgSplit;
                    msgSplit = serverTCPSocket.Client.RemoteEndPoint.ToString().Split(':');
                    foreach (HandleServer handleserver in serverList.Values)
                    {
                        Console.WriteLine(msgSplit[0] + " = " + handleserver.ServerIP + " " + msgSplit[0].Equals(handleserver.ServerIP));
                        if (msgSplit[0].Equals(handleserver.ServerIP))
                        {
                            handleserver.Connected    = true;
                            handleserver.ServerSocket = serverTCPSocket;
                            handleserver.StartServer();
                            newServer = false;
                            log.Write("server " + handleserver.ServerIP + " has reconected ");
                            Console.WriteLine("server " + handleserver.ServerIP + " has reconected ");
                            //lock (disconnectedServerList)
                            //    disconnectedServerList.Remove(handleserver.ServerID);
                            //Console.WriteLine("Clearing List " + disconnectedServerList.Count + " deleted " + handleserver.ServerID + handleserver.Connected + handleserver.ServerSocket.Connected);
                        }
                    }
                    if (newServer)
                    {
                        HandleServer server = new HandleServer {
                            ServerID = Interlocked.Increment(ref sereverounter), ServerSocket = serverTCPSocket, Sender = false, ServerIP = msgSplit[0]
                        };
                        server.StartServer();
                        serverList.Add(server.ServerID, server);
                    }
                }
            }
            catch (Exception ex)
            {
                log.Write("Exception in FetchServers " + ex);
                Console.WriteLine("Exception in FetchServers " + ex);
            }
        }
コード例 #3
0
        private static void DiscServersProcessor()
        {
            try
            {
                bool clear = false;
                while (true)
                {
                    reconnectServerWait.WaitOne();

                    lock (disconnectedServerList)
                    {
                        if (0 != disconnectedServerList.Count)
                        {
                            log.Write("Sometihng has crashed ");
                            Console.WriteLine("Sometihng has crashed " + disconnectedServerList.Count);

                            foreach (int serverID in disconnectedServerList)
                            {
                                HandleServer server       = (HandleServer)serverList[serverID];
                                TcpClient    serverSocket = new TcpClient();
                                if (!server.Connected)
                                {
                                    if (server.Sender)
                                    {
                                        server.ReconnectServer();
                                        clear = true;
                                    }
                                    else
                                    {
                                        log.Write("Reconnecting server but it is not a sender so waiting for " + server.ServerIP);
                                        Console.WriteLine("=================== Reconnecting server but it is not a sender so waiting for " + server.ServerIP);
                                    }
                                }
                            }
                        }
                    }

                    if (clear)
                    {
                        Thread.Sleep(15000);
                        log.Write("Clearing List " + disconnectedServerList.Count);
                        Console.WriteLine("Clearing List " + disconnectedServerList.Count);
                        lock (disconnectedServerList)
                        {
                            foreach (int serverID in disconnectedServerList)
                            {
                                HandleServer server = (HandleServer)serverList[serverID];
                                server.Connected = true;
                            }
                            disconnectedServerList.Clear();
                        }

                        clear = false;
                    }
                    reconnectServerWait.Reset();
                }
            }
            catch (Exception ex)
            {
                log.Write("Exception in DiscServersProcessor " + ex);
                Console.WriteLine("Exception in DiscServersProcessor " + ex);
            }
        }
コード例 #4
0
        public async static void FrwrdMsgToServer(byte[] msg, int msgType, string recieverID)
        {
            Byte[]        msgTypeBuff = new Byte[4];
            Byte[]        finalMsg    = new Byte[4 + msg.Length];
            NetworkStream networkStream;
            bool          clientFound = false;
            HandleServer  serverFail  = null;

            try
            {
                using (MemoryStream mem = new MemoryStream(msgTypeBuff))
                {
                    using (BinaryWriter BW = new BinaryWriter(mem))
                    {
                        BW.Write(msgType);
                    }
                }
                Array.Copy(msgTypeBuff, finalMsg, 4);
                Array.Copy(msg, 0, finalMsg, 4, msg.Length);

                foreach (HandleServer server in serverList.Values)
                {
                    serverFail = server;
                    if (server.ClientsList.Contains(recieverID))
                    {
                        clientFound = true;
                        if (server.Connected && null != server.ServerSocket)
                        {
                            try
                            {
                                networkStream = server.ServerSocket.GetStream();
                                await networkStream.WriteAsync(finalMsg, 0, finalMsg.Length);

                                networkStream.Flush();
                            }
                            catch (IOException)
                            {
                                log.Write(serverList.Count + " Server socket has been closed so discarding the message " + recieverID);
                                discardedClientsList.Add(recieverID);
                                // Console.WriteLine("Discarding" + serverFail.ServerID);
                                lock (disconnectedServerList)
                                    log.Write("enterred " + recieverID);
                                {
                                    if (!disconnectedServerList.Contains(serverFail.ServerID))
                                    {
                                        serverFail.ServerSocket.Close();
                                        serverFail.Connected = false;
                                        Console.WriteLine("adding serverIP" + recieverID);
                                        disconnectedServerList.Add(serverFail.ServerID);
                                        reconnectServerWait.Set();
                                    }
                                }
                                continue;
                            }
                            catch (ObjectDisposedException)
                            {
                                log.Write(serverList.Count + " Server socket has been closed so discarding the message local try catch " + recieverID);
                                discardedClientsList.Add(recieverID);
                                // Console.WriteLine("Discarding" + serverFail.ServerID);
                                lock (disconnectedServerList)
                                {
                                    log.Write("enterred " + recieverID);
                                    if (!disconnectedServerList.Contains(serverFail.ServerID))
                                    {
                                        serverFail.ServerSocket.Close();
                                        serverFail.Connected = false;
                                        Console.WriteLine("adding serverIP " + recieverID);
                                        disconnectedServerList.Add(serverFail.ServerID);
                                        reconnectServerWait.Set();
                                    }
                                }
                                continue;
                            }


                            break;
                        }
                        else
                        {
                            log.Write(" server " + server.ServerIP + " has crashed so Discarding this msg  FrwrdMsgToServer");
                            // Console.WriteLine(recieverID);
                            discardedClientsList.Add(recieverID);
                            lock (disconnectedServerList)
                            {
                                if (!disconnectedServerList.Contains(serverFail.ServerID))
                                {
                                    serverFail.ServerSocket.Close();
                                    serverFail.Connected = false;
                                    Console.WriteLine("adding FrwrdMsgToServer");
                                    disconnectedServerList.Add(serverFail.ServerID);
                                    reconnectServerWait.Set();
                                }
                            }
                        }
                    }
                }
                if (!clientFound)
                {
                    HandleServer server = (HandleServer)serverList[1];
                    log.Write("Receiver not found in any srever " + recieverID);
                }
            }

            catch (Exception ex)
            {
                log.Write("Exception occured in FrwrdMsgToServer " + ex);
                Console.WriteLine("Exception occured in FrwrdMsgToServer " + ex);
            }
        }
コード例 #5
0
        private static void SendRemoveClientMSG(string clientID)
        {
            HandleServer serverFail = null;

            Byte[] msgTypeBuff = new Byte[4];
            Byte[] Size        = new Byte[4];
            Byte[] finalMsg;

            try
            {
                lock (clientsList)
                {
                    if (clientsList.Contains(clientID))
                    {
                        clientsList.Remove(clientID);
                    }
                    else
                    {
                        log.Write("Client already removed from this server List. So not sending it to servers");
                        return;
                    }
                }

                log.Write("Sending client ID " + clientID + " to Servers to remove from List ");
                using (MemoryStream mem = new MemoryStream(msgTypeBuff))
                {
                    using (BinaryWriter BW = new BinaryWriter(mem))
                    {
                        BW.Write(3);
                    }
                }
                Byte[] buffer = Encoding.Default.GetBytes(clientID);

                using (MemoryStream mem = new MemoryStream(Size))
                {
                    using (BinaryWriter BW = new BinaryWriter(mem))
                    {
                        BW.Write(buffer.Length);
                    }
                }
                finalMsg = new byte[8 + buffer.Length];
                Array.Copy(msgTypeBuff, finalMsg, 4);
                Array.Copy(Size, 0, finalMsg, 4, 4);
                Array.Copy(buffer, 0, finalMsg, 8, buffer.Length);

                foreach (HandleServer server in serverList.Values)
                {
                    serverFail = server;
                    if (server.Connected && server.ServerSocket.Connected)
                    {
                        NetworkStream netwrokStream = server.ServerSocket.GetStream();
                        netwrokStream.Write(finalMsg, 0, finalMsg.Length);
                        netwrokStream.Flush();
                        log.Write("Sending message to server " + server.ServerIP + " to Remove " + clientID);
                    }
                    else
                    {
                        log.Write("server " + server.ServerIP + " has crashed so reconnecting it SendRemoveClientMSG Excep ");
                        //server.Connected = false;
                        lock (disconnectedServerList)
                        {
                            if (!disconnectedServerList.Contains(serverFail.ServerID))
                            {
                                serverFail.ServerSocket.Close();
                                serverFail.Connected = false;
                                disconnectedServerList.Add(serverFail.ServerID);
                                reconnectServerWait.Set();
                            }
                        }
                    }
                }
            }
            catch (ObjectDisposedException ex)
            {
                log.Write("ObjectDisposedException in RemoveClientMSG " + ex);
                lock (disconnectedServerList)
                {
                    if (!disconnectedServerList.Contains(serverFail.ServerID))
                    {
                        serverFail.ServerSocket.Close();
                        serverFail.Connected = false;
                        disconnectedServerList.Add(serverFail.ServerID);
                        reconnectServerWait.Set();
                    }
                }
            }
            catch (InvalidOperationException ex)
            {
                log.Write("InvalidOperationException in RemoveClientMSG " + ex);
                lock (disconnectedServerList)
                {
                    if (!disconnectedServerList.Contains(serverFail.ServerID))
                    {
                        serverFail.ServerSocket.Close();
                        serverFail.Connected = false;
                        disconnectedServerList.Add(serverFail.ServerID);
                        reconnectServerWait.Set();
                    }
                }
            }
            catch (IOException ex)
            {
                log.Write("IOException in RemoveClientMSG " + ex);
                lock (disconnectedServerList)
                {
                    if (!disconnectedServerList.Contains(serverFail.ServerID))
                    {
                        serverFail.ServerSocket.Close();
                        serverFail.Connected = false;
                        disconnectedServerList.Add(serverFail.ServerID);
                        reconnectServerWait.Set();
                    }
                }
            }
            catch (Exception ex)
            {
                log.Write("Exception in RemoveClientMSG " + ex);
                Console.WriteLine("Exception in RemoveClientMSG " + ex);
            }
        }