Example #1
0
        public void ReceiveActionMethod(UdpClient socket)
        {      
            while (true)
            {
                try
                {
                    LogService.Logger.LogMessageToXml("Receiving started", null, LogService.LogMessageSeverities.Information);
                    IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
                    byte[] objectBytes = socket.Receive(ref sender);
                    STUNMessage stunMessage = new STUNMessage(objectBytes);
                    if (stunMessage.MessageHeader.MessageType == STUNMessageTypes.Binding_Request)
                    {
                        STUNMessage responseMessage = new STUNMessage(STUNMessageTypes.Binding_SuccessResponse);
                        responseMessage.AddAttribute(new STUNAttributeTLV(STUNAttributeTypes.MAPPED_ADDRESS, new MappedAddress(AddressFamilyTypes.IPv4, (ushort)sender.Port, sender.Address)));
                        socket.Send(responseMessage.Bytes, responseMessage.SizeInBytes, sender);
                    }
                    string message = "Getting objects - Length: " + objectBytes.Length.ToString();
                    if (MessageReceived != null) MessageReceived(this, new MessageReceivedEventArgs() { Message = message});
                    LogService.Logger.LogMessageToXml(message, null, LogService.LogMessageSeverities.Information);
                }
                catch (Exception ex)
                {
                    LogService.Logger.LogMessageToXml(ex.Message, ex, LogService.LogMessageSeverities.Error);
                }

            }
        }
        public void SendBindingRequest()
        {


            STUNMessage stunMessage = new STUNMessage(STUNMessageTypes.Binding_Request);      
            byte[] stunMessageBytes = stunMessage.Bytes;
          
            // Connect the socket to the remote endpoint. Catch any errors.
            try
            {

                _udpSocket.SendTo(stunMessageBytes, _serverEndpoint);
                _udpSocket.Poll(100, SelectMode.SelectRead);
                StartReceiveUdp(_udpSocket);

                //_tcpSocket.Connect(_serverEndpoint);
                //_tcpSocket.Send(stunMessageBytes);
                //_tcpSocket.StartListening();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                throw ex;
            }
        }
        private void StartReceiveUdp(Socket sender)
        {
            Task.Run(() =>
            {
                while (true)
                {
                    try
                    {
                        IPEndPoint remoteEndpoint = new IPEndPoint(IPAddress.Any, 0);
                        EndPoint remote = (EndPoint)remoteEndpoint;

                        byte[] bytes = new byte[short.MaxValue];
                        int messageLength = sender.ReceiveFrom(bytes,ref remote);

                        byte[] receivedBytes = bytes.Take(messageLength).ToArray();

                        STUNMessage stunMessage = new STUNMessage(receivedBytes);
                    }
                    catch (Exception ex)
                    {
                        ErrorOccurredEventArgs args = new ErrorOccurredEventArgs();
                        args.Exception = ex;
                        if (ErrorOccurred != null) ErrorOccurred(this, args);
                        Debug.WriteLine(ex.Message);
                        break;
                    }
                }
            });

        }