Beispiel #1
0
 public void InitUDP()
 {
     if (OwningConnection.MyTCPSocket != null)
     {
         m_SendArgsUDP = SocketAsyncEventArgsCache.PopSendEventArg(new EventHandler <SocketAsyncEventArgs>(IO_Completed), OwningConnection.MyUDPSocket);
     }
     else
     {
         throw new ArgumentException("AsynchTransitStrategy can't initialize UDP until the NetworkConnection has a valid UDPSocket. Currently UDPSocket is null.");
     }
 }
Beispiel #2
0
        public void StopListening()
        {
            try
            {
                m_ShuttingDown = true;

                if (Socket != null)
                {
                    Log.LogMsg("Shutting down UDP listener Socket. ");
                    StackTrace   stackTrace  = new StackTrace();        // get call stack
                    StackFrame[] stackFrames = stackTrace.GetFrames();  // get method calls (frames)

                    // write call stack method names
                    foreach (StackFrame stackFrame in stackFrames)
                    {
                        Log.LogMsg(stackFrame.GetFileName() + ", " + stackFrame.GetFileLineNumber() + " :" + stackFrame.GetMethod().Name);   // write method name
                    }

                    Socket.Shutdown(SocketShutdown.Both);
                }
            }
            catch (Exception e)
            {
            }
            finally
            {
                if (Socket != null)
                {
                    Log.LogMsg("Closing UDP listener Socket.");
                    Socket.Close();
                    Socket = null;
                }
                if (m_ListenArgs != null)
                {
                    for (int i = 0; i < m_ListenArgs.Count; i++)
                    {
                        SocketAsyncEventArgsCache.PushReadEventArg(m_ListenArgs[i], new EventHandler <SocketAsyncEventArgs>(ReadCompleted));
                    }
                }
                Listening = false;
            }
        }
Beispiel #3
0
        public void AfterShutdown()
        {
            if (m_RecArgs != null)
            {
                SocketAsyncEventArgsCache.PushReadEventArg(m_RecArgs, new EventHandler <SocketAsyncEventArgs>(IO_Completed));
                m_RecArgs = null;
            }

            if (m_SendArgs != null)
            {
                SocketAsyncEventArgsCache.PushSendEventArg(m_SendArgs, new EventHandler <SocketAsyncEventArgs>(IO_Completed));
                m_SendArgs = null;
            }

            if (m_SendArgsUDP != null)
            {
                SocketAsyncEventArgsCache.PushSendEventArg(m_SendArgsUDP, new EventHandler <SocketAsyncEventArgs>(IO_Completed));
                m_SendArgsUDP = null;
            }
        }
Beispiel #4
0
        public bool StartListening(AddressFamily family, int port, int maxSimultaneousListens, Func <IPEndPoint, INetworkConnection> getConMethod)
        {
            try
            {
                GetConnnection = getConMethod;
                if (Socket == null)
                {
                    //// Log.LogMsg("Testy 30");
                    Socket = new System.Net.Sockets.Socket(family, SocketType.Dgram, ProtocolType.Udp);
                    //// Log.LogMsg("Testy 31");
                    Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                    //// Log.LogMsg("Testy 32");
                    Socket.ExclusiveAddressUse = false;
                    //// Log.LogMsg("Testy 33");
                    if (family == AddressFamily.InterNetworkV6)
                    {
                        //// Log.LogMsg("Testy 34");
                        Socket.SetSocketOption(SocketOptionLevel.IPv6, (SocketOptionName)27, 0); // set IPV6Only to false.  enables dual mode socket - V4+v6
                        //// Log.LogMsg("Testy 35");
                        Socket.Bind(new IPEndPoint(IPAddress.IPv6Any, port));
                        //// Log.LogMsg("Testy 36");
                    }
                    else
                    {
                        //// Log.LogMsg("Testy 37");
                        Socket.Bind(new IPEndPoint(IPAddress.Any, port));
                    }
                }
                //// Log.LogMsg("Testy 38");
                Port = ((IPEndPoint)Socket.LocalEndPoint).Port;

                m_ListenArgs = new List <SocketAsyncEventArgs>(maxSimultaneousListens);
                for (int i = 0; i < maxSimultaneousListens; i++)
                {
                    //// Log.LogMsg("Testy 39");
                    SocketAsyncEventArgs arg = SocketAsyncEventArgsCache.PopReadEventArg(new EventHandler <SocketAsyncEventArgs>(ReadCompleted), Socket);
                    if (family == AddressFamily.InterNetworkV6)
                    {
                        arg.RemoteEndPoint = new IPEndPoint(IPAddress.IPv6Any, 0);
                    }
                    else
                    {
                        arg.RemoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
                    }

                    //// Log.LogMsg("Testy 40");
                    m_ListenArgs.Add(arg);
                    //// Log.LogMsg("Testy 41");
                    bool willRaiseEvent = Socket.ReceiveFromAsync(arg);
                    //// Log.LogMsg("Testy 999");
                    if (!willRaiseEvent)
                    {
                        //// Log.LogMsg("Testy 42");
                        OnReceiveResolved(arg);
                    }
                }
            }
            catch (Exception e)
            {
                Log.LogMsg("UDPListener - error start listen: " + e.Message);
                return(false);
            }
            Listening = true;
            Log.LogMsg("UDPListener - Listening for UDP traffic on port " + port.ToString() + " with " + maxSimultaneousListens.ToString() + " max listeners.");
            return(true);
        }