Esempio n. 1
0
        private void TurnServer_PeerDataReceived(ref ServerAsyncEventArgs e)
        {
            //lock (syncRoot)
            {
                try
                {
                    Allocation allocation = allocations.GetByPeer(e.LocalEndPoint, e.RemoteEndPoint);

                    if (allocation != null)
                    {
                        allocation.TouchLifetime();

                        if (allocation.IsActiveDestinationEnabled)
                        {
                            e.LocalEndPoint  = allocation.Alocated;
                            e.RemoteEndPoint = allocation.ActiveDestination;
                            e.Count          = e.BytesTransferred;
                            e.ConnectionId   = ServerAsyncEventArgs.AnyNewConnectionId;

                            peerServer.SendAsync(e);

                            e = null;
                        }
                    }
                }
                catch (Exception ex)
                {
                    logger.WriteWarning(ex.ToString());
                }
            }
        }
Esempio n. 2
0
 public void SendAsyncSip(ServerAsyncEventArgs e)
 {
     if (AjaxWebsocket.IsAjax(e))
     {
         ajax.ProcessOutgoing(e);
     }
     else
     {
         e.UserTokenForSending2 = SipModule;
         serversManager.SendAsync(e);
     }
 }
Esempio n. 3
0
        static bool ServersManager_Received(ServersManager<BaseConnection2> server, BaseConnection c, ref ServerAsyncEventArgs e)
        {
            e.Count = e.BytesTransferred;
            server.SendAsync(e);
            e = null;

            return true;
        }
        private bool TurnServer_Received(ServersManager <TurnConnection> s, TurnConnection c, ref ServerAsyncEventArgs e)
        {
            if (e.LocalEndPoint.Protocol == ServerProtocol.Udp)
            {
                if (TurnMessage.IsTurnMessage(e.Buffer, e.Offset, e.BytesTransferred))
                {
                    TurnServer_TurnDataReceived(ref e);
                }
                else
                {
                    TurnServer_PeerDataReceived(ref e);
                }
            }
            else if (e.LocalEndPoint.Protocol == ServerProtocol.Tcp)
            {
                if (c.Buffer.IsValid)
                {
                    c.Buffer.Resize(Math.Max(4096, c.BytesExpected));

                    if (c.Buffer.CopyTransferredFrom(e, 0) == false)
                    {
                        return(false);
                    }

                    if (c.Buffer.Count < c.BytesExpected)
                    {
                        return(true);
                    }
                }
                else
                {
                    if (e.BytesTransferred < c.BytesExpected)
                    {
                        return(c.Buffer.CopyTransferredFrom(e, 0));
                    }
                }

                int proccessed = 0;

                for (; ;)
                {
                    if (c.Buffer.IsValid)
                    {
                        if (e == null)
                        {
                            e = EventArgsManager.Get();
                            e.CopyAddressesFrom(c);
                        }

                        e.AttachBuffer(c.Buffer);
                        proccessed = 0;
                    }

                    if (e.BytesTransferred - proccessed < c.BytesExpected)
                    {
                        return(c.Buffer.CopyTransferredFrom(e, proccessed));
                    }

                    switch (c.Phase)
                    {
                    case TcpPhase.WaitingFirstXpacket:

                        if (pseudoTlsMessage.IsBeginOfClientHello(e.Buffer, e.Offset, FirstXpacketLength))
                        {
                            c.Phase         = TcpPhase.WaitingClientHello;
                            c.BytesExpected = PseudoTlsMessage.ClientHelloLength;
                        }
                        else
                        {
                            c.Phase         = TcpPhase.WaitingTcpFrame;
                            c.BytesExpected = TcpFramingHeader.TcpFramingHeaderLength;

                            if (FirstXpacketLength <= TcpFramingHeader.TcpFramingHeaderLength)
                            {
                                goto case TcpPhase.WaitingTcpFrame;
                            }
                        }
                        break;


                    case TcpPhase.WaitingClientHello:

                        if (pseudoTlsMessage.IsClientHello(e.Buffer, e.Offset) == false)
                        {
                            return(false);
                        }

                        var x = EventArgsManager.Get();

                        x.CopyAddressesFrom(e);
                        x.Count = PseudoTlsMessage.ServerHelloHelloDoneLength;

                        pseudoTlsMessage.GetServerHelloHelloDoneBytes(x.Buffer, x.Offset);

                        s.SendAsync(x);

                        proccessed     += c.BytesExpected;
                        c.Phase         = TcpPhase.WaitingTcpFrame;
                        c.BytesExpected = TcpFramingHeader.TcpFramingHeaderLength;

                        break;


                    case TcpPhase.WaitingTcpFrame:

                        TcpFramingHeader tcpHeader;
                        if (TcpFramingHeader.TryParse(e.Buffer, e.Offset, out tcpHeader) == false)
                        {
                            return(false);
                        }

                        proccessed += c.BytesExpected;
                        c.Phase     = (tcpHeader.Type == TcpFrameType.ControlMessage) ?
                                      TcpPhase.WaitingTurnControlMessage : TcpPhase.WaitingTurnEndToEndData;
                        c.BytesExpected = tcpHeader.Length;

                        break;



                    case TcpPhase.WaitingTurnEndToEndData:
                    case TcpPhase.WaitingTurnControlMessage:

                        if (e.BytesTransferred - proccessed < c.BytesExpected)
                        {
                            if (c.Buffer.CopyTransferredFrom(e, proccessed + c.BytesExpected) == false)
                            {
                                return(false);
                            }
                        }

                        e.Count           -= proccessed;
                        e.Offset          += proccessed;
                        e.BytesTransferred = c.BytesExpected;

                        if (c.Phase == TcpPhase.WaitingTurnEndToEndData)
                        {
                            TurnServer_PeerDataReceived(ref e);
                        }
                        else
                        {
                            TurnServer_TurnDataReceived(ref e);
                        }

                        proccessed = e.BytesTransferred;

                        c.Phase         = TcpPhase.WaitingTcpFrame;
                        c.BytesExpected = TcpFramingHeader.TcpFramingHeaderLength;

                        break;


                    default:
                        throw new NotImplementedException();
                    }
                }
            }
            else
            {
                throw new NotImplementedException();
            }

            return(true);
        }
Esempio n. 5
0
        private bool PeerServer_Received(ServersManager <PeerConnection> s, BaseConnection с, ref ServerAsyncEventArgs e)
        {
            //lock (syncRoot)
            {
                try
                {
                    Allocation allocation = allocations.Get(e.LocalEndPoint);

                    if (allocation != null)
                    {
                        if (allocation.Permissions.IsPermited(e.RemoteEndPoint))
                        {
                            allocation.TouchLifetime();

                            if (allocation.ActiveDestination.IsEqual(e.RemoteEndPoint))
                            {
                                if (e.LocalEndPoint.Protocol == ServerProtocol.Tcp)
                                {
                                    TcpFramingHeader.GetBytes(e.Buffer, e.Offset, TcpFrameType.EndToEndData, e.BytesTransferred);

                                    e.Count        = e.OffsetOffset + e.BytesTransferred;
                                    e.OffsetOffset = 0;
                                }
                                else
                                {
                                    e.Count = e.BytesTransferred;
                                }

                                e.LocalEndPoint  = allocation.Local;
                                e.RemoteEndPoint = allocation.Reflexive;
                                e.ConnectionId   = ServerAsyncEventArgs.AnyConnectionId;

                                turnServer.SendAsync(e);

                                e = null;
                            }
                            else
                            {
                                TurnMessage message = new TurnMessage()
                                {
                                    IsAttributePaddingDisabled = true,
                                    MessageType   = MessageType.DataIndication,
                                    TransactionId = TransactionServer.GenerateTransactionId(),

                                    MagicCookie = new MagicCookie(),

                                    RemoteAddress = new RemoteAddress()
                                    {
                                        IpAddress = e.RemoteEndPoint.Address,
                                        Port      = (UInt16)e.RemoteEndPoint.Port,
                                    },

                                    Data = new Data()
                                    {
                                        ValueRef       = e.Buffer,
                                        ValueRefOffset = e.Offset,
                                        ValueRefLength = e.BytesTransferred,
                                    },
                                };

                                SendTurn(message, allocation.Local, allocation.Reflexive);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    logger.WriteWarning(ex.ToString());
                }
            }

            return(true);
        }
Esempio n. 6
0
        private bool TurnServer_Received(ServersManager<TurnConnection> s, TurnConnection c, ref ServerAsyncEventArgs e)
        {
            if (e.LocalEndPoint.Protocol == ServerProtocol.Udp)
            {
                if (TurnMessage.IsTurnMessage(e.Buffer, e.Offset, e.BytesTransferred))
                    TurnServer_TurnDataReceived(ref e);
                else
                    TurnServer_PeerDataReceived(ref e);
            }
            else if (e.LocalEndPoint.Protocol == ServerProtocol.Tcp)
            {
                if (c.Buffer.IsValid)
                {
                    c.Buffer.Resize(Math.Max(4096, c.BytesExpected));

                    if (c.Buffer.CopyTransferredFrom(e, 0) == false)
                        return false;

                    if (c.Buffer.Count < c.BytesExpected)
                        return true;
                }
                else
                {
                    if (e.BytesTransferred < c.BytesExpected)
                        return c.Buffer.CopyTransferredFrom(e, 0);
                }

                int proccessed = 0;

                for (; ; )
                {
                    if (c.Buffer.IsValid)
                    {
                        if (e == null)
                        {
                            e = EventArgsManager.Get();
                            e.CopyAddressesFrom(c);
                        }

                        e.AttachBuffer(c.Buffer);
                        proccessed = 0;
                    }

                    if (e.BytesTransferred - proccessed < c.BytesExpected)
                        return c.Buffer.CopyTransferredFrom(e, proccessed);

                    switch (c.Phase)
                    {
                        case TcpPhase.WaitingFirstXpacket:

                            if (pseudoTlsMessage.IsBeginOfClientHello(e.Buffer, e.Offset, FirstXpacketLength))
                            {
                                c.Phase = TcpPhase.WaitingClientHello;
                                c.BytesExpected = PseudoTlsMessage.ClientHelloLength;
                            }
                            else
                            {
                                c.Phase = TcpPhase.WaitingTcpFrame;
                                c.BytesExpected = TcpFramingHeader.TcpFramingHeaderLength;

                                if (FirstXpacketLength <= TcpFramingHeader.TcpFramingHeaderLength)
                                    goto case TcpPhase.WaitingTcpFrame;
                            }
                            break;

                        case TcpPhase.WaitingClientHello:

                            if (pseudoTlsMessage.IsClientHello(e.Buffer, e.Offset) == false)
                                return false;

                            var x = EventArgsManager.Get();

                            x.CopyAddressesFrom(e);
                            x.Count = PseudoTlsMessage.ServerHelloHelloDoneLength;

                            pseudoTlsMessage.GetServerHelloHelloDoneBytes(x.Buffer, x.Offset);

                            s.SendAsync(x);

                            proccessed += c.BytesExpected;
                            c.Phase = TcpPhase.WaitingTcpFrame;
                            c.BytesExpected = TcpFramingHeader.TcpFramingHeaderLength;

                            break;

                        case TcpPhase.WaitingTcpFrame:

                            TcpFramingHeader tcpHeader;
                            if (TcpFramingHeader.TryParse(e.Buffer, e.Offset, out tcpHeader) == false)
                                return false;

                            proccessed += c.BytesExpected;
                            c.Phase = (tcpHeader.Type == TcpFrameType.ControlMessage) ?
                                TcpPhase.WaitingTurnControlMessage : TcpPhase.WaitingTurnEndToEndData;
                            c.BytesExpected = tcpHeader.Length;

                            break;

                        case TcpPhase.WaitingTurnEndToEndData:
                        case TcpPhase.WaitingTurnControlMessage:

                            if (e.BytesTransferred - proccessed < c.BytesExpected)
                                if (c.Buffer.CopyTransferredFrom(e, proccessed + c.BytesExpected) == false)
                                    return false;

                            e.Count -= proccessed;
                            e.Offset += proccessed;
                            e.BytesTransferred = c.BytesExpected;

                            if (c.Phase == TcpPhase.WaitingTurnEndToEndData)
                                TurnServer_PeerDataReceived(ref e);
                            else
                                TurnServer_TurnDataReceived(ref e);

                            proccessed = e.BytesTransferred;

                            c.Phase = TcpPhase.WaitingTcpFrame;
                            c.BytesExpected = TcpFramingHeader.TcpFramingHeaderLength;

                            break;

                        default:
                            throw new NotImplementedException();
                    }
                }
            }
            else
                throw new NotImplementedException();

            return true;
        }