Пример #1
0
        public virtual void SendPacketTo(byte[] buffer, int size, SocketFlags flags, uint circuitcode)
        //EndPoint packetSender)
        {
            // find the endpoint for this circuit
            EndPoint sendto;

            try
            {
                sendto = (EndPoint)clientCircuits_reverse[circuitcode];
            }
            catch
            {
                // Exceptions here mean there is no circuit
                m_log.Warn("[CLIENT]: Circuit not found, not sending packet");
                return;
            }

            if (sendto != null)
            {
                //we found the endpoint so send the packet to it
                if (proxyPortOffset != 0)
                {
                    //MainLog.Instance.Verbose("UDPSERVER", "SendPacketTo proxy " + proxyCircuits[circuitcode].ToString() + ": client " + sendto.ToString());
                    ProxyCodec.EncodeProxyMessage(buffer, ref size, sendto);
                    m_socket.SendTo(buffer, size, flags, proxyCircuits[circuitcode]);
                }
                else
                {
                    //MainLog.Instance.Verbose("UDPSERVER", "SendPacketTo : client " + sendto.ToString());
                    try
                    {
                        m_socket.SendTo(buffer, size, flags, sendto);
                    }
                    catch (SocketException SockE)
                    {
                        m_log.ErrorFormat("[UDPSERVER]: Caught Socket Error in the send buffer!. {0}", SockE.ToString());
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// This method is called every time that we receive new UDP data.
        /// </summary>
        /// <param name="result"></param>
        protected virtual void OnReceivedData(IAsyncResult result)
        {
            Packet   packet   = null;
            int      numBytes = 1;
            EndPoint epSender = new IPEndPoint(IPAddress.Any, 0);
            EndPoint epProxy  = null;

            try
            {
                if (EndReceive(out numBytes, result, ref epSender))
                {
                    // Make sure we are getting zeroes when running off the
                    // end of grab / degrab packets from old clients
                    Array.Clear(RecvBuffer, numBytes, RecvBuffer.Length - numBytes);

                    int packetEnd = numBytes - 1;
                    if (proxyPortOffset != 0)
                    {
                        packetEnd -= 6;
                    }

                    try
                    {
                        packet = PacketPool.Instance.GetPacket(RecvBuffer, ref packetEnd, ZeroBuffer);
                    }
                    catch (MalformedDataException e)
                    {
                        m_log.DebugFormat("[CLIENT]: Dropped Malformed Packet due to MalformedDataException: {0}", e.StackTrace);
                    }
                    catch (IndexOutOfRangeException e)
                    {
                        m_log.DebugFormat("[CLIENT]: Dropped Malformed Packet due to IndexOutOfRangeException: {0}", e.StackTrace);
                    }
                    catch (Exception e)
                    {
                        m_log.Debug("[CLIENT]: " + e);
                    }
                }


                if (proxyPortOffset != 0)
                {
                    // If we've received a use circuit packet, then we need to decode an endpoint proxy, if one exists,
                    // before allowing the RecvBuffer to be overwritten by the next packet.
                    if (packet != null && packet.Type == PacketType.UseCircuitCode)
                    {
                        epProxy = epSender;
                    }

                    // Now decode the message from the proxy server
                    epSender = ProxyCodec.DecodeProxyMessage(RecvBuffer, ref numBytes);
                }
            }
            catch (Exception ex)
            {
                m_log.ErrorFormat("[CLIENT]: Exception thrown during EndReceive(): {0}", ex);
            }

            BeginRobustReceive();

            if (packet != null)
            {
                if (packet.Type == PacketType.UseCircuitCode)
                {
                    AddNewClient((UseCircuitCodePacket)packet, epSender, epProxy);
                }
                else
                {
                    ProcessInPacket(packet, epSender);
                }
            }
        }