Ejemplo n.º 1
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                IPEndPoint remoteEndpoint = null;
                byte[] receivedBytes = client.EndReceive(ar, ref remoteEndpoint);
                UdpMessage resp = new UdpMessage(receivedBytes);

                ProcessResponse(remoteEndpoint, resp);

                if(resp.Type == UdpMessage.Types.close)
                {
                    StopListening();
                }
                else
                {
                    client.BeginReceive(new AsyncCallback(this.ReceiveCallback), null);
                }
            }
            catch (Exception ex)
            {
                AddLogItem("  E", ex.Message);
            }
        }
Ejemplo n.º 2
0
        private void ProcessResponse(IPEndPoint from, UdpMessage resp)
        {
            if (resp.Type != UdpMessage.Types.message)
            {
                AddLogItem(
                    string.Format("{0}:{1} ->", from.Address, from.Port),
                    resp.Text
                );
            }
            else
            {
                var sender = clientCollection.FirstOrDefault(x => String.Compare(x.Id, resp.Parameters[0]) == 0);

                AddLogItem(
                    sender != null ? string.Format("{0}:{1} ->", sender.Endpoint.Address, sender.Endpoint.Port) : "  N/A ->",
                    resp.Text
                );
            }

            switch (resp.Type)
            {
                case UdpMessage.Types.register:
                    clientCollection.Add(new Client(resp.Parameters[0], new IPEndPoint(from.Address, Convert.ToInt32(resp.Parameters[1]))));
                    break;

                case UdpMessage.Types.message:
                    ForwardMessage(resp.Parameters[0], resp.Parameters[1]);
                    break;

                case UdpMessage.Types.close:
                    break;
            }
        }