public Client(String serverHost)
 {
     try
     {
         this.serverHost = serverHost;
         Console.WriteLine("Подключение к {0}", this.serverHost);
         cSocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
         cSocket.Connect(this.serverHost, port);
         net = new NetMessaging(cSocket);
         net.LoginCmdReceived    += OnLogin;
         net.UserListCmdReceived += OnUserList;
         net.StartCmdReceived    += OnStart;
         net.MessageCmdReceived  += OnMessage;
         new Thread(() =>
         {
             try
             {
                 net.Communicate();
             }
             catch (Exception ex)
             {
                 Console.WriteLine("Не удалось получить данные. Завершение соединения...");
             }
         }).Start();
     }
     catch (Exception e)
     {
         Console.WriteLine("Что-то пошло не так... :(");
     }
 }
Exemple #2
0
 public Client(String serverHost)
 {
     try
     {
         this.serverHost = serverHost;
         Console.WriteLine("Подключение к {0}", this.serverHost);
         cSocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
         cSocket.Connect(this.serverHost, port);
         net = new NetMessaging(cSocket);
         net.DataReceived += OnResult;
         //подписки
         new Thread(() =>
         {
             try
             {
                 net.Communicate();
             }
             catch (Exception ex)
             {
                 //exception
             }
         }).Start();
     }
     catch (Exception e)
     {
         //exception
     }
 }
Exemple #3
0
        /// <summary>
        ///     Initializes this instance.
        /// </summary>
        internal static void Initialize()
        {
            while (true)
            {
                string cmd = Console.ReadLine();

                if (!string.IsNullOrEmpty(cmd))
                {
                    if (cmd[0] == '/')
                    {
                        string[] parameters = cmd.Substring(1).Split(' ');

                        switch (parameters[0])
                        {
                        case "shutdown":
                        case "close":
                        case "quit":
                            CmdHandler.Close();
                            break;

                        case "queue":
                            Console.WriteLine("Queues: " + NetMessaging.GetHandler());
                            break;

                        case "test":
                            break;
                        }
                    }
                }
            }
        }
Exemple #4
0
        public void Start()
        {
            isConnected = false;
            try {
                ClientLog.Invoke(String.Format("[Server]: Подключение к {0}", this.serverHost));
                cSocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
                cSocket.Connect(this.serverHost, port);
                net = new NetMessaging(cSocket);

                net.LoginCmdReceived    += OnLogin;
                net.UserListCmdReceived += OnUserList;
                net.StartCmdReceived    += OnStart;
                net.MessageCmdReceived  += OnMessage;

                communicate = new Thread(() => {
                    try {
                        net.Communicate();
                    } catch (Exception) {
                        ClientLog.Invoke("[Server]: Не удалось получить данные. Завершение соединения...");
                    }
                });
                communicate.Start();
            } catch (Exception) {
                ClientLog.Invoke("[Server]: Что-то пошло не так... :(");
            }
        }
Exemple #5
0
        private void Choice_Click(object sender, EventArgs e)
        {
            var    sHost = "DESKTOP-6GCQ6FP";
            String usHost;

            usHost = AddressTB.Text;
            if (usHost.Length > 0)
            {
                sHost = usHost;
            }
            try
            {
                serverHost        = sHost;
                AddressTB.Enabled = false;
                Choice.Enabled    = false;
                AddressTB.Text    = "Подключение к " + sHost;
                cSocket           = new Socket(SocketType.Stream, ProtocolType.Tcp);
                cSocket.Connect(this.serverHost, port);
                net = new NetMessaging(cSocket);
                net.MessageCmdReceived += OnMessage;
                net.EventNeedInverse   += GetAnswer;

                GoMessaging();
                ThreadStart th = new ThreadStart(Communicate);
                t1 = new Thread(th);
                t1.Start();
            }
            catch (Exception)
            {
                OnComment("Что-то пошло не так... :(");
            }
        }
        /// <summary>
        ///     Sends the <see cref="NetMessage"/> to the specified <see cref="NetSocket"/>.
        /// </summary>
        public static void SendMessage(NetSocket socket, byte[] sessionId, NetMessage message)
        {
            if (sessionId == null)
            {
                throw new ArgumentNullException("sessionId");
            }

            NetMessaging.Send(socket, message, sessionId);
        }
Exemple #7
0
 //public string Name { get; private set; }
 public ConnectedClient(Socket s)
 {
     cSocket           = s;
     net               = new NetMessaging(cSocket);
     net.DataReceived += OnData;
     new Thread(() =>
     {
         try
         {
             net.Communicate();
         }
         catch (Exception ex)
         {
             Console.WriteLine("Не удалось получить данные от клиента :(");
             clients.Remove(this);
         }
     }).Start();
 }
Exemple #8
0
 public ConnectedClient(Socket s)
 {
     cSocket = s;
     net     = new NetMessaging(cSocket);
     //net.SendData("LOGIN", "?");
     //net.LoginCmdReceived += OnLogin;
     net.MessageCmdReceived += OnMessage;
     net.AppendCmdReceived  += Append;
     new Thread(() =>
     {
         try
         {
             net.Communicate();
         }
         catch (Exception)
         {
             Console.WriteLine("Не удалось получить данные от клиента :(");
             clients.Remove(this);
         }
     }).Start();
 }
Exemple #9
0
 private void Connecting()
 {
     try
     {
         //Console.WriteLine("Подключение к {0}", this.sHost);
         txtChat.AppendText("Подключение к " + usHost.ToString() + Environment.NewLine);
         cSocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
         cSocket.Connect(usHost, port);
         net = new NetMessaging(cSocket);
         net.LoginCmdReceived += OnLogin;
         //net.SendData("LOGIN", txtUserName.Text);
         net.UserListCmdReceived  += OnUserList;
         net.StartCmdReceived     += OnStart;
         net.MessageCmdReceived   += OnMessage;
         net.CheckNameCmdReceived += OnCheckName;
         new Thread(() =>
         {
             try
             {
                 net.Communicate();
             }
             catch (Exception ex)
             {
                 Console.WriteLine("Не удалось получить данные. Завершение соединения...");
                 //txtChat.AppendText("Не удалось получить данные. Завершение соединения...");
             }
         }).Start();
     }
     catch (Exception e)
     {
         //Console.WriteLine("Что-то пошло не так... :(");
         txtChat.AppendText("Что-то пошло не так... :(");
         Stop();
         btnConnect.Text     = "Подключить";
         txtUserName.Enabled = true;
         txtHost.Enabled     = true;
         btnSend.Enabled     = false;
     }
 }
Exemple #10
0
        private void Choice_Click(object sender, EventArgs e)
        {
            var    sHost = "SMAKBOOK";
            String usHost;

            usHost             = AddressTB.Text;
            userNameTB.Enabled = true;
            Entry.Enabled      = true;
            if (usHost.Length > 0)
            {
                sHost = usHost;
            }
            try
            {
                serverHost        = sHost;
                AddressTB.Enabled = false;
                Choice.Enabled    = false;
                ChatTB.AppendText("Подключение к " + sHost + Environment.NewLine);
                cSocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
                cSocket.Connect(this.serverHost, port);
                net = new NetMessaging(cSocket);
                net.LoginCmdReceived    += OnLogin;
                net.UserListCmdReceived += OnUserList;
                net.StartCmdReceived    += OnStart;
                net.MessageCmdReceived  += OnMessage;
                net.EventComment        += OnComment;
                net.EventError          += OnNameError;
                //
                ThreadStart th = new ThreadStart(Communicate);
                t1 = new Thread(th);
                t1.Start();
            }
            catch (Exception ex)
            {
                OnComment("Что-то пошло не так... :(");
            }
        }
Exemple #11
0
 public ConnectedCLient(Socket s, Server serv)
 {
     this.serv = serv;
     cSocket   = s;
     net       = new NetMessaging(cSocket);
     //net.SendData("LOGIN", "?");
     net.LoginCmdReceived    += OnLogin;
     net.MessageCmdReceived  += OnMessage;
     net.UserListCmdReceived += OnUsers;
     net.PrivateReceived     += OnPrivate;
     net.SendInfo            += OnLog;
     new Thread(() =>
     {
         try
         {
             net.Communicate();
         }
         catch (Exception ex)
         {
             Console.WriteLine("Не удалось получить данные от клиента :(");
             clients.Remove(this);
         }
     }).Start();
 }
 /// <summary>
 ///     Sends the <see cref="NetMessage"/> to the specified <see cref="NetSocket"/>.
 /// </summary>
 public static void SendMessage(NetSocket socket, NetMessage message)
 {
     NetMessaging.Send(socket, message);
 }
 /// <summary>
 ///     Sends the <see cref="NetMessage"/> to the specified <see cref="NetSocket"/>.
 /// </summary>
 public static void SendMessage(int serviceNodeType, int serviceNodeId, byte[] sessionId, NetMessage message)
 {
     NetMessaging.Send(serviceNodeType, serviceNodeId, message, sessionId);
 }
Exemple #14
0
 /// <summary>
 ///     Initializes the net part.
 /// </summary>
 private static void InitNet(NetMessageManager messageManager)
 {
     NetManager.Initialize();
     NetMessaging.Initialize();
     NetMessaging.SetMessageManager(messageManager);
 }