예제 #1
0
파일: TcpSender.cs 프로젝트: Mofsy/sr-rbd
        public void AddServerNode(IPEndPoint node, BlockingQueue<Message> storage)
        {
            lock (this)
            {
                NodeInfo nodeInfo = null;
                if (nodes.ContainsKey(node))
                {
                    nodeInfo = nodes[node];
                }

                if (nodeInfo == null)
                {
                    // connect to node
                    Socket socket;
                    try
                    {
                        socket = new Socket(AddressFamily.InterNetwork,
                                    SocketType.Stream, ProtocolType.Tcp);
                        socket.Connect(node);

                        TcpWorker worker = new TcpWorker(socket, storage);
                        new Thread(new ThreadStart(worker.run)).Start();
                        nodes[node] = new NodeInfo(socket, true);
                    }
                    catch (Exception)
                    {
                        Logger.getInstance().log("Failed to add server node: " + node.ToString(),
                                        LOGGING_NAME,
                                        Logger.Level.WARNING);
                        return;
                    }
                }
                else
                {
                    // mark node as server
                    nodeInfo.setIsServer(true);
                }
            }
        }
예제 #2
0
파일: TcpListener.cs 프로젝트: Mofsy/sr-rbd
        public void run()
        {
            try
            {
                IPEndPoint ip = new IPEndPoint(listenHost, listenPort);
                serverSocket = new Socket(AddressFamily.InterNetwork,
                                            SocketType.Stream, ProtocolType.Tcp);
                serverSocket.Bind(ip);
                serverSocket.Listen(BACKLOG);

                Logger.getInstance().log("Server is listening on port " +
                        listenPort + ".", LOGGING_NAME, Logger.Level.INFO);
            }
            catch (SocketException)
            {
                Logger.getInstance().log("Could not listen on port " + listenPort + "!", LOGGING_NAME, Logger.Level.SEVERE);
                return;
            }

            while (true)
            {
                try
                {
                    Socket newConnection = serverSocket.Accept();
                    TcpSender.getInstance().addNodeBySocket((IPEndPoint)newConnection.RemoteEndPoint,
                        newConnection
                    );

                    TcpWorker worker = new TcpWorker(newConnection, storage);
                    new Thread(new ThreadStart(worker.run)).Start();
                }
                catch (SocketException)
                {
                    Logger.getInstance().log("Could not accept new connection!",
                            LOGGING_NAME, Logger.Level.WARNING);
                }
            }
        }