Example #1
0
        void ListenTo(TcpClient tcp)
        {
            var stream = tcp.GetStream();
            var data   = ConnectionHelper.Receive2(stream);
            // ==== 1. Nachricht ====
            var m = new PrimaryMessage();

            m.Load(data);
            // ---- Überprüfe den Typ der Nachricht ----
            if (m.MessageType != PrimaryMessageType.WantToConnect)
            {
                ConnectionHelper.Send2(stream,
                                       CreateFail(PrimaryMessageType.ConnectFailed_ExpectLogin));
                return;
            }
            // ---- Überprüfe die Identifikation des Clienten ----
            var id  = m.ClientData.GetLoadSaveAble <CurrentIdentification>();
            var cid = base.Manager.CurrentId;

            if (id.StaticIdentification != cid.StaticIdentification ||
                id.Version != cid.Version)
            {
                var b = CreateFail(PrimaryMessageType.ConnectFailed_WrongKey);
                b.ClientData.SetLoadSaveAble(cid);
                ConnectionHelper.Send2(stream, b);
                return;
            }
            // ---- Überprüfe den Platz auf dem Server ----
            if (base.Manager.Users.Count >= base.Manager.Users.MaxCount)
            {
                ConnectionHelper.Send2(stream,
                                       CreateFail(PrimaryMessageType.ConnectFailed_FullServer));
                return;
            }
            // ---- Lege Nutzerdaten an ----
            var user = Manager.Users.AddNewUser();

            user.GlobalId = id.Id;
            Connection con = new Connection(ConnectorProtocol.TCP,
                                            (tcp.Client.LocalEndPoint as IPEndPoint).Port);
            int ctr = Manager.DefaultDataTransport.ConnectorId;

            if (GetUserConnection != null)
            {
                GetUserConnection(user, ref con, ref ctr);
            }
            user.DefaultConnection = con;
            user.DefaultConnector  = ctr;
            if (AddConnection != null)
            {
                AddConnection(tcp, con);
            }
            else
            {
                (Manager.DefaultDataTransport as DataTransport2).AddConnection(tcp, con);
            }
            // ==== 2. Nachricht - Die Verbindung wurde stattgegeben ====
            var pm = new PrimaryMessage();

            pm.MessageType = PrimaryMessageType.ConnectAllowed;
            ConnectionHelper.Send2(stream, pm);
        }
Example #2
0
 void RunLoop()
 {
     while (base.ProgressRun)
     {
         if (State == LoginState.IsConnecting)
         {
             try
             {
                 var ip = new IPEndPoint(IPAddress.Parse(ConnectTo),
                                         ServerPort);
                 var pm = new PrimaryMessage();
                 pm.MessageType = PrimaryMessageType.WantToConnect;
                 pm.ClientData.SetLoadSaveAble(Manager.CurrentId);
                 var tcp = new TcpClient();
                 tcp.Connect(ip);
                 var stream = tcp.GetStream();
                 ConnectionHelper.Send2(stream, pm);
                 pm.Load(ConnectionHelper.Receive2(stream));
                 ExceptedId = null;
                 if (pm.MessageType == PrimaryMessageType.ConnectAllowed)
                 {
                     State = LoginState.Connected;
                     var con = new Connection(ConnectorProtocol.TCP,
                                              (tcp.Client.LocalEndPoint as IPEndPoint).Port,
                                              ConnectTo);
                     var user = Manager.Users.AddNewUser();
                     user.GlobalId          = pm.SenderID;
                     user.DefaultConnection = con;
                     user.DefaultConnector  = Manager.DefaultDataTransport != null ?
                                              Manager.DefaultDataTransport.ConnectorId : -1;
                     if (Connected != null)
                     {
                         Connected(user);
                     }
                     if (AddConnection != null)
                     {
                         AddConnection(tcp, con);
                     }
                     else
                     {
                         (Manager.DefaultDataTransport as DataTransport2).AddConnection(tcp, con);
                     }
                 }
                 else if (pm.MessageType == PrimaryMessageType.ConnectFailed_FullServer)
                 {
                     State = LoginState.ServerFull;
                     if (ServerFull != null)
                     {
                         ServerFull();
                     }
                 }
                 else if (pm.MessageType == PrimaryMessageType.ConnectFailed_WrongKey)
                 {
                     State      = LoginState.WrongID;
                     ExceptedId = pm.ClientData.GetLoadSaveAble <CurrentIdentification>();
                     if (ErrorWhileConnection != null)
                     {
                         ErrorWhileConnection();
                     }
                 }
                 else
                 {
                     State = LoginState.NotConnectable;
                     if (ErrorWhileConnection != null)
                     {
                         ErrorWhileConnection();
                     }
                 }
             }
             catch
             {
                 State = LoginState.NotConnectable;
                 if (ErrorWhileConnection != null)
                 {
                     ErrorWhileConnection();
                 }
             }
         }
         Thread.Sleep(10);
     }
 }
Example #3
0
        void RunLoop()
        {
            using (var udp = new UdpClient(new IPEndPoint(IPAddress.Any, Connections[0].Port)))
                while (ProgressRun)
                {
                    //Kurzerklärung:
                    //Ein Aufrufsignal mit einem ID-Paket wird geschickt. Dieses wird eingelesen und ein User wird erstellt.
                    //Eine Zielkooridinate wird erstellt und rückgesendet. Fertig.
                    while (udp.Available > 0)
                    {
                        var ep   = new IPEndPoint(IPAddress.Any, 0);
                        var data = udp.Receive(ref ep);
                        //try
                        //{
                        var m = new PrimaryMessage();
                        m.Load(data);
                        byte[] b;
                        if (m.MessageType != PrimaryMessageType.WantToConnect)
                        {
                            b = CreateFail(PrimaryMessageType.ConnectFailed_ExpectLogin).Save();
                            udp.Send(b, b.Length, ep);
                            continue;
                        }
                        var id  = m.ClientData.GetLoadSaveAble <CurrentIdentification>();
                        var cid = base.Manager.CurrentId;
                        if (id.StaticIdentification != cid.StaticIdentification || id.Version != cid.Version)
                        {
                            b = CreateFail(PrimaryMessageType.ConnectFailed_WrongKey).Save();
                            udp.Send(b, b.Length, ep);
                            continue;
                        }
                        if (base.Manager.Users.Count >= base.Manager.Users.MaxCount)
                        {
                            b = CreateFail(PrimaryMessageType.ConnectFailed_FullServer).Save();
                            udp.Send(b, b.Length, ep);
                            continue;
                        }
                        var user = Manager.Users.AddNewUser();
                        user.GlobalId = id.Id;
                        Connection con;
                        int        ctr;
                        if (GetUserConnection != null)
                        {
                            GetUserConnection(user, out con, out ctr);
                        }
                        else
                        {
                            ctr = Manager.DefaultDataTransport.ConnectorId;
                            con = Manager.DefaultDataTransport.Connections.GetFree();
                            Manager.FindConnector(ctr).Connections[con] = true;
                        }
                        user.DefaultConnection = con;
                        user.DefaultConnector  = ctr;
                        var pm = new PrimaryMessage();
                        pm.MessageType = PrimaryMessageType.ConnectAllowed;
                        pm.ClientData.SetSerializeAble(con);
                        b = pm.Save();
                        udp.Send(b, b.Length, ep);
                        //}
                        //catch
                        //{
                        //    var b = CreateFail(PrimaryMessageType.ConnectFailed).Save();
                        //    udp.Send(b, b.Length, ep);
                        //    continue;
                        //}
                    }

                    if (base.ProgressRun)
                    {
                        Thread.Sleep(10);
                    }
                }
        }
Example #4
0
        //public event GetUserConnectionHandler CreateServerUser;

        void RunLoop()
        {
            while (base.ProgressRun)
            {
                if (State == LoginState.IsConnecting)
                {
                    try
                    {
                        var ip = new IPEndPoint(IPAddress.Parse(ConnectTo), ServerPort);
                        var pm = new PrimaryMessage();
                        pm.MessageType = PrimaryMessageType.WantToConnect;
                        pm.ClientData.SetLoadSaveAble(Manager.CurrentId);
                        using (var udp = new UdpClient())
                        {
                            var b = pm.Save();
                            udp.Send(b, b.Length, ip);
                            for (int i = 0; i < 500; ++i)
                            {
                                if (udp.Available != 0)
                                {
                                    break;
                                }
                                Thread.Sleep(1);
                            }
                            b = udp.Receive(ref ip);
                            pm.Load(b);
                            if (pm.MessageType == PrimaryMessageType.ConnectAllowed)
                            {
                                State = LoginState.Connected;
                                var con = pm.ClientData.GetSerializeAble <Connection>();
                                con = new Connection(con.Protocol, con.Port, ConnectTo);
                                var user = Manager.Users.AddNewUser();
                                user.GlobalId          = pm.SenderID;
                                user.DefaultConnection = con;
                                user.DefaultConnector  = Manager.DefaultDataTransport != null ?
                                                         Manager.DefaultDataTransport.ConnectorId : -1;
                                if (Connected != null)
                                {
                                    Connected(con);
                                }
                            }
                            else if (pm.MessageType == PrimaryMessageType.ConnectFailed_FullServer)
                            {
                                State = LoginState.ServerFull;
                                if (ServerFull != null)
                                {
                                    ServerFull();
                                }
                            }
                            else if (pm.MessageType == PrimaryMessageType.ConnectFailed_WrongKey)
                            {
                                State = LoginState.WrongID;
                                if (ErrorWhileConnection != null)
                                {
                                    ErrorWhileConnection();
                                }
                            }
                            else
                            {
                                State = LoginState.NotConnectable;
                                if (ErrorWhileConnection != null)
                                {
                                    ErrorWhileConnection();
                                }
                            }
                        }
                    }
                    catch
                    {
                        State = LoginState.NotConnectable;
                        if (ErrorWhileConnection != null)
                        {
                            ErrorWhileConnection();
                        }
                    }
                }
                Thread.Sleep(10);
            }
        }
Example #5
0
 void doCont()
 {
     using (var stream = tcp.GetStream())
         while (active)
         {
             if (!tcp.Connected)
             {
                 cl = new ConnectionLostEventArgument(datatr, con, null);
                 return;
             }
             DoPing();
             while (tcp.Available > 0)
             {
                 var data = ConnectionHelper.Receive2(stream);
                 try
                 {
                     var mes = new PrimaryMessage();
                     mes.Load(data);
                     mes.MessageRoot.Connection = con;
                     mes.MessageRoot.Connector  = connector;
                     mes.MessageRoot.RemoteId   = id;
                     var task = new Task(() => Manager.ReceiveMessage(mes));
                     task.Start();
                 }
                 catch
                 {
                     if (tcp.Connected)
                     {
                         continue;
                     }
                     else
                     {
                         cl = new ConnectionLostEventArgument(datatr, con, null);
                         return;
                     }
                 }
             }
             while (messages.Count > 0)
             {
                 var mes = messages.Dequeue();
                 if (mes == null)
                 {
                     continue;
                 }
                 if (!tcp.Connected)
                 {
                     cl = new ConnectionLostEventArgument(datatr, con, mes);
                     return;
                 }
                 mes.SenderID = Manager.CurrentId.Id;
                 try { ConnectionHelper.Send2(stream, mes); }
                 catch
                 {
                     cl = new ConnectionLostEventArgument(datatr, con, mes);
                     return;
                 }
             }
             if (active)
             {
                 if (!tcp.Connected)
                 {
                     cl = new ConnectionLostEventArgument(datatr, con, null);
                     return;
                 }
                 Thread.Sleep(10);
             }
         }
 }
Example #6
0
 void doConct(TcpClient tcp)
 {
     using (var stream = tcp.GetStream())
         while (active)
         {
             if (!tcp.Connected)
             {
                 cl = new ConnectionLostEventArgument(datatr, con, null);
                 return;
             }
             DoPing();
             while (tcp.Available > 0)
             {
                 var l = new List <byte>();
                 var b = new byte[tcp.ReceiveBufferSize];
                 int len;
                 while (true)
                 {
                     len = stream.Read(b, 0, b.Length);
                     l.AddRange(b.ToList().GetRange(0, len).ToArray());
                     if (len < b.Length)
                     {
                         break;
                     }
                 }
                 try
                 {
                     var mes = new PrimaryMessage();
                     mes.Load(l.ToArray());
                     mes.MessageRoot.Connection = con;
                     mes.MessageRoot.Connector  = connector;
                     mes.MessageRoot.RemoteId   = id;
                     Manager.ReceiveMessage(mes);
                 }
                 catch
                 {
                     if (tcp.Connected)
                     {
                         continue;
                     }
                     else
                     {
                         cl = new ConnectionLostEventArgument(datatr, con, null);
                         return;
                     }
                 }
             }
             while (messages.Count > 0)
             {
                 var mes = messages.Dequeue();
                 if (mes == null)
                 {
                     continue;
                 }
                 if (!tcp.Connected)
                 {
                     cl = new ConnectionLostEventArgument(datatr, con, mes);
                     return;
                 }
                 mes.SenderID = Manager.CurrentId.Id;
                 var b = mes.Save();
                 try
                 {
                     stream.Write(b, 0, b.Length);
                 }
                 catch
                 {
                     cl = new ConnectionLostEventArgument(datatr, con, mes);
                     return;
                 }
             }
             if (active)
             {
                 if (!tcp.Connected)
                 {
                     cl = new ConnectionLostEventArgument(datatr, con, null);
                     return;
                 }
                 Thread.Sleep(10);
             }
         }
 }