예제 #1
0
        static void Main(string[] args)
        {
            IPEndPoint superPeerEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8090);
            int        port = 8091;

            ServerPeer server = new ServerPeer(superPeerEndPoint, port);

            server.Run();

            ClientPeer client = new ClientPeer(superPeerEndPoint);

            client.Run();

            //used Task.Factory.StartNew(() and sleep to simulate that this tasks are performed on different applications/machines
            Task.Factory.StartNew(() => server.AllowConnection(client.GetPeerAddress()));
            Thread.Sleep(3000);
            Task.Factory.StartNew(() => client.Connect(server.GetPeerAddress()));

            Thread.Sleep(3000);
            server.Send(new byte[] { 1, 2, 3, 4, 5 });
            Console.WriteLine(client.Read());

            client.Close();
            Thread.Sleep(1000);
            server.Close();

            Thread.Sleep(60000);
        }
예제 #2
0
 //下线
 public bool Logout()
 {
     //事件处理,稍后实现
     //SocketServer.Instance.handlePlayerEvent.OnLogout(this);
     //保存
     //if (!DataMgr.instance.SavePlayer(this))
     //{
     //    return false;
     //}
     //下线
     Peer.m_curUser = null;
     Peer.Close();
     return(true);
 }
        private Iso8583Message ToFEP(Iso8583Message msgToSend, SinkNode sinkNode, out bool needReversal)
        {
            Message response    = null;
            string  responseMsg = string.Empty;

            needReversal = false;
            try
            {
                if (msgToSend == null)
                {
                    Logger.Log("Iso message is null.");
                    return(SetReponseMessage(msgToSend, "20")); //Invalid response
                }

                if (sinkNode == null)
                {
                    Logger.Log("Sink node is null.");
                    return(SetReponseMessage(msgToSend, "91")); //Issuer inoperative
                }

                int maxNoRetries  = 3;
                int serverTimeout = 60000;

                sinkNode.IsActive = true;
                ClientPeer _clientPeer = new ClientPeer(sinkNode.Name, new TwoBytesNboHeaderChannel(
                                                            new Iso8583Ascii1987BinaryBitmapMessageFormatter(), sinkNode.IPAddress,
                                                            Convert.ToInt16(sinkNode.Port)), new Trx.Messaging.BasicMessagesIdentifier(11));
                _clientPeer.Connect();
                Thread.Sleep(1800);
                int retries = 0;
                while (retries < maxNoRetries)
                {
                    if (_clientPeer.IsConnected)
                    {
                        break;
                    }
                    else
                    {
                        _clientPeer.Close();
                        retries++;
                        _clientPeer.Connect();
                    }
                    Thread.Sleep(2000);
                }

                PeerRequest request = null;
                if (_clientPeer.IsConnected)
                {
                    request = new PeerRequest(_clientPeer, msgToSend);
                    request.Send();
                    request.WaitResponse(serverTimeout);
                    //request.MarkAsExpired();   //uncomment to test timeout

                    if (request.Expired)
                    {
                        Logger.Log("Connection timeout.");
                        needReversal = true;
                        return(SetReponseMessage(msgToSend, "68")); //Response received too late
                    }
                    if (request != null)
                    {
                        response = request.ResponseMessage;
                        //ResponseMessage = GetResponseMesage(response as Iso8583Message);
                    }
                    return(response as Iso8583Message);
                }
                else
                {
                    Console.WriteLine("\n Could not connect to the Sink Node..");
                    Console.BackgroundColor = ConsoleColor.Red;
                    Logger.Log("\n Could not connect to the Sink Node.");
                    return(SetReponseMessage(msgToSend, "91"));
                }
            }
            catch (Exception ex)
            {
                Logger.Log("ERROR: " + ex.Message);
                return(SetReponseMessage(msgToSend, "06")); //Error
            }
        }
        private Iso8583Message ToFEP(Iso8583Message msgToSend, SinkNode sinkNode, out bool needReversal)
        {
            Message response = null;
               string responseMsg = string.Empty;
               needReversal = false;
               try
               {
               if (msgToSend == null)
               {
                   Logger.Log("Iso message is null.");
                   return SetReponseMessage(msgToSend, "20");  //Invalid response
               }

               if (sinkNode == null)
               {
                   Logger.Log("Sink node is null.");
                   return SetReponseMessage(msgToSend, "91");  //Issuer inoperative
               }

               int maxNoRetries = 3;
               int serverTimeout = 60000;

               sinkNode.IsActive = true;
               ClientPeer _clientPeer = new ClientPeer(sinkNode.Name, new TwoBytesNboHeaderChannel(
                       new Iso8583Ascii1987BinaryBitmapMessageFormatter(), sinkNode.IPAddress,
                       Convert.ToInt16(sinkNode.Port)), new Trx.Messaging.BasicMessagesIdentifier(11));
               _clientPeer.Connect();
               Thread.Sleep(1800);
               int retries = 0;
               while (retries < maxNoRetries)
               {
                   if (_clientPeer.IsConnected)
                   {
                       break;
                   }
                   else
                   {
                       _clientPeer.Close();
                       retries++;
                       _clientPeer.Connect();
                   }
                   Thread.Sleep(2000);
               }

               PeerRequest request = null;
               if (_clientPeer.IsConnected)
               {
                   request = new PeerRequest(_clientPeer, msgToSend);
                   request.Send();
                   request.WaitResponse(serverTimeout);
                   //request.MarkAsExpired();   //uncomment to test timeout

                   if (request.Expired)
                   {
                       Logger.Log("Connection timeout.");
                       needReversal = true;
                       return SetReponseMessage(msgToSend, "68");  //Response received too late
                   }
                   if (request != null)
                   {
                       response = request.ResponseMessage;
                       //ResponseMessage = GetResponseMesage(response as Iso8583Message);
                   }
                   return response as Iso8583Message;
               }
               else
               {
                   Console.WriteLine("\n Could not connect to the Sink Node..");
                   Console.BackgroundColor = ConsoleColor.Red;
                   Logger.Log("\n Could not connect to the Sink Node.");
                   return SetReponseMessage(msgToSend, "91");
               }
               }
               catch (Exception ex)
               {
               Logger.Log("ERROR: " + ex.Message);
               return SetReponseMessage(msgToSend, "06");  //Error
               }
        }