Beispiel #1
0
        private void OnRecive(TCPSocketToken token, RecivePacket packet)
        {
            TCPSession sendList = clients.GetOrAdd(token, (TCPSocketToken tk) =>
            {
                TCPSession session = new TCPSession();
                session.SetToken(tk);
                return(session);
            });

            /*
             * string str = Encoding.Default.GetString(pack.Buffer, pack.HeadLen, pack.Length - pack.HeadLen);
             * if (str != StringNetPacket.TestStr)
             * {
             *  Console.WriteLine("server recive error !");
             * }
             */
            for (int i = 0; i < packet.Length - packet.HeadLen; ++i)
            {
                if (packet.Buffer[packet.HeadLen + i] != (byte)i)
                {
                    Console.WriteLine("!!!");
                }
            }
            StringNetPacket pongPacket = new StringNetPacket(1024);

            token.Session.SendPacket(pongPacket);
        }
Beispiel #2
0
        private void OnConnect(TCPSession session, SocketError e)
        {
            if (e != SocketError.Success)
            {
                Console.WriteLine(string.Format("{0} : ConnectResult {1} ", (session.UserData as SendInfor).Index, e.ToString()));
                return;
            }
            SendInfor infor = session.UserData as SendInfor;

            SendPack(session);
        }
Beispiel #3
0
        private void SendPack(TCPSession session)
        {
            SendInfor infor = session.UserData as SendInfor;

            if (infor.SendNum > 100)
            {
                session.DisConnect();
                return;
            }
            StringNetPacket pongPacket = new StringNetPacket(1024);

            infor.SendNum++;
            session.SendPacket(pongPacket);
        }
Beispiel #4
0
 public virtual SessionT Connect <SessionT>() where SessionT : Session
 {
     if (Protocol == Protocol.TCP)
     {
         TcpClient tcpClient = new TcpClient();
         tcpClient.Connect(Host, Port);
         NetworkSession = new TCPSession(tcpClient);
         Session        = Activator.CreateInstance(typeof(SessionT), NetworkSession) as Session;
         Session.Start();
         return(Session as SessionT);
     }
     else
     {
         throw new NotImplementedException();
     }
 }
Beispiel #5
0
 public void Start()
 {
     times++;
     count = 0;
     for (int i = 0; i < MaxNum; ++i)
     {
         TCPSession session = new TCPSession();
         lock (Sessions)
         {
             Sessions.Add(session);
         }
         SendInfor infor = new SendInfor()
         {
             Index = i, times = times
         };
         session.UserData = infor;
         client.Connect("127.0.0.1", 12345, session);
         Thread.Sleep(2);
     }
 }
Beispiel #6
0
        public int ReloadPlatConfig()
        {
            Dictionary <string, string>     normalDict          = new Dictionary <string, string>();
            Dictionary <int, WaitingConfig> waitingDict         = new Dictionary <int, WaitingConfig>();
            List <TradeLevelLimitConfig>    tradeLevelLimitList = new List <TradeLevelLimitConfig>();
            Dictionary <ChatTypeIndexes, List <ChatLevelLimitConfig> > chatLevelLimitDic = new Dictionary <ChatTypeIndexes, List <ChatLevelLimitConfig> >();

            try
            {
                XElement xml = XElement.Load(Global.GameResPath(this.fileName));
                this.LoadNormalConfig(xml, normalDict);
                this.LoadWaitingConfig(xml, waitingDict);
                this.LoadTradeLevelLimitConfig(xml, tradeLevelLimitList);
                this.LoadChatLevelLimitConfig(xml, chatLevelLimitDic);
                this._PlatConfigTradeLimitConfigDict = this.LoadTradeLimitsConfig(xml);
            }
            catch (Exception e)
            {
                LogManager.WriteException("重新加载配置文件 PlatConfig.xml  失败!!!" + e.ToString());
                return(-1);
            }
            lock (this._PlatConfigNormalDict)
            {
                this._PlatConfigNormalDict = normalDict;
            }
            lock (this._PlatConfigWaitingDict)
            {
                this._PlatConfigWaitingDict = waitingDict;
            }
            lock (this._PlatConfigTradeLevelLimitList)
            {
                this._PlatConfigTradeLevelLimitList = tradeLevelLimitList;
            }
            lock (this._PlatConfigChatLevelLimitDic)
            {
                this._PlatConfigChatLevelLimitDic = chatLevelLimitDic;
            }
            TCPSession.SetMaxPosCmdNumPer5Seconds(8);
            GameManager.loginWaitLogic.LoadConfig();
            return(0);
        }
Beispiel #7
0
        /// <summary>
        /// 重新加载文件
        /// </summary>
        /// <returns></returns>
        public int ReloadPlatConfig()
        {
            Dictionary <string, string>     normalDict          = new Dictionary <string, string>();
            Dictionary <int, WaitingConfig> waitingDict         = new Dictionary <int, WaitingConfig>();
            List <TradeLevelLimitConfig>    tradeLevelLimitList = new List <TradeLevelLimitConfig>();

            try
            {
                XElement xml = XElement.Load(Global.GameResPath(fileName));
                LoadNormalConfig(xml, normalDict);
                LoadWaitingConfig(xml, waitingDict);
                LoadTradeLevelLimitConfig(xml, tradeLevelLimitList);
            }
            catch (Exception e)
            {
                LogManager.WriteException("重新加载配置文件 PlatConfig.xml  失败!!!" + e.ToString());
                return(-1);
            }

            //没有异常,说明reload的配置文件没有错
            //可以把相关字典替换掉替换了
            lock (_PlatConfigNormalDict)
            {
                _PlatConfigNormalDict = normalDict;
            }
            lock (_PlatConfigWaitingDict)
            {
                _PlatConfigWaitingDict = waitingDict;
            }
            lock (_PlatConfigTradeLevelLimitList)
            {
                _PlatConfigTradeLevelLimitList = tradeLevelLimitList;
            }
            //重新Load配置文件后要重新设定MaxPosCmdNumPer5Seconds的值
            TCPSession.SetMaxPosCmdNumPer5Seconds(8);
            //更新登录配置
            GameManager.loginWaitLogic.LoadConfig();
            return(0);
        }
Beispiel #8
0
        public bool SendTCP(ConnectionKey Key, IPPacket ipPkt)
        {
            Log_Verb("TCP");
            TCP tcp = (TCP)ipPkt.Payload;

            Key.PS2Port = tcp.SourcePort; Key.SRVPort = tcp.DestinationPort;

            if (tcp.DestinationPort == 53 && Utils.memcmp(ipPkt.DestinationIP, 0, DefaultDHCPConfig.DHCP_IP, 0, 4))
            { //DNS
                throw new NotImplementedException("DNS over TCP not supported");
            }

            int res = SendFromConnection(Key, ipPkt);

            if (res == 1)
            {
                return(true);
            }
            else if (res == 0)
            {
                return(false);
            }
            else
            {
                Log_Verb("Creating New Connection with key " + Key);
                Log_Info("Creating New TCP Connection with Dest Port " + tcp.DestinationPort);
                TCPSession s = new TCPSession(Key, adapterIP);
                s.ConnectionClosedEvent += HandleConnectionClosed;
                s.DestIP   = ipPkt.DestinationIP;
                s.SourceIP = dhcpServer.PS2IP;
                if (!connections.TryAdd(Key, s))
                {
                    throw new Exception("Connection Add Failed");
                }
                return(s.Send(ipPkt.Payload));
            }
        }
Beispiel #9
0
 public static void sendMessage <T>(TCPSession session, T message)
 {
     byte[] bytes = toByteArray <T>(message);
     session.Write(bytes);
 }
Beispiel #10
0
 public ProcessSessionTask(TCPSession session)
 {
     this.beginTime = TimeUtil.NowEx();
     this.session   = session;
 }
Beispiel #11
0
 /// <summary> start a channel for the TLS profile.  Besides issuing the
 /// channel start request, it also performs the initiator side
 /// chores necessary to begin encrypted communication using TLS
 /// over a session.  Parameters regarding the type of encryption
 /// and authentication are specified using the profile
 /// configuration passed to the <code>init</code> method Upon
 /// returning, all traffic over the session will be entrusted as
 /// per these parameters.<p>
 /// 
 /// </summary>
 /// <seealso cref="profile configuration">
 /// </seealso>
 /// <param name="session">the session to encrypt communcation for
 /// </param>
 /// <returns> new {@link TCPSession} with TLS negotiated.
 /// @throws BEEPException an error occurs during the channel start
 /// request or the TLS handshake (such as trying to negotiate an
 /// anonymous connection with a peer that doesn't support an
 /// anonymous cipher suite).
 /// </returns>
 public abstract TCPSession startTLS(TCPSession session);
Beispiel #12
0
        //sends the packet and deletes it when done (if successful).rv :true success
        public override bool send(netHeader.NetPacket pkt)
        {
            bool result = false;

            PacketReader.EthernetFrame ef = new PacketReader.EthernetFrame(pkt);

            switch (ef.Protocol)
            {
            case (int)EtherFrameType.NULL:
                //Adapter Reset
                //TODO close all open connections
                break;

            case (int)EtherFrameType.IPv4:
                //Console.Error.WriteLine("IPv4");
                IPPacket ippkt = ((IPPacket)ef.Payload);
                if (ippkt.VerifyCheckSum() == false)
                {
                    Console.Error.WriteLine("IP packet with bad CSUM");
                }
                if (ippkt.Payload.VerifyCheckSum(ippkt.SourceIP, ippkt.DestinationIP) == false)
                {
                    Console.Error.WriteLine("IP packet with bad Payload CSUM");
                }

                string Key = (ippkt.DestinationIP[0]) + "." + (ippkt.DestinationIP[1]) + "." + (ippkt.DestinationIP[2]) + "." + ((UInt64)ippkt.DestinationIP[3])
                             + "-" + (ippkt.Protocol);

                switch (ippkt.Protocol)     //(Prase Payload)
                {
                case (byte)IPType.ICMP:
                    Console.Error.WriteLine("ICMP");
                    lock (sentry)
                    {
                        if (Connections.ContainsKey(Key))
                        {
                            if (Connections[Key].isOpen() == false)
                            {
                                throw new Exception("Attempt to send on Closed Connection");
                            }
                            Console.Error.WriteLine("Found Open Connection");
                            result = Connections[Key].send(ippkt.Payload);
                        }
                        else
                        {
                            Console.Error.WriteLine("Creating New Connection with key " + Key);
                            ICMPSession s = new ICMPSession();
                            s.DestIP   = ippkt.DestinationIP;
                            s.SourceIP = UDP_DHCPsession.PS2_IP;
                            result     = s.send(ippkt.Payload);
                            Connections.Add(Key, s);
                        }
                    }
                    break;

                case (byte)IPType.TCP:
                    //Console.Error.WriteLine("TCP");
                    TCP tcp = (TCP)ippkt.Payload;

                    Key += "-" + ((UInt64)tcp.SourcePort) + ":" + ((UInt64)tcp.DestinationPort);
                    lock (sentry)
                    {
                        if (Connections.ContainsKey(Key))
                        {
                            if (Connections[Key].isOpen() == false)
                            {
                                throw new Exception("Attempt to send on Closed TCP Connection of Key : " + Key + "");
                            }
                            //Console.Error.WriteLine("Found Open Connection");
                            result = Connections[Key].send(ippkt.Payload);
                        }
                        else
                        {
                            //Console.Error.WriteLine("Creating New Connection with key " + Key);
                            Console.Error.WriteLine("Creating New TCP Connection with Dest Port " + tcp.DestinationPort);
                            TCPSession s = new TCPSession();
                            s.DestIP   = ippkt.DestinationIP;
                            s.SourceIP = UDP_DHCPsession.PS2_IP;
                            result     = s.send(ippkt.Payload);
                            Connections.Add(Key, s);
                        }
                    }
                    break;

                case (byte)IPType.UDP:
                    //Console.Error.WriteLine("UDP");
                    UDP udp = (UDP)ippkt.Payload;

                    Key += "-" + ((UInt64)udp.SourcePort) + ":" + ((UInt64)udp.DestinationPort);
                    if (udp.DestinationPort == 67)
                    {         //DHCP
                        result = DCHP_server.send(ippkt.Payload);
                        break;
                    }
                    lock (sentry)
                    {
                        if (Connections.ContainsKey(Key))
                        {
                            if (Connections[Key].isOpen() == false)
                            {
                                throw new Exception("Attempt to send on Closed Connection");
                            }
                            //Console.Error.WriteLine("Found Open Connection");
                            result = Connections[Key].send(ippkt.Payload);
                        }
                        else
                        {
                            //Console.Error.WriteLine("Creating New Connection with key " + Key);
                            Console.Error.WriteLine("Creating New UDP Connection with Dest Port " + udp.DestinationPort);
                            UDPSession s = new UDPSession();
                            s.DestIP   = ippkt.DestinationIP;
                            s.SourceIP = UDP_DHCPsession.PS2_IP;
                            result     = s.send(ippkt.Payload);
                            Connections.Add(Key, s);
                        }
                    }
                    break;

                default:
                    Console.Error.WriteLine("Unkown Protocol");
                    //throw new NotImplementedException();
                    break;
                }
                //Console.Error.WriteLine("Key = " + Key);
                break;

                #region "ARP"
            case (int)EtherFrameType.ARP:
                Console.Error.WriteLine("ARP (Ignoring)");
                ARPPacket arppkt = ((ARPPacket)ef.Payload);

                ////Detect ARP Packet Types
                //if (Utils.memcmp(arppkt.SenderProtocolAddress, 0, new byte[] { 0, 0, 0, 0 }, 0, 4))
                //{
                //    Console.WriteLine("ARP Probe"); //(Who has my IP?)
                //    break;
                //}
                //if (Utils.memcmp(arppkt.SenderProtocolAddress, 0, arppkt.TargetProtocolAddress, 0, 4))
                //{
                //    if (Utils.memcmp(arppkt.TargetHardwareAddress, 0, new byte[] { 0, 0, 0, 0, 0, 0 }, 0, 6) & arppkt.OP == 1)
                //    {
                //        Console.WriteLine("ARP Announcement Type 1");
                //        break;
                //    }
                //    if (Utils.memcmp(arppkt.SenderHardwareAddress, 0, arppkt.TargetHardwareAddress, 0, 6) & arppkt.OP == 2)
                //    {
                //        Console.WriteLine("ARP Announcement Type 2");
                //        break;
                //    }
                //}

                ////if (arppkt.OP == 1) //ARP request
                ////{
                ////    //This didn't work for whatever reason.
                ////    if (Utils.memcmp(arppkt.TargetProtocolAddress,0,UDP_DHCPsession.GATEWAY_IP,0,4))
                ////    //it's trying to resolve the virtual gateway's mac addr
                ////    {
                ////        Console.Error.WriteLine("ARP Attempt to Resolve Gateway Mac");
                ////        arppkt.TargetHardwareAddress = arppkt.SenderHardwareAddress;
                ////        arppkt.SenderHardwareAddress = gateway_mac;
                ////        arppkt.TargetProtocolAddress = arppkt.SenderProtocolAddress;
                ////        arppkt.SenderProtocolAddress = UDP_DHCPsession.GATEWAY_IP;
                ////        arppkt.OP = 2;

                ////        EthernetFrame retARP = new EthernetFrame(arppkt);
                ////        retARP.DestinationMAC = ps2_mac;
                ////        retARP.SourceMAC = gateway_mac;
                ////        retARP.Protocol = (Int16)EtherFrameType.ARP;
                ////        vRecBuffer.Add(retARP.CreatePacket());
                ////        break;
                ////    }
                ////}
                //Console.Error.WriteLine("Unhandled ARP packet");

                result = true;
                break;

                #endregion
            case (int)0x0081:
                Console.Error.WriteLine("VLAN-tagged frame (IEEE 802.1Q)");
                throw new NotImplementedException();

            //break;
            default:
                Console.Error.WriteLine("Unkown EtherframeType " + ef.Protocol.ToString("X4"));
                break;
            }

            return(result);
        }
Beispiel #13
0
 public virtual bool Decode(TCPSession session, PackageBuffer buffer, ProtocolDecoderOutput output)
 {
     return(false);
 }
Beispiel #14
0
 public virtual byte[] Encode(TCPSession session, object message)
 {
     return(null);
 }