Пример #1
0
 public void PutClient(string auth, s2hClient Client)
 {
     lock (m_Clients)
     {
         m_Clients.Add(auth, Client);
     }
 }
Пример #2
0
        public long CloseAll(string auth)
        {
            S2hState  s2hState = (S2hState)Application["s2hState"];
            s2hClient Client   = s2hState.GetClient(auth);

            if (Client == null)
            {
                return((long)s2h_errors.s2h_client_not_authenticated);
            }
            Client = null;
            s2hState.RemoveClient(auth);
            return((long)s2h_errors.s2h_ok);
        }
Пример #3
0
        public long Recv(string a, int Seq, int WaitTimeout, out ExData[] d)
        {
            System.Diagnostics.Debug.WriteLine(this.ToString() + ": requested seq=" + Seq.ToString());
            d = null;
            int       tmpWaitTimeout = WaitTimeout;
            S2hState  s2hState       = (S2hState)Application["s2hState"];
            s2hClient Client         = s2hState.GetClient(a);

            // auth error
            if (Client == null)
            {
                return((long)s2h_errors.s2h_client_not_authenticated);
            }

            lock (Client)
            {
                // the client wants the last packet; last communication failed?
                if (Seq == Client.LastRecvSequenceId)
                {
                    d = Client.LastRecvData;
                    return((long)s2h_errors.s2h_ok);                     // sending last recieved data
                }
            }

            // try to recieve data on all active connections
            while (true)
            {
                s2hState.Exchanger.DeQueueData(out d);

                if ((d == null || d.Length == 0) && tmpWaitTimeout > 0)
                {
                    System.Threading.Thread.Sleep(tmpWaitTimeout < 100?tmpWaitTimeout:100);
                    tmpWaitTimeout -= 100;
                }
                else
                {
                    break;
                }
            }


            // remember the last sent packet
            lock (Client)
            {
                // NOTE: store current value, also if the packet is null
                Client.LastRecvData       = d;
                Client.LastRecvSequenceId = Seq;
            }
            return((long)s2h_errors.s2h_ok);
        }
Пример #4
0
        public void RemoveClient(string auth)
        {
            s2hClient Client = GetClient(auth);

            if (Client != null)
            {
                lock (m_Clients)
                {
                    string [] connectionHandles = Client.ConnectionHandles;
                    foreach (string s in connectionHandles)
                    {
                        m_DataExchanger.CloseConnection(s);
                    }
                    Client.CloseAllConnections();
                    m_Clients.Remove(auth);
                }
            }
        }
Пример #5
0
        public long Auth(string usr, string pwd, out string auth)
        {
            S2hState  s2hState = (S2hState)Application["s2hState"];
            s2hClient Client   = new s2hClient();

            auth = null;

            UserManager.user_info user = m_userManager.GetUser(usr);
            if (user == null || pwd != user.pass)
            {
                return((long)s2h_errors.s2h_invalid_credentials);
            }

            auth = Client.AuthToken = s2hState.GetNewAuthToken();
            Client.ClientAddress = IPAddress.Parse(this.Context.Request.UserHostAddress);
            s2hState.PutClient(auth, Client);

            return((long)s2h_errors.s2h_ok);
        }
Пример #6
0
        public long Send(string a, ExData[] d, int Seq)
        {
            System.Diagnostics.Debug.WriteLine(this.ToString() + ": sent seq=" + Seq.ToString());
            S2hState  s2hState = (S2hState)Application["s2hState"];
            s2hClient Client   = s2hState.GetClient(a);

            if (Client == null)
            {
                return((long)s2h_errors.s2h_client_not_authenticated);
            }

            if (Seq == Client.LastSendSequenceId)
            {
                return((long)s2h_errors.s2h_ok);                 // already processed data
            }
            // just send recv data to the data exchanger
            s2hState.Exchanger.EnqueueData(d);

            return((long)s2h_errors.s2h_ok);
        }
Пример #7
0
        public long Ready(string auth, string Token)
        {
            S2hState  s2hState = (S2hState)Application["s2hState"];
            s2hClient Client   = s2hState.GetClient(auth);

            if (Client == null)
            {
                return((long)s2h_errors.s2h_client_not_authenticated);
            }

            s2hConnection connection = Client.GetConnection(Token);

            if (connection == null)
            {
                return((long)s2h_errors.s2h_unknown_connection_token);
            }

            connection.bReady = true;

            return((long)s2h_errors.s2h_ok);
        }
Пример #8
0
        public long Conn(string auth, string host, int port, out string Token, out byte[] iplAddr, out int lPort, out byte[] iprAddr, out int rPort)
        {
            iprAddr = iplAddr = new byte[4];
            lPort   = 0;
            rPort   = port;
            Token   = "";
            S2hState  s2hState = (S2hState)Application["s2hState"];
            s2hClient Client   = s2hState.GetClient(auth);

            if (Client == null)
            {
                return((long)s2h_errors.s2h_client_not_authenticated);
            }
            s2hConnection connection = null;

            s2h_errors error = s2hState.Exchanger.CreateConnection(host, port, out connection);

            if (error != s2h_errors.s2h_ok)
            {
                return((long)error);
            }
            Client.AddConnection(connection.handle, connection);

            Token = connection.handle;
            if (connection.socket.LocalEndPoint is IPEndPoint)
            {
                IPEndPoint ip = (IPEndPoint)connection.socket.LocalEndPoint;
                iplAddr = ip.Address.GetAddressBytes();
                lPort   = ip.Port;
            }
            if (connection.socket.RemoteEndPoint is IPEndPoint)
            {
                IPEndPoint ip = (IPEndPoint)connection.socket.RemoteEndPoint;
                iprAddr = ip.Address.GetAddressBytes();
                rPort   = ip.Port;
            }
            return((long)s2h_errors.s2h_ok);
        }
Пример #9
0
 public s2hClient GetClient(string auth)
 {
     lock (m_Clients)
     {
         if (m_Clients.Contains(auth))
         {
             s2hClient client = (s2hClient)m_Clients[auth];
             long      nowIs  = DateTime.Now.ToFileTime();
             if (((nowIs - client.LastSeen) / (10 * 1000000)) > 60 * 5)
             {
                 client.CloseAllConnections();
                 m_Clients.Remove(client.AuthToken);
                 return(null);
             }
             client.ResetLastSeen();
             return(client);
         }
         else
         {
             return(null);
         }
     }
 }
Пример #10
0
        public long Close(string auth, string[] Tokens)
        {
            long      lRes     = (long)s2h_errors.s2h_ok;
            S2hState  s2hState = (S2hState)Application["s2hState"];
            s2hClient Client   = s2hState.GetClient(auth);

            if (Client == null)
            {
                return((long)s2h_errors.s2h_client_not_authenticated);
            }

            foreach (string t in Tokens)
            {
                s2hConnection connection = Client.GetConnection(t);
                if (connection == null)
                {
                    lRes = (long)s2h_errors.s2h_unknown_connection_token;
                }
                s2hState.Exchanger.CloseConnection(t);
                Client.RemoveConnection(connection.handle);
            }
            return(lRes);
        }