public void CloseChannel()
 {
     if (_client != null)
     {
         _client.Shutdown();
     }
 }
Пример #2
0
        public List <BrowseComponent> Execute()
        {
            Clear();

            NetworkChannel         channel = null;
            List <BrowseComponent> list    = new List <BrowseComponent>();

            try
            {
                // Connect
                channel = new NetworkChannel(Connection);

                // Request
                JsonGroupRequestMessage jsonRequestMessage = new JsonGroupRequestMessage();
                JsonPacket jsonRequest = new JsonPacket(jsonRequestMessage);

                HttpRequest httpRequest = new HttpRequest(Session.Id)
                {
                    Data = Session.Encrypt(jsonRequest)
                };
                channel.Send(httpRequest);

                // Response
                HttpResponse httpResponse;
                channel.Receive(out httpResponse);
                Code = httpResponse.Code;

                if (httpResponse.Ok)
                {
                    JsonPacket jsonResponse = JsonPacket.Parse(Session.Decrypt(httpResponse.Data));
                    JsonGroupResponseMessage jsonResponseMessage = JsonGroupResponseMessage.Parse(jsonResponse.Message);

                    if (jsonResponseMessage != null)
                    {
                        // Clients
                        foreach (JsonClient jsonClient in jsonResponseMessage.Clients)
                        {
                            BrowseComponent browse = new BrowseComponent(jsonClient.Name)
                            {
                                ClientId = jsonClient.Id, Owner = Entity
                            };
                            list.Add(browse);
                        }
                    }
#if DEBUG
                    Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse);
#endif
                }
            }
            finally
            {
                if (channel != null)
                {
                    channel.Shutdown();
                }
            }

            return(list);
        }
Пример #3
0
 public void CancelConnectTimer()
 {
     if (_connectTimer != null)
     {
         _connectTimer.Stop();
         _client.Shutdown();
     }
 }
Пример #4
0
        public HttpCode Execute()
        {
            Clear();

            // Connect
            NetworkChannel channel = null;

            try
            {
                channel = new NetworkChannel(Connection);

                // Request
                JsonEntity jsonEntity = (JsonEntity)Entity;
                JsonClient jsonClient = (JsonClient)Client;
                JsonGroup  jsonGroup  = (JsonGroup)Group;

                JsonJoinRequestMessage jsonRequestMessage = new JsonJoinRequestMessage()
                {
                    Entity = jsonEntity, Client = jsonClient, Group = jsonGroup
                };
                JsonPacket jsonRequest = new JsonPacket(jsonRequestMessage);

                HttpRequest httpRequest = new HttpRequest(Session.Id)
                {
                    Data = Session.Encrypt(jsonRequest)
                };
                channel.Send(httpRequest);

                // Response
                HttpResponse httpResponse;
                channel.Receive(out httpResponse);
                Code = httpResponse.Code;

                if (httpResponse.Ok)
                {
                    JsonPacket jsonResponse = JsonPacket.Parse(Session.Decrypt(httpResponse.Data));
                    JsonJoinResponseMessage jsonResponseMessage = JsonJoinResponseMessage.Parse(jsonResponse.Message);

                    // Data
                    Server.Entities = jsonResponseMessage.Entities;
                    Server.Sessions = jsonResponseMessage.Sessions;
                    Server.Clients  = jsonResponseMessage.Clients;
                    Server.Groups   = jsonResponseMessage.Groups;
#if DEBUG
                    Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse);
#endif
                }
            }
            finally
            {
                if (channel != null)
                {
                    channel.Shutdown();
                }
            }

            return(Code);
        }
Пример #5
0
        private void ExecuteThread()
        {
            NetworkChannel channel = null;

            try
            {
                // Connect
                channel = new NetworkChannel(Connection);

                // Request
                JsonSearchRequestMessage jsonRequestMessage = new JsonSearchRequestMessage();

                JsonSearch jsonSearch = new JsonSearch()
                {
                    Keyword = Keyword, Filter = Filter
                };
                JsonSearchRequestData jsonRequestData = new JsonSearchRequestData()
                {
                    Search = jsonSearch
                };
                JsonPacket jsonRequest = new JsonPacket(jsonRequestMessage, Group.Encrypt(jsonRequestData));

                HttpRequest httpRequest = new HttpRequest(Session.Id)
                {
                    Data = Session.Encrypt(jsonRequest)
                };
                channel.Send(httpRequest);

                // Response
                HttpResponse httpResponse;
                channel.Receive(out httpResponse);
                Code = httpResponse.Code;

                if (httpResponse.Ok)
                {
                    JsonPacket jsonResponse = JsonPacket.Parse(Session.Decrypt(httpResponse.Data));
                    JsonSearchResponseMessage jsonResponseMessage = JsonSearchResponseMessage.Parse(jsonResponse.Message);
                    Debug.Assert(!string.IsNullOrEmpty(jsonResponseMessage.Id));

                    // Data
                    SearchList.Id = jsonResponseMessage.Id;
#if DEBUG
                    Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse);
#endif
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (channel != null)
                {
                    channel.Shutdown();
                }
            }
        }
Пример #6
0
        public HttpCode Execute()
        {
            Clear();

            // Connect
            NetworkChannel channel = null;

            try
            {
                channel = new NetworkChannel(Connection);

                // Request
                JsonPingRequestMessage jsonRequestMessage = new JsonPingRequestMessage();
                JsonPacket             jsonRequest        = new JsonPacket(jsonRequestMessage);

                HttpRequest httpRequest = new HttpRequest(Session.Id)
                {
                    Data = Session.Encrypt(jsonRequest)
                };

                // Response
                HttpResponse httpResponse;

                if (Owner)
                {
                    channel.Send(httpRequest);
                    channel.Receive(out httpResponse);
                }
                else
                {
                    lock (Socket)
                    {
                        channel.Send(httpRequest);
                        channel.Receive(out httpResponse);
                    }
                }

                Code = httpResponse.Code;

                if (httpResponse.Ok)
                {
                    JsonPacket jsonResponse = JsonPacket.Parse(Session.Decrypt(httpResponse.Data));
#if DEBUG
                    Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse);
#endif
                }
            }
            finally
            {
                if (channel != null)
                {
                    channel.Shutdown();
                }
            }

            return(Code);
        }
Пример #7
0
 internal override void Shutdown()
 {
     foreach (KeyValuePair <string, NetworkChannel> networkChannel in m_NetworkChannels)
     {
         NetworkChannel nc = networkChannel.Value;
         nc.NetworkChannelConnected -= OnNetworkChannelConnected;
         nc.Shutdown();
     }
     m_NetworkChannels.Clear();
 }
Пример #8
0
        private void ExecuteThread()
        {
            Clear();

            // Connect
            NetworkChannel channel = null;

            try
            {
                channel = new NetworkChannel(Connection);

                // Request
                JsonInfoRequestMessage jsonRequestMessage = new JsonInfoRequestMessage();
                JsonPacket             jsonRequest        = new JsonPacket(jsonRequestMessage);

                HttpRequest httpRequest = new HttpRequest(Session.Id)
                {
                    Data = Session.Encrypt(jsonRequest)
                };
                channel.Send(httpRequest);

                // Response
                HttpResponse httpResponse;
                channel.Receive(out httpResponse);
                Code = httpResponse.Code;

                if (httpResponse.Ok)
                {
                    JsonPacket jsonResponse = JsonPacket.Parse(Session.Decrypt(httpResponse.Data));
                    JsonInfoResponseMessage jsonResponseMessage = JsonInfoResponseMessage.Parse(jsonResponse.Message);

                    // Data
                    Server.Entities = jsonResponseMessage.Entities;
                    Server.Sessions = jsonResponseMessage.Sessions;
                    Server.Clients  = jsonResponseMessage.Clients;
                    Server.Groups   = jsonResponseMessage.Groups;
#if DEBUG
                    Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse);
#endif
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Machine.Status = DemonStatus.Warning;
            }
            finally
            {
                if (channel != null)
                {
                    channel.Shutdown();
                }
            }
        }
Пример #9
0
        public bool DestroyNetworkChannel(string name)
        {
            NetworkChannel nc = null;

            if (m_NetworkChannels.TryGetValue(name ?? string.Empty, out networkChannel))
            {
                nc.NetworkChannelConnected -= OnNetworkChannelConnected;
                nc.Shutdown();
                return(m_NetworkChannels.Remove(name));
            }
            return(false);
        }
        public void CloseConnection()
        {
            if (_timers != null)
            {
                _timers.StopAllTimers();
            }

            if (_client != null)
            {
                NetworkChannel <MQMessage> currClient = _client;
                _client = null;
                currClient.Shutdown();
            }
        }
Пример #11
0
        private void ExecuteThread()
        {
            // Connect
            NetworkChannel channel = null;

            try
            {
                channel = new NetworkChannel(Connection);

                // Request
                JsonPingRequestMessage jsonRequestMessage = new JsonPingRequestMessage();
                JsonPacket             jsonRequest        = new JsonPacket(jsonRequestMessage);

                HttpRequest httpRequest = new HttpRequest(Session.Id)
                {
                    Data = Session.Encrypt(jsonRequest)
                };
                channel.Send(httpRequest);

                // Response
                HttpResponse httpResponse;
                channel.Receive(out httpResponse);
                Code = httpResponse.Code;

                if (httpResponse.Ok)
                {
                    JsonPacket jsonResponse = JsonPacket.Parse(Session.Decrypt(httpResponse.Data));
#if DEBUG
                    Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse);
#endif
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

                if (Owner && Machine != null)
                {
                    Machine.Restart();
                }
            }
            finally
            {
                if (channel != null)
                {
                    channel.Shutdown();
                }
            }
        }
Пример #12
0
        /// <summary>
        /// 关闭并清理网络管理器。
        /// </summary>
        internal override void Shutdown()
        {
            foreach (KeyValuePair <string, NetworkChannel> networkChannel in m_NetworkChannels)
            {
                NetworkChannel nc = networkChannel.Value;
                nc.NetworkChannelConnected     -= OnNetworkChannelConnected;
                nc.NetworkChannelClosed        -= OnNetworkChannelClosed;
                nc.NetworkChannelMissHeartBeat -= OnNetworkChannelMissHeartBeat;
                nc.NetworkChannelError         -= OnNetworkChannelError;
                nc.NetworkChannelCustomError   -= OnNetworkChannelCustomError;
                nc.Shutdown();
            }

            m_NetworkChannels.Clear();
        }
Пример #13
0
        /// <summary>
        /// 销毁网络频道。
        /// </summary>
        /// <param name="name">网络频道名称。</param>
        /// <returns>是否销毁网络频道成功。</returns>
        public bool DestroyNetworkChannel(string name)
        {
            NetworkChannel networkChannel = null;

            if (m_NetworkChannels.TryGetValue(name ?? string.Empty, out networkChannel))
            {
                networkChannel.NetworkChannelConnected     -= OnNetworkChannelConnected;
                networkChannel.NetworkChannelClosed        -= OnNetworkChannelClosed;
                networkChannel.NetworkChannelMissHeartBeat -= OnNetworkChannelMissHeartBeat;
                networkChannel.NetworkChannelError         -= OnNetworkChannelError;
                networkChannel.NetworkChannelCustomError   -= OnNetworkChannelCustomError;
                networkChannel.Shutdown();
                return(m_NetworkChannels.Remove(name));
            }

            return(false);
        }
Пример #14
0
        public HttpCode Execute()
        {
            Clear();

            // Connect
            NetworkChannel channel = null;

            try
            {
                channel = new NetworkChannel(Connection);

                // Request
                HttpRequest httpRequest = new HttpRequest()
                {
                    Data = SecurityUtil.Token
                };
                channel.Send(httpRequest);

                // Response
                HttpResponse httpResponse;
                channel.Receive(out httpResponse);
                Code = httpResponse.Code;

                if (httpResponse.Ok)
                {
                    // Data
                    Session.Id  = httpResponse.Session;
                    Session.Key = Key.Parse(httpResponse.Data);
#if DEBUG
                    Log.Add(httpRequest, httpResponse);
#endif
                }
            }
            finally
            {
                if (channel != null)
                {
                    channel.Shutdown();
                }
            }

            return(Code);
        }
Пример #15
0
        private void ProcessState(object obj)
        {
            Socket         socket  = (Socket)obj;
            NetworkChannel channel = null;

            JsonAction action   = JsonAction.None;
            JsonType   jsonType = JsonType.None;

            try
            {
                do
                {
                    // Connect
                    channel = new NetworkChannel(socket);

                    // Receive
                    HttpRequest httpRequest;
                    channel.Receive(out httpRequest);
                    if (httpRequest == null)
                    {
                        channel.SendBadRequest();
                        return;
                    }

                    // Data
                    Status = DemonStatus.Success;

                    // Handshake
                    if (httpRequest.Session == null)
                    {
                        ServerStatusComponent    status  = Owner.Get <ServerStatusComponent>();
                        HandshakeResponseCommand command = new HandshakeResponseCommand(Owner, socket)
                        {
                            Listener = status.Update
                        };
                        command.Execute(httpRequest);
                        break;
                    }

                    // Session
                    SessionMapComponent sessions = Owner.Get <SessionMapComponent>();
                    Entity entity = sessions.Get(httpRequest.Session);
                    if (entity == null)
                    {
                        channel.SendUnauthorized();
                        break;
                    }

                    // TODO: Version check
                    //
                    // Message
                    SessionComponent session            = entity.Get <SessionComponent>();
                    string           decrypted          = session.Decrypt(httpRequest.Data);
                    JsonPacket       jsonRequest        = JsonPacket.Parse(decrypted);
                    JsonMessage      jsonRequestMessage = JsonMessage.Parse(jsonRequest.Message);
                    jsonType = jsonRequestMessage.Type;

                    switch (jsonType)
                    {
                    case JsonType.Handshake:
                    {
                        HandshakeResponseCommand command = new HandshakeResponseCommand(Owner, socket);
                        command.Execute(httpRequest, jsonRequest, session);
                        break;
                    }

                    case JsonType.Ping:
                    {
                        PingResponseCommand command = new PingResponseCommand(Owner, socket);
                        command.Execute(httpRequest, jsonRequest, session);
                        break;
                    }

                    case JsonType.Info:
                    {
                        InfoResponseCommand command = new InfoResponseCommand(Owner, socket);
                        command.Execute(httpRequest, jsonRequest, session);
                        break;
                    }

                    case JsonType.Join:
                    {
                        JoinResponseCommand command = new JoinResponseCommand(Owner, socket);
                        command.Execute(httpRequest, jsonRequest, session);
                        break;
                    }

                    case JsonType.Tunnel:
                    {
                        TunnelResponseCommand command = new TunnelResponseCommand(Owner, socket);
                        command.Execute(httpRequest, jsonRequest, session);
                        break;
                    }

                    case JsonType.Search:
                    {
                        SearchResponseCommand command = new SearchResponseCommand(Owner, socket);
                        command.Execute(httpRequest, jsonRequest, session);
                        break;
                    }

                    case JsonType.Group:
                    {
                        GroupResponseCommand command = new GroupResponseCommand(Owner, socket);
                        command.Execute(httpRequest, jsonRequest, session);
                        break;
                    }

                    case JsonType.Browse:
                    {
                        BrowseResponseCommand command = new BrowseResponseCommand(Owner, socket);
                        command.Execute(httpRequest, jsonRequest, session);
                        break;
                    }

                    case JsonType.Download:
                    {
                        // TODO: Add max transfer check!
                        //
                        DownloadResponseCommand command = new DownloadResponseCommand(Owner, socket);
                        action = command.Execute(httpRequest, jsonRequest, session);
                        break;
                    }

                    case JsonType.Upload:
                    {
                        UploadResponseCommand command = new UploadResponseCommand(Owner, socket);
                        action = command.Execute(httpRequest, jsonRequest, session);
                        break;
                    }

                    case JsonType.Quit:
                    {
                        QuitResponseCommand command = new QuitResponseCommand(Owner, socket);
                        command.Execute(httpRequest, jsonRequest, session);
                        break;
                    }

                    default:
                    {
                        channel.SendBadRequest();
                        return;
                    }
                    }
                }while (action != JsonAction.None);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Status = DemonStatus.Error;
                channel.SendInternalServerError();
            }
            finally
            {
                if (channel != null)
                {
                    channel.Shutdown();
                }

                if (jsonType != JsonType.Tunnel)
                {
                    socket.Shutdown(SocketShutdown.Both);
                    socket.Close();
                }
            }
        }
Пример #16
0
        public List <BrowseComponent> Execute(string clientId, string fileId)
        {
            Clear();

            NetworkChannel channel = null;

            try
            {
                // Connect
                channel = new NetworkChannel(Connection);

                // Request
                JsonClient jsonClient = new JsonClient(clientId);
                JsonBrowseRequestMessage jsonRequestMessage = new JsonBrowseRequestMessage()
                {
                    Client = jsonClient
                };

                JsonFile jsonFolder = !string.IsNullOrEmpty(fileId) ? new JsonFile(fileId) : null;
                JsonBrowseRequestData jsonRequestData = new JsonBrowseRequestData()
                {
                    Folder = jsonFolder
                };
                JsonPacket jsonRequest = new JsonPacket(jsonRequestMessage, Group.Encrypt(jsonRequestData));

                HttpRequest httpRequest = new HttpRequest(Session.Id)
                {
                    Data = Session.Encrypt(jsonRequest)
                };
                channel.Send(httpRequest);

                // Response
                HttpResponse httpResponse;
                channel.Receive(out httpResponse);
                Code = httpResponse.Code;

                if (httpResponse.Ok)
                {
                    JsonPacket jsonResponse = JsonPacket.Parse(Session.Decrypt(httpResponse.Data));
                    JsonBrowseResponseMessage jsonResponseMessage = JsonBrowseResponseMessage.Parse(jsonResponse.Message);
                    Debug.Assert(jsonResponseMessage != null);

                    JsonBrowseResponseData jsonResponseData = JsonBrowseResponseData.Parse(Group.Decrypt(jsonResponse.Data));
                    Debug.Assert(jsonResponseData != null);

                    // Data
                    List <BrowseComponent> list = new List <BrowseComponent>();

                    // Folders
                    if (jsonResponseData.Folders != null)
                    {
                        foreach (JsonFile json in jsonResponseData.Folders)
                        {
                            BrowseComponent comp = new BrowseComponent(json.Id, json.Name)
                            {
                                ClientId = jsonClient.Id, Owner = Entity
                            };
                            list.Add(comp);
                        }
                    }

                    // Files
                    if (jsonResponseData.Files != null)
                    {
                        foreach (JsonFile json in jsonResponseData.Files)
                        {
                            BrowseComponent comp = new BrowseComponent(json.Id, json.Name, json.Size)
                            {
                                ClientId = jsonClient.Id, Owner = Entity
                            };
                            list.Add(comp);
                        }
                    }
#if DEBUG
                    Log.Add(httpRequest, httpResponse, jsonRequest, jsonResponse);
#endif
                    return(list);
                }
            }
            finally
            {
                if (channel != null)
                {
                    channel.Shutdown();
                }
            }

            return(null);
        }