internal void SendObject(object o)
        {
            serializer.SendNext(o);

            while (serializer.HasQueuedObjects)
            {
                UdpSendFailReason reason = CheckCanSend(false);

                if (reason != UdpSendFailReason.None)
                {
                    while (serializer.HasQueuedObjects)
                    {
                        socket.Raise(UdpEvent.PUBLIC_OBJECT_SEND_FAILED, this, serializer.NextObject(), reason);
                    }

                    break;
                }

                UdpStream stream = socket.GetWriteStream(mtu << 3, UdpSocket.HeaderBitSize);
                object    obj    = serializer.NextObject();

                if (serializer.Pack(stream, ref obj))
                {
                    if (stream.Overflowing && (socket.Config.AllowPacketOverflow == false))
                    {
                        UdpLog.Error("stream to {0} is overflowing, not sending", endpoint.ToString());
                        socket.Raise(UdpEvent.PUBLIC_OBJECT_SEND_FAILED, this, obj, UdpSendFailReason.StreamOverflow);
                        return;
                    }

                    UdpHeader header = MakeHeader(true);
                    header.Pack(stream, socket);

                    UdpHandle handle = MakeHandle(ref header);
                    handle.Object = obj;

                    if (SendStream(stream, handle, alwaysSendMtu))
                    {
                        // track stats
                        stats.PacketSent((uint)stream.Ptr >> 3);
                        socket.Statistics.PacketSent((uint)stream.Ptr >> 3);

                        // push object to user thread
                        socket.Raise(UdpEvent.PUBLIC_OBJECT_SENT, this, obj);
                        Log.info(this, "SendObject##UdpSocket.HeaderBitSize: " + (UdpSocket.HeaderBitSize >> 3));
                        Log.info(this, "SendObject##size: " + (stream.Ptr >> 3));
                    }
                    else
                    {
                        socket.Raise(UdpEvent.PUBLIC_OBJECT_SEND_FAILED, this, obj, UdpSendFailReason.SocketError);
                    }
                }
                else
                {
                    socket.Raise(UdpEvent.PUBLIC_OBJECT_SEND_FAILED, this, obj, UdpSendFailReason.SerializerReturnedFalse);
                }
            }
        }
示例#2
0
        internal void ProcessConnectedTimeouts(uint now)
        {
            if ((recvTime + socket.Config.ConnectionTimeout) < now)
            {
                UdpLog.Debug("disconnecting due to timeout from {0}, last packet received: {1}, current time: {2}", endpoint.ToString(), recvTime.ToString(), now.ToString());
                ChangeState(UdpConnectionState.Disconnected);
            }

            if (CheckState(UdpConnectionState.Connected))
            {
                if (sendTime + socket.Config.PingTimeout < now || recvSinceLastSend >= socket.Config.RecvWithoutAckLimit)
                {
                    SendCommand(UdpCommandType.Ping);
                }
            }
        }
示例#3
0
        void RecvNetworkData()
        {
            if (platform.RecvPoll(1))
            {
                int         byteReceived = 0;
                UdpEndPoint ep           = UdpEndPoint.Any;

                if (platform.RecvFrom(receiveBuffer, receiveBuffer.Length, ref byteReceived, ref ep))
                {
#if DEBUG
                    if (random.NextDouble() < Config.SimulatedLoss)
                    {
                        UdpLog.Info("simulated loss of packet from {0}", ep.ToString());
                        return;
                    }
#endif
                    UdpConnection cn;

                    if (connLookup.TryGetValue(ep, out cn))
                    {
                        cn.OnPacket(new UdpBitStream(receiveBuffer, byteReceived));
                    }
                    else
                    {
                        RecvUnconnectedPacket(new UdpBitStream(receiveBuffer, byteReceived), ep);
                    }
                }
            }
        }
示例#4
0
        void RecvUnconnectedPacket(UdpBitStream buff, UdpEndPoint ep)
        {
            buff.Ptr = UdpHeader.GetSize(this);

            if (buff.ReadByte(8) == (byte)UdpCommandType.Connect)
            {
                if (Config.AllowIncommingConnections && ((connLookup.Count + pendingConnections.Count) < Config.ConnectionLimit || Config.ConnectionLimit == -1))
                {
                    if (Config.AutoAcceptIncommingConnections)
                    {
                        AcceptConnection(ep);
                    }
                    else
                    {
                        if (pendingConnections.Add(ep))
                        {
                            Raise(UdpEvent.PUBLIC_CONNECT_REQUEST, ep);
                        }
                    }
                }
                else
                {
                    SendRefusedCommand(ep);
                }
            }
            else
            {
                UdpLog.Debug("received invalid header byte in unconnected packet from {0}", ep.ToString());
            }
        }