public bool Connect()
        {
            // Create a client peer to connect to remote system. The messages
            // will be matched using fields 41 and 11.
            if (_clientPeer == null)
            {
                _clientPeer = new ClientPeer(Client_NAME, new TwoBytesNboHeaderChannel(
                                                 new Iso8583Ascii1987BinaryBitmapMessageFormatter(), _hostname, _port),
                                             new BasicMessagesIdentifier(FieldNos.F11_Trace));
            }
            _clientPeer.Connect();
            Thread.Sleep(1000);

            int retries = 0;

            while (retries < maxNoRetries)
            {
                lock (this)
                {
                    if (_clientPeer.IsConnected)
                    {
                        break;
                    }
                    else
                    {
                        _clientPeer.Close();
                        retries++;
                        _clientPeer.Connect();
                    }
                }
                Thread.Sleep(1000);
            }

            return(_clientPeer.IsConnected);
        }
Exemple #2
0
 public void TestCloseCalledTwiceOnUnconnectedPeer()
 {
     try
     {
         Peer peer = new Peer("127.0.0.1", 6881, new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0));
         peer.Close();
         peer.Close();
     }
     catch (Exception ex)
     {
         Assert.True(false, "Should not throw execption here but it did. " + ex.Message);
     }
     Assert.True(true);
 }
Exemple #3
0
        public virtual void TestEviction()
        {
            int       Capacity = 3;
            PeerCache cache    = new PeerCache(Capacity, 100000);

            DatanodeID[]             dnIds = new DatanodeID[Capacity + 1];
            TestPeerCache.FakePeer[] peers = new TestPeerCache.FakePeer[Capacity + 1];
            for (int i = 0; i < dnIds.Length; ++i)
            {
                dnIds[i] = new DatanodeID("192.168.0.1", "fakehostname_" + i, "fake_datanode_id_"
                                          + i, 100, 101, 102, 103);
                peers[i] = new TestPeerCache.FakePeer(dnIds[i], false);
            }
            for (int i_1 = 0; i_1 < Capacity; ++i_1)
            {
                cache.Put(dnIds[i_1], peers[i_1]);
            }
            // Check that the peers are cached
            NUnit.Framework.Assert.AreEqual(Capacity, cache.Size());
            // Add another entry and check that the first entry was evicted
            cache.Put(dnIds[Capacity], peers[Capacity]);
            NUnit.Framework.Assert.AreEqual(Capacity, cache.Size());
            NUnit.Framework.Assert.AreSame(null, cache.Get(dnIds[0], false));
            // Make sure that the other entries are still there
            for (int i_2 = 1; i_2 < Capacity; ++i_2)
            {
                Peer peer = cache.Get(dnIds[i_2], false);
                NUnit.Framework.Assert.AreSame(peers[i_2], peer);
                NUnit.Framework.Assert.IsTrue(!peer.IsClosed());
                peer.Close();
            }
            NUnit.Framework.Assert.AreEqual(1, cache.Size());
            cache.Close();
        }
 public void StopServer()
 {
     if (peer != null)
     {
         peer.Close();
         Debug.LogError("[Server] Shutting down...");
     }
 }
Exemple #5
0
        public void Close()
        {
            if (_server != null)
            {
                _server.Close();
            }

            Peer.Close();
        }
//        private void ProcessSuperPeerMessages()
//        {
//            Peer.AddMethodToMessageReceivedEvent(ProcessSuperPeerMessages);
//            Task.Factory.StartNew(() => Peer.StartListenMessagesFromSuperPeer());
//        }

//        private void ProcessSuperPeerMessages(IClient client, MessageEventArgs messageEventArgs)
//        {
//            RequestMessage requestMessage = (RequestMessage)messageEventArgs.Message;
//
//            switch (requestMessage.RequestedMessageType)
//            {
//                case MessageType.TcpConnection:
//                    ServerPeerConnection serverPeerConnection = new TcpServerPeerConnection(this);
//                    _client = serverPeerConnection.GetConnection();
//                    break;
//            }
//        }

        public void Close()
        {
            if (_client != null)
            {
                _client.Close();
            }
            //Peer.StopListenMessagesFromSuperPeer();
            Peer.Close();
        }
Exemple #7
0
        public static Iso8583Message SendMessageToFEP(Peer peer, Iso8583Message msg)
        {
            if (msg == null)
            {
                MessageLogger.LogMessage("iso Message is null");
                SetReponseMessage(msg, ResponseCode.INVALID_RESPONSE.ToString());
            }

            int maxRetries = 3; int numberOfRetries = 1;

            while (numberOfRetries < maxRetries)
            {
                if (peer.IsConnected)
                {
                    break;
                }
                peer.Close();
                numberOfRetries++;
                peer.Connect();
                Thread.Sleep(2000);
            }

            if (peer.IsConnected)
            {
                try
                {
                    var request = new PeerRequest(peer, msg);

                    request.Send();

                    //At this point, the message has been sent to the SINK for processing
                    int serverTimeout = 60000;          //60 secs
                    request.WaitResponse(serverTimeout);
                    if (request.Expired)
                    {
                        MessageLogger.LogMessage("Connection timeout.");
                        return(SetReponseMessage(msg, ResponseCode.ISSUER_OR_SWITCH_INOPERATIVE)); //Response received too late
                    }

                    var response = request.ResponseMessage;
                    return(response as Iso8583Message);
                }
                catch (Exception ex)
                {
                    MessageLogger.LogError("Error sending message: " + ex.Message);
                    return(SetReponseMessage(msg, ResponseCode.ERROR));
                }
            }
            else
            {
                MessageLogger.LogMessage("\n Could not connect to the Sink Node.");
                return(SetReponseMessage(msg, ResponseCode.ISSUER_OR_SWITCH_INOPERATIVE.ToString())); //Issuer inoperative
            }
        }
Exemple #8
0
        public void Connect(PeerAddress peerAddress)
        {
            var connectAsServerMessage = new PeerAddressMessage(peerAddress, MessageType.ConnectAsClient);

            Peer.SendToSuperPeer(connectAsServerMessage);

            Peer.ReadFromSuperPeer();
            var connection = new TcpClientPeerConnection(this, Peer.PeerAddress);

            _server = connection.GetConnection();
            Peer.Close();
        }
 /// <summary>
 /// 断开连接
 /// </summary>
 public void Disconnect()
 {
     if (Peer != null)
     {
         if (IsNeedConnect && IsConnect)
         {
             Peer.Shutdown(SocketShutdown.Both);
             Peer.Disconnect(false);
         }
         Peer.Close();
         Peer.Dispose();
         Peer = null;
     }
 }
        public void AllowConnection(PeerAddress peerAddress)
        {
            var connectAsServerMessage = new PeerAddressMessage(peerAddress, MessageType.ConnectAsServer);

            Peer.SendToSuperPeer(connectAsServerMessage);

            Peer.ReadFromSuperPeer();
            ServerPeerConnection serverPeerConnection = new TcpServerPeerConnection(this);

            _client = serverPeerConnection.GetConnection();

            Peer.Close();
            Console.WriteLine("Client connected to server");
        }
Exemple #11
0
    public static Peer CreateServer(string ip, int port, int maxConnections)
    {
        Peer peer = new Peer();

        peer.ptr = RakNet_Native.NET_Create();

        if (RakNet_Native.NET_StartServer(peer.ptr, ip, port, maxConnections) == 0)
        {
            return(peer);
        }
        peer.Close();
        string text = StringFromPointer(RakNet_Native.NET_LastStartupError(peer.ptr));

        Debug.LogWarning("[RakNet] Couldn't create server on port " + port + " (" + text + ")");
        return(null);
    }
 /// <exception cref="System.IO.IOException"/>
 public override void Close()
 {
     lock (this)
     {
         startOffset = -1;
         checksum    = null;
         if (peerCache != null & sentStatusCode)
         {
             peerCache.Put(datanodeID, peer);
         }
         else
         {
             peer.Close();
         }
     }
 }
Exemple #13
0
 /// <exception cref="System.IO.IOException"/>
 public virtual void Close()
 {
     lock (this)
     {
         packetReceiver.Close();
         startOffset = -1;
         checksum    = null;
         if (peerCache != null && sentStatusCode)
         {
             peerCache.Put(datanodeID, peer);
         }
         else
         {
             peer.Close();
         }
     }
 }
Exemple #14
0
 /// <exception cref="System.IO.IOException"/>
 public virtual void Close()
 {
     try
     {
         @in.Close();
     }
     finally
     {
         try
         {
             @out.Close();
         }
         finally
         {
             enclosedPeer.Close();
         }
     }
 }
Exemple #15
0
        static Message SendMessage(Peer peer, Message msg)
        {
            int maxRetries = 3; int numberOfRetries = 1;

            while (numberOfRetries < maxRetries)
            {
                if (peer.IsConnected)
                {
                    break;
                }
                peer.Close();
                numberOfRetries++;
                peer.Connect();
                Thread.Sleep(2000);
            }

            if (peer.IsConnected)
            {
                try
                {
                    var request = new PeerRequest(peer, msg);

                    request.Send();

                    //At this point, the message has been sent to the SINK for processing
                    int serverTimeout = 30000;
                    request.WaitResponse(serverTimeout);

                    var response = request.ResponseMessage;
                    return(response);
                }
                catch (Exception ex)
                {
                    msg.Fields.Add(39, "06"); // ERROR
                    MessageLogger.LogError("Error sending message " + ex.Message + "   Inner Exception:  " + ex.InnerException);
                    return(msg);
                }
            }
            else
            {
                msg.Fields.Add(39, ResponseCode.ISSUER_OR_SWITCH_INOPERATIVE); // ERROR
                return(msg);
            }
        }
Exemple #16
0
    public static Peer CreateConnection(string hostname, int port, int retries, int retryDelay, int timeout)
    {
        Peer peer = new Peer();

        peer.ptr = RakNet_Native.NET_Create();

        if (RakNet_Native.NET_StartClient(peer.ptr, hostname, port, retries, retryDelay * 100, timeout * 100) == 0)
        {
            Debug.Log("[RakNet] Peer created connection to " + hostname + ":" + port + " with " + retries + " retry count [delay: " + retryDelay + "] [Timeout: " + timeout + "]");
            return(peer);
        }

        string text = StringFromPointer(RakNet_Native.NET_LastStartupError(peer.ptr));

        Debug.LogWarning("[RakNet] Couldn't connect to server " + hostname + ":" + port + " (" + text + ")");
        peer.Close();
        peer = null;
        return(null);
    }
Exemple #17
0
 private Peer GetInternal(DatanodeID dnId, bool isDomain)
 {
     lock (this)
     {
         IList <PeerCache.Value> sockStreamList = multimap.Get(new PeerCache.Key(dnId, isDomain
                                                                                 ));
         if (sockStreamList == null)
         {
             return(null);
         }
         IEnumerator <PeerCache.Value> iter = sockStreamList.GetEnumerator();
         while (iter.HasNext())
         {
             PeerCache.Value candidate = iter.Next();
             iter.Remove();
             long ageMs = Time.MonotonicNow() - candidate.GetTime();
             Peer peer  = candidate.GetPeer();
             if (ageMs >= expiryPeriod)
             {
                 try
                 {
                     peer.Close();
                 }
                 catch (IOException)
                 {
                     Log.Warn("got IOException closing stale peer " + peer + ", which is " + ageMs + " ms old"
                              );
                 }
             }
             else
             {
                 if (!peer.IsClosed())
                 {
                     return(peer);
                 }
             }
         }
         return(null);
     }
 }