예제 #1
0
파일: mainForm.cs 프로젝트: tstorcz/LevelUp
        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);
            }
        }
예제 #2
0
파일: mainForm.cs 프로젝트: tstorcz/LevelUp
        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;
            }
        }