コード例 #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);
                }
            }
        }