Ejemplo n.º 1
0
 private void SendPacketEnvelope(SocketState connection, PacketEnvelope envelope, string encryptionKey, string salt)
 {
     byte[] sendBuffer = Packetizer.AssembleMessagePacket(envelope, connection.UseCompression, true, encryptionKey, salt);
     Stats.BytesSent += (UInt64)sendBuffer.Length;
     connection.Socket.Send(sendBuffer, sendBuffer.Length, SocketFlags.None);
 }
Ejemplo n.º 2
0
 private void SendPacketEnvelope(SocketState connection, PacketEnvelope envelope)
 {
     byte[] sendBuffer = Packetizer.AssembleMessagePacket(envelope, connection.UseCompression, false, null, null);
     Stats.BytesSent += (UInt64)sendBuffer.Length;
     connection.Socket.Send(sendBuffer, sendBuffer.Length, SocketFlags.None);
 }
Ejemplo n.º 3
0
        void ProcessReceivedData(SocketState connection, byte[] buffer, int bufferSize)
        {
            string sharedSecretString = connection.Peer.IsEncryptionNegotationComplete ? connection.Peer.KeyNegotiator.SharedSecretString : null;

            string commonSalt = null;

            if (connection.Peer.IsIncomming && connection.Peer.IsEncryptionNegotationComplete)
            {
                commonSalt = _route.BindingPreSharedKey;
            }
            else if (connection.Peer.IsOutgoing && connection.Peer.IsEncryptionNegotationComplete)
            {
                commonSalt = _route.EndpointPreSharedKey;
            }

            if (_route.TrafficType == TrafficType.Http)
            {
                HttpHeaderType httpHeaderType = HttpHeaderType.None;

                string httpHeader      = null;
                string httpRequestVerb = string.Empty;

                if (connection.HttpHeaderBuilder != string.Empty)
                {
                    //This is a continuation of a previously received fragmented header.
                    httpHeader     = (connection.HttpHeaderBuilder ?? string.Empty) + Encoding.UTF8.GetString(buffer);
                    httpHeaderType = HttpUtility.IsHttpHeader(httpHeader, out httpRequestVerb);
                }
                else
                {
                    httpHeaderType = HttpUtility.IsHttpHeader(buffer, bufferSize, out httpRequestVerb);

                    if (httpHeaderType != HttpHeaderType.None)
                    {
                        httpHeader = Encoding.UTF8.GetString(buffer);
                    }
                }

                if (httpHeaderType != HttpHeaderType.None)
                {
                    string lineBreak      = "";
                    int    endOfHeaderPos = HttpUtility.GetHttpHeaderEnd(httpHeader, out lineBreak);

                    if (endOfHeaderPos < 0)
                    {
                        throw new NotSupportedException(); //Not sure what to do yet.
                    }

                    if (endOfHeaderPos > bufferSize)
                    {
                        throw new InvalidOperationException(); //Not sure what to do yet.
                    }

                    httpHeader  = httpHeader.Substring(0, endOfHeaderPos).Trim(new char[] { '\r', '\n', ' ', '\0' });
                    httpHeader  = ApplyHttpHeaderRules(httpHeader, httpHeaderType, httpRequestVerb, lineBreak);
                    httpHeader += lineBreak + lineBreak; //Terminate the header.

                    int contentLength = bufferSize - (endOfHeaderPos - connection.HttpHeaderBuilder.Length);
                    connection.HttpHeaderBuilder = string.Empty;

                    byte[] fullReponse = new byte[httpHeader.Length + contentLength];

                    Buffer.BlockCopy(System.Text.Encoding.UTF8.GetBytes(httpHeader), 0, fullReponse, 0, httpHeader.Length);
                    if (contentLength > 0)
                    {
                        Buffer.BlockCopy(buffer, endOfHeaderPos, fullReponse, httpHeader.Length, contentLength);
                    }

                    if (connection.Peer.UsePackets)
                    {
                        //Console.WriteLine("--Send:{0}, Packet: {1}", route.Name, Encoding.UTF8.GetString(fullReponse.Take(fullReponse.Length).ToArray()));

                        byte[] sendBuffer = Packetizer.AssembleMessagePacket(fullReponse, fullReponse.Length, connection.Peer.UseCompression, connection.Peer.UseEncryption, sharedSecretString, commonSalt);
                        Stats.BytesSent += (UInt64)sendBuffer.Length;
                        connection.Peer.Socket.Send(sendBuffer, sendBuffer.Length, SocketFlags.None);
                        WaitForData(connection);
                    }
                    else
                    {
                        //Console.WriteLine("--Send:{0}, Raw: {1}", route.Name, Encoding.UTF8.GetString(fullReponse.Take(fullReponse.Length).ToArray()));

                        Stats.BytesSent += (UInt64)fullReponse.Length;
                        connection.Peer.Socket.Send(fullReponse, fullReponse.Length, SocketFlags.None);
                    }

                    return;
                }
            }

            if (connection.Peer.UsePackets)
            {
                //Console.WriteLine("--Send:{0}, Packet: {1}", route.Name, Encoding.UTF8.GetString(buffer.Take(bufferSize).ToArray()));

                byte[] sendBuffer = Packetizer.AssembleMessagePacket(buffer, bufferSize, connection.Peer.UseCompression, connection.Peer.UseEncryption, sharedSecretString, commonSalt);
                Stats.BytesSent += (UInt64)sendBuffer.Length;
                connection.Peer.Socket.Send(sendBuffer, sendBuffer.Length, SocketFlags.None);
            }
            else
            {
                //Console.WriteLine("--Send:{0}, Raw: {1}", route.Name, Encoding.UTF8.GetString(buffer.Take(bufferSize).ToArray()));

                Stats.BytesSent += (UInt64)bufferSize;
                connection.Peer.Socket.Send(buffer, bufferSize, SocketFlags.None);
            }
        }