コード例 #1
0
ファイル: TcpClient.cs プロジェクト: ChaotixBluix/framework
        private void ProcessReceivedPacket(ByteArray packetBuffer, int receivedPacketSize)
        {
            // Packet size is greater than 0, so a packet was received
            if (inPacket) {
                // We are still receiving data from a large packet
                int newPacketSize = this.receivedPacketSize + receivedPacketSize;
                if (newPacketSize >= totalPacketSize) {
                    if (packetType == MessageType.Generic) {
                        // Append the remaining data
                        byteBuffer.AppendTo(packetBuffer);
                        this.receivedPacketSize += receivedPacketSize;
                        // Transfer the data to a new byte array
                        ByteArray newBuffer = new ByteArray(byteBuffer.ToArray());

                        inPacket = false;
                        totalPacketSize = 0;
                        this.receivedPacketSize = 0;
                        byteBuffer = null;

                        ProcessReceivedPacket(newBuffer, newPacketSize);
                    } else if (packetType == MessageType.FileTransfer) {
                        ByteArray fileBytes = packetBuffer.SubArray(0, totalPacketSize - this.receivedPacketSize);
                        if (activeTransferFileStream != null) {
                            activeTransferFileStream.Write(fileBytes.ToArray(), 0, fileBytes.Length());

                            activeTransferFileStream.Close();
                            activeTransferFileStream.Dispose();
                            activeTransferFileStream = null;
                        }

                        inPacket = false;
                        totalPacketSize = 0;
                        this.receivedPacketSize = 0;
                        byteBuffer = null;

                        ByteArray newBuffer = packetBuffer.SubArray(fileBytes.Length(), packetBuffer.Length());
                        ProcessReceivedPacket(newBuffer, newBuffer.Length());
                    }
                } else {
                    if (packetType == MessageType.Generic) {
                        byteBuffer.AppendTo(packetBuffer);
                    } else if (packetType == MessageType.FileTransfer) {
                        if (activeTransferFileStream != null) {
                            activeTransferFileStream.Write(packetBuffer.ToArray(), 0, packetBuffer.Length());
                        }
                    }
                    this.receivedPacketSize += receivedPacketSize;
                }
            } else {
                if (IsValidPacket(packetBuffer)) {
                    if (buildingPacketData) {
                        byteBuffer.AppendTo(packetBuffer);
                        packetBuffer = new ByteArray(byteBuffer.ToArray());
                        buildingPacketData = false;
                        byteBuffer = null;
                    }
                    totalPacketSize = packetBuffer.SubArray(1, 5).ToInt() + GetHeaderSize();
                    packetType = (MessageType)packetBuffer[5];
                    byte[] packetCustomHeader = new byte[customHeaderSize];
                    for (int i = 0; i < customHeaderSize; i++) {
                        packetCustomHeader[i] = packetBuffer[6 + i];
                    }

                    if (receivedPacketSize == totalPacketSize) {
                        // We received the entire packet and nothing else
                        ByteArray fullPacket = packetBuffer.SubArray(GetHeaderSize(), totalPacketSize);

                        if (packetType == MessageType.Generic) {
                            if (DataReceived != null)
                                DataReceived(this, new DataReceivedEventArgs(fullPacket.ToArray(), packetCustomHeader, fullPacket.ToString()));
                        } else if (packetType == MessageType.FileTransfer) {
                            int fileNameSize = fullPacket.SubArray(0, 4).ToInt();
                            string fileName = fullPacket.SubArray(4, 4 + fileNameSize).ToString();

                            FileTransferInitiationEventArgs e = new FileTransferInitiationEventArgs(fileName);
                            if (FileTransferInitiation != null)
                                FileTransferInitiation(this, e);

                            if (e.Accept) {
                                byte[] fileBytes = fullPacket.SubArray(4 + fileNameSize, fullPacket.Length()).ToArray();

                                using (FileStream fileStream = new FileStream(e.DestinationDirectory + e.FileName, FileMode.OpenOrCreate, FileAccess.Write)) {
                                    fileStream.Write(fileBytes, 0, fileBytes.Length);
                                }
                            }
                        }
                        inPacket = false;
                        totalPacketSize = 0;
                        this.receivedPacketSize = 0;
                        byteBuffer = null;
                    } else if (receivedPacketSize > totalPacketSize) {
                        // We received the entire packet and the start of another packet
                        ByteArray fullPacket = packetBuffer.SubArray(GetHeaderSize(), totalPacketSize);
                        ByteArray leftoverData = packetBuffer.SubArray(fullPacket.Length() + GetHeaderSize(), packetBuffer.Length());

                        if (packetType == MessageType.Generic) {
                            if (DataReceived != null)
                                DataReceived(this, new DataReceivedEventArgs(fullPacket.ToArray(), packetCustomHeader, fullPacket.ToString()));
                        } else if (packetType == MessageType.FileTransfer) {
                            int fileNameSize = fullPacket.SubArray(0, 4).ToInt();
                            string fileName = fullPacket.SubArray(4, 4 + fileNameSize).ToString();

                            FileTransferInitiationEventArgs e = new FileTransferInitiationEventArgs(fileName);
                            if (FileTransferInitiation != null)
                                FileTransferInitiation(this, e);

                            if (e.Accept) {
                                byte[] fileBytes = fullPacket.SubArray(4 + fileNameSize, fullPacket.Length()).ToArray();

                                using (FileStream fileStream = new FileStream(e.DestinationDirectory + e.FileName, FileMode.OpenOrCreate, FileAccess.Write)) {
                                    fileStream.Write(fileBytes, 0, fileBytes.Length);
                                }
                            }
                        }

                        inPacket = false;
                        totalPacketSize = 0;
                        this.receivedPacketSize = 0;
                        byteBuffer = null;

                        ProcessReceivedPacket(leftoverData, leftoverData.Length());
                    } else if (receivedPacketSize < totalPacketSize) {
                        // We didn't receive the entire packet.
                        if (packetType == MessageType.Generic) {
                            // If it's a generic packet, store it
                            byteBuffer = packetBuffer;
                        } else if (packetType == MessageType.FileTransfer) {
                            // If it's a file transfer, read the header and start writing
                            int fileNameSize = packetBuffer.SubArray(6, 10).ToInt();
                            activeTransferFileName = packetBuffer.SubArray(10, 10 + fileNameSize).ToString();

                            FileTransferInitiationEventArgs e = new FileTransferInitiationEventArgs(activeTransferFileName);
                            if (FileTransferInitiation != null)
                                FileTransferInitiation(this, e);

                            if (e.Accept) {
                                byte[] fileBytes = packetBuffer.SubArray(10 + fileNameSize, packetBuffer.Length()).ToArray();

                                activeTransferFileStream = new FileStream(e.DestinationDirectory + e.FileName, FileMode.OpenOrCreate, FileAccess.Write);
                                activeTransferFileStream.Write(fileBytes, 0, fileBytes.Length);
                            } else {
                                activeTransferFileStream = null;
                            }
                        }
                        inPacket = true;
                        this.receivedPacketSize = receivedPacketSize;
                    }
                } else {
                    buildingPacketData = true;
                    byteBuffer = packetBuffer;
                    //ByteArray newBuffer = RepairPacketBuffer(packetBuffer);
                    //if (newBuffer != null) {
                    //    ProcessReceivedPacket(newBuffer, newBuffer.Length());
                    //}
                }
            }
        }
コード例 #2
0
        private void ProcessReceivedPacket(ByteArray packetBuffer, int receivedPacketSize)
        {
            // Packet size is greater than 0, so a packet was received
            if (inPacket)
            {
                // We are still receiving data from a large packet
                int newPacketSize = this.receivedPacketSize + receivedPacketSize;
                if (newPacketSize >= totalPacketSize)
                {
                    if (packetType == MessageType.Generic)
                    {
                        // Append the remaining data
                        byteBuffer.AppendTo(packetBuffer);
                        this.receivedPacketSize += receivedPacketSize;
                        // Transfer the data to a new byte array
                        ByteArray newBuffer = new ByteArray(byteBuffer.ToArray());

                        inPacket                = false;
                        totalPacketSize         = 0;
                        this.receivedPacketSize = 0;
                        byteBuffer              = null;

                        ProcessReceivedPacket(newBuffer, newPacketSize);
                    }
                    else if (packetType == MessageType.FileTransfer)
                    {
                        ByteArray fileBytes = packetBuffer.SubArray(0, totalPacketSize - this.receivedPacketSize);
                        if (activeTransferFileStream != null)
                        {
                            activeTransferFileStream.Write(fileBytes.ToArray(), 0, fileBytes.Length());

                            activeTransferFileStream.Close();
                            activeTransferFileStream.Dispose();
                            activeTransferFileStream = null;
                        }

                        inPacket                = false;
                        totalPacketSize         = 0;
                        this.receivedPacketSize = 0;
                        byteBuffer              = null;

                        ByteArray newBuffer = packetBuffer.SubArray(fileBytes.Length(), packetBuffer.Length());
                        ProcessReceivedPacket(newBuffer, newBuffer.Length());
                    }
                }
                else
                {
                    if (packetType == MessageType.Generic)
                    {
                        byteBuffer.AppendTo(packetBuffer);
                    }
                    else if (packetType == MessageType.FileTransfer)
                    {
                        if (activeTransferFileStream != null)
                        {
                            activeTransferFileStream.Write(packetBuffer.ToArray(), 0, packetBuffer.Length());
                        }
                    }
                    this.receivedPacketSize += receivedPacketSize;
                }
            }
            else
            {
                if (IsValidPacket(packetBuffer))
                {
                    if (buildingPacketData)
                    {
                        byteBuffer.AppendTo(packetBuffer);
                        packetBuffer       = new ByteArray(byteBuffer.ToArray());
                        buildingPacketData = false;
                        byteBuffer         = null;
                    }
                    totalPacketSize = packetBuffer.SubArray(1, 5).ToInt() + GetHeaderSize();
                    packetType      = (MessageType)packetBuffer[5];
                    byte[] packetCustomHeader = new byte[customHeaderSize];
                    for (int i = 0; i < customHeaderSize; i++)
                    {
                        packetCustomHeader[i] = packetBuffer[6 + i];
                    }

                    if (receivedPacketSize == totalPacketSize)
                    {
                        // We received the entire packet and nothing else
                        ByteArray fullPacket = packetBuffer.SubArray(GetHeaderSize(), totalPacketSize);

                        if (packetType == MessageType.Generic)
                        {
                            if (DataReceived != null)
                            {
                                DataReceived(this, new DataReceivedEventArgs(fullPacket.ToArray(), packetCustomHeader, fullPacket.ToString()));
                            }
                        }
                        else if (packetType == MessageType.FileTransfer)
                        {
                            int    fileNameSize = fullPacket.SubArray(0, 4).ToInt();
                            string fileName     = fullPacket.SubArray(4, 4 + fileNameSize).ToString();

                            FileTransferInitiationEventArgs e = new FileTransferInitiationEventArgs(fileName);
                            if (FileTransferInitiation != null)
                            {
                                FileTransferInitiation(this, e);
                            }

                            if (e.Accept)
                            {
                                byte[] fileBytes = fullPacket.SubArray(4 + fileNameSize, fullPacket.Length()).ToArray();

                                using (FileStream fileStream = new FileStream(e.DestinationDirectory + e.FileName, FileMode.OpenOrCreate, FileAccess.Write)) {
                                    fileStream.Write(fileBytes, 0, fileBytes.Length);
                                }
                            }
                        }
                        inPacket                = false;
                        totalPacketSize         = 0;
                        this.receivedPacketSize = 0;
                        byteBuffer              = null;
                    }
                    else if (receivedPacketSize > totalPacketSize)
                    {
                        // We received the entire packet and the start of another packet
                        ByteArray fullPacket   = packetBuffer.SubArray(GetHeaderSize(), totalPacketSize);
                        ByteArray leftoverData = packetBuffer.SubArray(fullPacket.Length() + GetHeaderSize(), packetBuffer.Length());

                        if (packetType == MessageType.Generic)
                        {
                            if (DataReceived != null)
                            {
                                DataReceived(this, new DataReceivedEventArgs(fullPacket.ToArray(), packetCustomHeader, fullPacket.ToString()));
                            }
                        }
                        else if (packetType == MessageType.FileTransfer)
                        {
                            int    fileNameSize = fullPacket.SubArray(0, 4).ToInt();
                            string fileName     = fullPacket.SubArray(4, 4 + fileNameSize).ToString();

                            FileTransferInitiationEventArgs e = new FileTransferInitiationEventArgs(fileName);
                            if (FileTransferInitiation != null)
                            {
                                FileTransferInitiation(this, e);
                            }

                            if (e.Accept)
                            {
                                byte[] fileBytes = fullPacket.SubArray(4 + fileNameSize, fullPacket.Length()).ToArray();

                                using (FileStream fileStream = new FileStream(e.DestinationDirectory + e.FileName, FileMode.OpenOrCreate, FileAccess.Write)) {
                                    fileStream.Write(fileBytes, 0, fileBytes.Length);
                                }
                            }
                        }

                        inPacket                = false;
                        totalPacketSize         = 0;
                        this.receivedPacketSize = 0;
                        byteBuffer              = null;

                        ProcessReceivedPacket(leftoverData, leftoverData.Length());
                    }
                    else if (receivedPacketSize < totalPacketSize)
                    {
                        // We didn't receive the entire packet.
                        if (packetType == MessageType.Generic)
                        {
                            // If it's a generic packet, store it
                            byteBuffer = packetBuffer;
                        }
                        else if (packetType == MessageType.FileTransfer)
                        {
                            // If it's a file transfer, read the header and start writing
                            int fileNameSize = packetBuffer.SubArray(6, 10).ToInt();
                            activeTransferFileName = packetBuffer.SubArray(10, 10 + fileNameSize).ToString();

                            FileTransferInitiationEventArgs e = new FileTransferInitiationEventArgs(activeTransferFileName);
                            if (FileTransferInitiation != null)
                            {
                                FileTransferInitiation(this, e);
                            }

                            if (e.Accept)
                            {
                                byte[] fileBytes = packetBuffer.SubArray(10 + fileNameSize, packetBuffer.Length()).ToArray();

                                activeTransferFileStream = new FileStream(e.DestinationDirectory + e.FileName, FileMode.OpenOrCreate, FileAccess.Write);
                                activeTransferFileStream.Write(fileBytes, 0, fileBytes.Length);
                            }
                            else
                            {
                                activeTransferFileStream = null;
                            }
                        }
                        inPacket = true;
                        this.receivedPacketSize = receivedPacketSize;
                    }
                }
                else
                {
                    buildingPacketData = true;
                    byteBuffer         = packetBuffer;
                    //ByteArray newBuffer = RepairPacketBuffer(packetBuffer);
                    //if (newBuffer != null) {
                    //    ProcessReceivedPacket(newBuffer, newBuffer.Length());
                    //}
                }
            }
        }