Esempio n. 1
0
        public bool Connect()
        {
            if (Program.DEBUG)
            {
                CIO.DebugOut("Connecting to " + mIp + ":" + mPort);
            }
            SocketAsyncEventArgs asyncConnection = new SocketAsyncEventArgs();
            bool SuccessfulConnected             = false;

            asyncConnection.Completed     += (object sender, SocketAsyncEventArgs e) => { SuccessfulConnected = true; };
            asyncConnection.RemoteEndPoint = new IPEndPoint(mIp, mPort);
            mSocket.ConnectAsync(asyncConnection);
            Thread.Sleep(3000);
            if (SuccessfulConnected)
            {
                if (Program.DEBUG)
                {
                    CIO.DebugOut("Connection with " + mIp + ":" + mPort + " enstablished!");
                }
                mConnect        = true;
                mThreadListener = new Thread(new ThreadStart(Listen));
                mThreadListener.Start();
                return(true);
            }
            else
            {
                if (Program.DEBUG)
                {
                    CIO.DebugOut("Connection with " + mIp + ":" + mPort + " failed!");
                }
                asyncConnection.Dispose();
                return(false);
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Crea un nuovo oggetto CBlock usando una stringa che lo rappresenta.
 /// </summary>
 /// <param name="BlockString">Stringa che rappresenta l'oggetto CBlock.</param>
 public static CBlock Deserialize(string BlockString)
 {
     string[] blockField;
     BlockString = BlockString.Trim('{', '}');
     blockField  = BlockString.Split(';');
     if (Program.DEBUG)
     {
         CIO.DebugOut("Deserializing block number: " + blockField[1] + ".");
     }
     return(new CBlock(blockField[0], Convert.ToUInt64(blockField[1]), blockField[2], Convert.ToUInt64(blockField[3]), Convert.ToUInt64(blockField[4]), Convert.ToUInt16(blockField[5])));
 }
Esempio n. 3
0
        //attende il collegamento di nuovi peer
        private void StartAcceptUsersConnection()
        {
            //crea un eventargs per una richiesta di connessione asincrona, se la lista dei peers non è ancora piena inizia ad attendere fino a quando non riceve
            //una richiesta di connessione o il segnale d'arresto. Se viene ricevuta una richiesta di connessione viene chiamata la funzione InsertNewPeer che
            //inserisce il nuovo peer nella lista dei peer mPeers

            //è asincrono perchè altrimenti al segnale di spegnimento non si fermerebbe
            SocketAsyncEventArgs asyncConnection;
            bool IncomingConnection = false;

            if (Program.DEBUG)
            {
                Console.WriteLine("Attending connection...");
            }
            while (!IsStopped)
            {
                if (ConnectedPeers < MAX_PEERS)
                {
                    IncomingConnection         = false;
                    asyncConnection            = new SocketAsyncEventArgs();
                    asyncConnection.Completed += (object sender, SocketAsyncEventArgs e) => { IncomingConnection = true; };
                    mListener.AcceptAsync(asyncConnection);
                    while (!IncomingConnection && !IsStopped)
                    {
                        Thread.Sleep(1000);
                    }
                    if (IncomingConnection)
                    {
                        if (Program.DEBUG)
                        {
                            CIO.DebugOut("Established connection!");
                        }
                        InsertNewPeer(asyncConnection.AcceptSocket);
                    }
                    asyncConnection.Dispose();
                }
                else
                {
                    Thread.Sleep(10000);
                }
            }
            //TODO
            //CloseAllConnection();
            if (Program.DEBUG)
            {
                CIO.WriteLine("Chiuse tutte le connessioni con gli users");
            }
        }
Esempio n. 4
0
        private bool IsStopped = false; //set true per spegnere il server

        private CServer(List <CPeer> Peers)
        {
            rsaKeyPair = new RSACryptoServiceProvider(); // crea oggetto CSP per generare o caricare il keypair
            if (File.Exists("keystore.xml"))             // Se il file di keystore esiste viene caricato in memoria
            {
                rsaKeyPair = new RSACryptoServiceProvider();
                string xmlString = rsaKeyPair.ToXmlString(true);
                File.WriteAllText("keystore.xml", xmlString);
            }
            else//se il file non esiste ne viene generato uno
            {
                rsaKeyPair = RSA.GenRSAKey();
                string xmlString = rsaKeyPair.ToXmlString(true);
                File.WriteAllText("keystore.xml", xmlString);
            }


            mLastBlockNumber = CBlockChain.Instance.LastBlock.BlockNumber;
            if (Program.DEBUG)
            {
                CIO.DebugOut("Last block number: " + mLastBlockNumber + ".");
            }

            if (Program.DEBUG)
            {
                CIO.DebugOut("Inizialize mPeers...");
            }
            mPeers = new CPeers(MAX_PEERS, RESERVED_CONNECTION);

            if (Program.DEBUG)
            {
                CIO.DebugOut("Inizialie the Listener...");
            }
            //crea un socket che attende connessioni in ingresso di peer che vogliono collegarsi, in ascolto sulla porta DEFOULT_PORT
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, DEFOULT_PORT);

            mListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            mListener.Bind(localEndPoint);
            mListener.Listen(DEFOULT_PORT);

            if (Program.DEBUG)
            {
                CIO.DebugOut("Finish inizializing!");
            }
            Start(Peers);
        }
Esempio n. 5
0
        private void Start(List <CPeer> Peers)
        {
            if (Program.DEBUG)
            {
                CIO.DebugOut("Begin to enstablish connections to initial peers...");
            }
            //si collega ai peer inseriti nella lista iniziale.
            foreach (CPeer p in Peers)
            {
                if (p.Connect())
                {
                    if (!mPeers.Insert(p))
                    {
                        break;
                    }
                }
            }

            if (Program.DEBUG)
            {
                CIO.DebugOut("Begin to enstablish connections to other peers...");
            }
            mThreadPeers = new Thread(new ThreadStart(UpdatePeersList));
            mThreadPeers.Start();

            if (Program.DEBUG)
            {
                CIO.DebugOut("Start listening...");
            }
            mThreadListener = new Thread(new ThreadStart(StartAcceptUsersConnection));
            mThreadListener.Start();

            if (Program.DEBUG)
            {
                CIO.DebugOut("Start update blockchain...");
            }
            mUpdateBlockChainThread = new Thread(new ThreadStart(UpdateBlockchain));
            mUpdateBlockChainThread.Start();
        }