Beispiel #1
0
 void ListenServer()
 {
     while (true)
     {
         byte[] incomingPackage = null;
         try
         {
             incomingPackage = TCPMessageHandler.Read(this.server_client);
         }
         catch
         {
             C_OnDisconnected();
             return;
         }
         C_OnReceivedMessage(incomingPackage);
     }
 }
Beispiel #2
0
        void ListenClient(SharpClient client)
        {
            while (true)
            {
                byte[] package = null;
                try
                {
                    package = TCPMessageHandler.Read(client.Connection);
                }
                catch
                {
                    S_OnClientDisconnected(client);
                    return;
                }

                S_OnReceivedMessage(client, package);
            }
        }
Beispiel #3
0
        void HandleIncomingConnection(TcpClient client)
        {
            Stopwatch timeout = Stopwatch.StartNew();


            LockableBool isdone = new LockableBool();

            byte[] output = TCPMessageHandler.Read(client);
            //LockableObject<byte[]> output = null;
            //Thread thread;

            //TCPMessageHandler.Read(client, isdone, output, out thread);

            //while (!isdone)
            //{
            //    Thread.Sleep(1);
            //    if (timeout.ElapsedMilliseconds > this.timeout)
            //    {
            //        try { thread.Abort(); thread = null; } catch { }
            //        TCPMessageHandler.CloseConnection(client);
            //        return;
            //    }
            //}
            SharpSerializer package = SharpSerializer.Create(output);

            try
            {
                HandleIncomingConnectionRequest(package, client);
            }
            catch (Exception e)
            {
                TCPMessageHandler.CloseConnection(client);
            }

            package.Post();
        }
Beispiel #4
0
        public bool Connect(string IP, int Port, string Password, SharpClient Self, out ConnectResults result)
        {
            if (Password == null)
            {
                Password = "";
            }
            if (this.connected)
            {
                result = ConnectResults.AlreadyConnected;
                return(false);
            }
            try
            {
                server_client = new TcpClient();
                IPAddress address = null;
                #region IP Parse
                try
                {
                    address = IPAddress.Parse(IP);
                }
                catch
                {
                    result = ConnectResults.InvalidIpAddress;
                    return(false);
                }
                #endregion
                IPEndPoint remoteAddress = new IPEndPoint(address, Port);

                #region Physical Connetion
                try
                {
                    server_client.Connect(remoteAddress);
                }
                catch
                {
                    result = ConnectResults.UnhandledException;
                    return(false);
                }
                #endregion

                #region ID Self
                SharpSerializer hailMessage = SharpSerializer.Create();

                hailMessage.Write((byte)IncomingConnectionRequests.ConnectionApprove);
                Self.Write(ref hailMessage);
                hailMessage.Write(Password);

                TCPMessageHandler.Write(server_client, hailMessage.DataAndPost());
                #endregion

                #region Wait For Response
                byte[]          responseRaw = TCPMessageHandler.Read(server_client);
                SharpSerializer response    = SharpSerializer.Create(responseRaw);
                #endregion

                IncomingConnectionResponse message = (IncomingConnectionResponse)response.ReadByte();
                if (message == IncomingConnectionResponse.WrongPassword)
                {
                    TCPMessageHandler.CloseConnection(server_client);
                    result = ConnectResults.InvalidPassword;
                    return(false);
                }
                if (message == IncomingConnectionResponse.ServerIsFull)
                {
                    TCPMessageHandler.CloseConnection(server_client);
                    result = ConnectResults.ServerIsFull;
                    return(false);
                }
                if (message == IncomingConnectionResponse.NameIsExist)
                {
                    TCPMessageHandler.CloseConnection(server_client);
                    result = ConnectResults.NameIsExist;
                    return(false);
                }


                short myID = response.ReadInt16();
                Self.AssingNetID(myID);

                this.room = new SharpRoom(ref response);

                this.listenerThread = new Thread(ListenServer);
                this.listenerThread.Start();

                connected = true;

                me = Self;
                room.AssignClient(me);

                room.Server.Connection      = server_client;
                room.Server.ListeningThread = listenerThread;

                C_OnConnected(room);

                result = ConnectResults.Succes;
                return(true);
            }
            catch (Exception e)
            {
                try
                {
                    TCPMessageHandler.CloseConnection(server_client);
                }
                catch { }
                server_client = null;

                connected = false;

                try
                {
                    listenerThread.Abort();
                }
                catch { }
                listenerThread = null;

                me = null;

                room = null;

                result = ConnectResults.UnhandledException;
                return(false);
            }
        }