Ejemplo n.º 1
0
        private void ClientReceived(Client sender, TransferPackage package)
        {
            switch (package.TransferAction)
            {
            case TransferAction.Login:
                if (ClientLogedIn != null)
                {
                    ClientLogedIn(sender);
                }
                break;

            case TransferAction.SendMessage:
                MessageModel messageModel = package.Data as MessageModel;
                Client       targetClient = Clients.FirstOrDefault(x => x.Id == messageModel.ClientId);
                if (targetClient != null)
                {
                    messageModel.ClientName = targetClient.Name;

                    TransferPackage pkg = new TransferPackage(TransferAction.SendMessage, new MessageModel
                    {
                        ClientId   = sender.Id,
                        ClientName = sender.Name,
                        Message    = messageModel.Message
                    });

                    targetClient.Socket.Send(pkg.ToByteArray());

                    if (ClientSendMessage != null)
                    {
                        ClientSendMessage(sender, messageModel);
                    }
                }
                break;
            }
        }
Ejemplo n.º 2
0
        private void HandleReceivedData(byte[] buffer)
        {
            MethodInvoker method = null;

            TransferPackage package = new TransferPackage(buffer);

            switch (package.TransferAction)
            {
            case TransferAction.ListClient:
                method = () =>
                {
                    ListClientModel listModel = package.Data as ListClientModel;

                    lstClients.Items.Clear();
                    lstClients.Items.AddRange(listModel.Clients);
                };
                break;

            case TransferAction.SendMessage:
                method = () =>
                {
                    MessageModel messageModel = package.Data as MessageModel;

                    lstMessages.Items.Add(messageModel.ClientName + ": " + messageModel.Message);
                };
                break;
            }

            if (method != null)
            {
                Invoke(method);
            }
        }
Ejemplo n.º 3
0
        public void ReceivedCallback(IAsyncResult ar)
        {
            try
            {
                Socket.EndReceive(ar);

                byte[] buffer   = new byte[8192];
                int    received = Socket.Receive(buffer, buffer.Length, SocketFlags.None);

                if (received < buffer.Length)
                {
                    Array.Resize(ref buffer, received);
                }

                TransferPackage package = new TransferPackage(buffer);
                ProcessPackage(package);

                if (Received != null)
                {
                    Received(this, package);
                }

                Socket.BeginReceive(new byte[] { 0 }, 0, 0, 0, ReceivedCallback, null);
            }
            catch (Exception)
            {
                Close();
            }
        }
Ejemplo n.º 4
0
 private void ProcessPackage(TransferPackage package)
 {
     switch (package.TransferAction)
     {
     case TransferAction.Login:
         Name = ((package.Data) as LoginModel).Name;
         break;
     }
 }
Ejemplo n.º 5
0
        private void ConnectedCallback(IAsyncResult ar)
        {
            sock.EndConnect(ar);

            TransferPackage package = new TransferPackage(TransferAction.Login, new LoginModel {
                Name = txtName.Text
            });

            sock.Send(package.ToByteArray());

            Invoke((MethodInvoker)(() => ChangeStatus(true)));

            sock.BeginReceive(new byte[] { 0 }, 0, 0, 0, ReceivedCallback, null);
        }
Ejemplo n.º 6
0
        private void TimerCallbackAction(object state)
        {
            foreach (var client in server.Clients)
            {
                ListClientModel listClientModel = new ListClientModel
                {
                    Clients = server.Clients.Where(x => x.Id != client.Id).Select(x => new ClientModel {
                        Id = x.Id, Name = x.Name
                    }).ToArray()
                };
                TransferPackage package = new TransferPackage(TransferAction.ListClient, listClientModel);

                client.Socket.Send(package.ToByteArray());
            }
        }
Ejemplo n.º 7
0
        private void btnSend_Click(object sender, EventArgs e)
        {
            if (txtMessage.Text.Trim().Length == 0)
            {
                return;
            }

            lstMessages.Items.Add("Me: " + txtMessage.Text);

            TransferPackage package = new TransferPackage(TransferAction.SendMessage, new MessageModel
            {
                ClientId = Guid.Parse(lblClientId.Text),
                Message  = txtMessage.Text
            });

            sock.Send(package.ToByteArray());
        }