Beispiel #1
0
        /// <summary>
        /// Creates a serialized request packet (DtmPacket)
        /// </summary>
        private MemoryStream CreateRequest(DtmPacketFlags Message, short State)
        {
            MemoryStream ret = new DtmPacketStruct(Message, 0, 0, State).ToStream();

            ret.Seek(0, SeekOrigin.Begin);
            return(ret);
        }
Beispiel #2
0
        private void Transmit(DtmPacketFlags PacketType, short PacketFlag, long OptionFlag = 0, MemoryStream Payload = null)
        {
            lock (_sndLock)
            {
                long pldLen = Payload == null ? 0 : Payload.Length;
                // create a new packet: packet flag, payload size, sequence, and state flag
                MemoryStream pktStm = new DtmPacketStruct(PacketType, pldLen, _sndSequence, PacketFlag, OptionFlag).ToStream();

                // add payload
                if (Payload != null)
                {
                    // copy to output
                    pktStm.Seek(0, SeekOrigin.End);
                    Payload.WriteTo(pktStm);
                    pktStm.Seek(0, SeekOrigin.Begin);
                }

                // store in the file packet buffer
                _sndBuffer.Push(_sndSequence, pktStm);
                // increment file send counter
                _sndSequence++;

                // transmit to remote client
                if (_clientSocket.IsConnected)
                {
                    try
                    {
                        _clientSocket.SendAsync(pktStm);
                    }
                    catch (CryptoSocketException ce)
                    {
                        SocketException se = ce.InnerException as SocketException;

                        if (se.SocketErrorCode == SocketError.WouldBlock ||
                            se.SocketErrorCode == SocketError.IOPending ||
                            se.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
                        {
                            // buffer is full, throttle down
                            Throttle(pktStm);
                        }
                        else
                        {
                            // possible connection dropped, alert app
                            if (SessionError != null)
                            {
                                DtmErrorArgs args = new DtmErrorArgs(ce, DtmErrorSeverityFlags.Connection);
                                SessionError(this, args);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        // possible connection dropped, alert app
                        if (SessionError != null)
                        {
                            DtmErrorArgs args = new DtmErrorArgs(ex, DtmErrorSeverityFlags.Connection);
                            SessionError(this, args);
                        }
                    }

                    // notify app
                    if (PacketSent != null)
                    {
                        PacketSent(this, new DtmPacketArgs((short)DtmTransferFlags.DataChunk, pldLen));
                    }
                }
            }
        }