Merge pull request #187 from orastarter/master

Make AGIReply Threadsafe
This commit is contained in:
Deantwo 2019-10-10 09:23:47 +02:00 committed by GitHub
commit c2725a09da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 71 deletions

View file

@ -12,8 +12,6 @@ namespace AsterNET.FastAGI
private readonly bool _SCHANGUP_CAUSES_EXCEPTION; private readonly bool _SCHANGUP_CAUSES_EXCEPTION;
private readonly AGIReader agiReader; private readonly AGIReader agiReader;
private readonly AGIWriter agiWriter; private readonly AGIWriter agiWriter;
private AGIReply agiReply;
public AGIChannel(SocketConnection socket, bool SC511_CAUSES_EXCEPTION, bool SCHANGUP_CAUSES_EXCEPTION) public AGIChannel(SocketConnection socket, bool SC511_CAUSES_EXCEPTION, bool SCHANGUP_CAUSES_EXCEPTION)
{ {
@ -34,18 +32,10 @@ namespace AsterNET.FastAGI
_SCHANGUP_CAUSES_EXCEPTION = SCHANGUP_CAUSES_EXCEPTION; _SCHANGUP_CAUSES_EXCEPTION = SCHANGUP_CAUSES_EXCEPTION;
} }
/// <summary>
/// Get last AGI Reply.
/// </summary>
public AGIReply LastReply
{
get { return agiReply; }
}
public AGIReply SendCommand(AGICommand command) public AGIReply SendCommand(AGICommand command)
{ {
agiWriter.SendCommand(command); agiWriter.SendCommand(command);
agiReply = agiReader.ReadReply(); AGIReply agiReply = agiReader.ReadReply();
int status = agiReply.GetStatus(); int status = agiReply.GetStatus();
if (status == (int) AGIReplyStatuses.SC_INVALID_OR_UNKNOWN_COMMAND) if (status == (int) AGIReplyStatuses.SC_INVALID_OR_UNKNOWN_COMMAND)
throw new InvalidOrUnknownCommandException(command.BuildCommand()); throw new InvalidOrUnknownCommandException(command.BuildCommand());

View file

@ -102,8 +102,8 @@ namespace AsterNET.FastAGI
protected internal int GetChannelStatus() protected internal int GetChannelStatus()
{ {
AGIChannel channel = this.Channel; AGIChannel channel = this.Channel;
channel.SendCommand(new Command.ChannelStatusCommand()); AGIReply lastReply = channel.SendCommand(new Command.ChannelStatusCommand());
return channel.LastReply.ResultCode; return lastReply.ResultCode;
} }
#endregion #endregion
@ -118,8 +118,8 @@ namespace AsterNET.FastAGI
protected internal string GetData(string file) protected internal string GetData(string file)
{ {
AGIChannel channel = this.Channel; AGIChannel channel = this.Channel;
channel.SendCommand(new Command.GetDataCommand(file)); AGIReply lastReply = channel.SendCommand(new Command.GetDataCommand(file));
return channel.LastReply.GetResult(); return lastReply.GetResult();
} }
#endregion #endregion
@ -137,8 +137,8 @@ namespace AsterNET.FastAGI
protected internal string GetData(string file, long timeout) protected internal string GetData(string file, long timeout)
{ {
AGIChannel channel = this.Channel; AGIChannel channel = this.Channel;
channel.SendCommand(new Command.GetDataCommand(file, timeout)); AGIReply lastReply = channel.SendCommand(new Command.GetDataCommand(file, timeout));
return channel.LastReply.GetResult(); return lastReply.GetResult();
} }
#endregion #endregion
@ -158,8 +158,8 @@ namespace AsterNET.FastAGI
protected internal string GetData(string file, long timeout, int maxDigits) protected internal string GetData(string file, long timeout, int maxDigits)
{ {
AGIChannel channel = this.Channel; AGIChannel channel = this.Channel;
channel.SendCommand(new Command.GetDataCommand(file, timeout, maxDigits)); AGIReply lastReply = channel.SendCommand(new Command.GetDataCommand(file, timeout, maxDigits));
return channel.LastReply.GetResult(); return lastReply.GetResult();
} }
#endregion #endregion
@ -177,8 +177,8 @@ namespace AsterNET.FastAGI
protected internal char GetOption(string file, string escapeDigits) protected internal char GetOption(string file, string escapeDigits)
{ {
AGIChannel channel = this.Channel; AGIChannel channel = this.Channel;
channel.SendCommand(new Command.GetOptionCommand(file, escapeDigits)); AGIReply lastReply = channel.SendCommand(new Command.GetOptionCommand(file, escapeDigits));
return channel.LastReply.ResultCodeAsChar; return lastReply.ResultCodeAsChar;
} }
#endregion #endregion
@ -196,8 +196,8 @@ namespace AsterNET.FastAGI
protected internal char GetOption(string file, string escapeDigits, int timeout) protected internal char GetOption(string file, string escapeDigits, int timeout)
{ {
AGIChannel channel = this.Channel; AGIChannel channel = this.Channel;
channel.SendCommand(new Command.GetOptionCommand(file, escapeDigits, timeout)); AGIReply lastReply = channel.SendCommand(new Command.GetOptionCommand(file, escapeDigits, timeout));
return channel.LastReply.ResultCodeAsChar; return lastReply.ResultCodeAsChar;
} }
#endregion #endregion
@ -210,8 +210,8 @@ namespace AsterNET.FastAGI
protected internal int Exec(string application) protected internal int Exec(string application)
{ {
AGIChannel channel = this.Channel; AGIChannel channel = this.Channel;
channel.SendCommand(new Command.ExecCommand(application)); AGIReply lastReply = channel.SendCommand(new Command.ExecCommand(application));
return channel.LastReply.ResultCode; return lastReply.ResultCode;
} }
#endregion #endregion
@ -225,8 +225,8 @@ namespace AsterNET.FastAGI
protected internal int Exec(string application, string options) protected internal int Exec(string application, string options)
{ {
AGIChannel channel = this.Channel; AGIChannel channel = this.Channel;
channel.SendCommand(new Command.ExecCommand(application, options)); AGIReply lastReply = channel.SendCommand(new Command.ExecCommand(application, options));
return channel.LastReply.ResultCode; return lastReply.ResultCode;
} }
#endregion #endregion
@ -291,8 +291,8 @@ namespace AsterNET.FastAGI
protected internal char StreamFile(string file, string escapeDigits) protected internal char StreamFile(string file, string escapeDigits)
{ {
AGIChannel channel = this.Channel; AGIChannel channel = this.Channel;
channel.SendCommand(new Command.StreamFileCommand(file, escapeDigits)); AGIReply lastReply = channel.SendCommand(new Command.StreamFileCommand(file, escapeDigits));
return channel.LastReply.ResultCodeAsChar; return lastReply.ResultCodeAsChar;
} }
#endregion #endregion
@ -318,8 +318,8 @@ namespace AsterNET.FastAGI
protected internal char SayDigits(string digits, string escapeDigits) protected internal char SayDigits(string digits, string escapeDigits)
{ {
AGIChannel channel = this.Channel; AGIChannel channel = this.Channel;
channel.SendCommand(new Command.SayDigitsCommand(digits, escapeDigits)); AGIReply lastReply = channel.SendCommand(new Command.SayDigitsCommand(digits, escapeDigits));
return channel.LastReply.ResultCodeAsChar; return lastReply.ResultCodeAsChar;
} }
#endregion #endregion
@ -345,8 +345,8 @@ namespace AsterNET.FastAGI
protected internal char SayNumber(string number, string escapeDigits) protected internal char SayNumber(string number, string escapeDigits)
{ {
AGIChannel channel = this.Channel; AGIChannel channel = this.Channel;
channel.SendCommand(new Command.SayNumberCommand(number, escapeDigits)); AGIReply lastReply = channel.SendCommand(new Command.SayNumberCommand(number, escapeDigits));
return channel.LastReply.ResultCodeAsChar; return lastReply.ResultCodeAsChar;
} }
#endregion #endregion
@ -372,8 +372,8 @@ namespace AsterNET.FastAGI
protected internal char SayPhonetic(string text, string escapeDigits) protected internal char SayPhonetic(string text, string escapeDigits)
{ {
AGIChannel channel = this.Channel; AGIChannel channel = this.Channel;
channel.SendCommand(new Command.SayPhoneticCommand(text, escapeDigits)); AGIReply lastReply = channel.SendCommand(new Command.SayPhoneticCommand(text, escapeDigits));
return channel.LastReply.ResultCodeAsChar; return lastReply.ResultCodeAsChar;
} }
#endregion #endregion
@ -399,8 +399,8 @@ namespace AsterNET.FastAGI
protected internal char SayAlpha(string text, string escapeDigits) protected internal char SayAlpha(string text, string escapeDigits)
{ {
AGIChannel channel = this.Channel; AGIChannel channel = this.Channel;
channel.SendCommand(new Command.SayAlphaCommand(text, escapeDigits)); AGIReply lastReply = channel.SendCommand(new Command.SayAlphaCommand(text, escapeDigits));
return channel.LastReply.ResultCodeAsChar; return lastReply.ResultCodeAsChar;
} }
#endregion #endregion
@ -426,8 +426,8 @@ namespace AsterNET.FastAGI
protected internal char SayTime(long time, string escapeDigits) protected internal char SayTime(long time, string escapeDigits)
{ {
AGIChannel channel = this.Channel; AGIChannel channel = this.Channel;
channel.SendCommand(new Command.SayTimeCommand(time, escapeDigits)); AGIReply lastReply = channel.SendCommand(new Command.SayTimeCommand(time, escapeDigits));
return channel.LastReply.ResultCodeAsChar; return lastReply.ResultCodeAsChar;
} }
#endregion #endregion
@ -440,10 +440,10 @@ namespace AsterNET.FastAGI
protected internal string GetVariable(string name) protected internal string GetVariable(string name)
{ {
AGIChannel channel = this.Channel; AGIChannel channel = this.Channel;
channel.SendCommand(new Command.GetVariableCommand(name)); AGIReply lastReply = channel.SendCommand(new Command.GetVariableCommand(name));
if (channel.LastReply.ResultCode != 1) if (lastReply.ResultCode != 1)
return null; return null;
return channel.LastReply.Extra; return lastReply.Extra;
} }
#endregion #endregion
@ -468,8 +468,8 @@ namespace AsterNET.FastAGI
protected internal char WaitForDigit(int timeout) protected internal char WaitForDigit(int timeout)
{ {
AGIChannel channel = this.Channel; AGIChannel channel = this.Channel;
channel.SendCommand(new Command.WaitForDigitCommand(timeout)); AGIReply lastReply = channel.SendCommand(new Command.WaitForDigitCommand(timeout));
return channel.LastReply.ResultCodeAsChar; return lastReply.ResultCodeAsChar;
} }
#endregion #endregion
@ -484,10 +484,10 @@ namespace AsterNET.FastAGI
protected internal string GetFullVariable(string name) protected internal string GetFullVariable(string name)
{ {
AGIChannel channel = this.Channel; AGIChannel channel = this.Channel;
channel.SendCommand(new Command.GetFullVariableCommand(name)); AGIReply lastReply = channel.SendCommand(new Command.GetFullVariableCommand(name));
if (channel.LastReply.ResultCode != 1) if (lastReply.ResultCode != 1)
return null; return null;
return channel.LastReply.Extra; return lastReply.Extra;
} }
#endregion #endregion
@ -502,10 +502,10 @@ namespace AsterNET.FastAGI
protected internal string GetFullVariable(string name, string channelName) protected internal string GetFullVariable(string name, string channelName)
{ {
AGIChannel channel = this.Channel; AGIChannel channel = this.Channel;
channel.SendCommand(new Command.GetFullVariableCommand(name, channelName)); AGIReply lastReply = channel.SendCommand(new Command.GetFullVariableCommand(name, channelName));
if (channel.LastReply.ResultCode != 1) if (lastReply.ResultCode != 1)
return null; return null;
return channel.LastReply.Extra; return lastReply.Extra;
} }
#endregion #endregion
@ -530,8 +530,8 @@ namespace AsterNET.FastAGI
protected internal char SayDateTime(long time, string escapeDigits) protected internal char SayDateTime(long time, string escapeDigits)
{ {
AGIChannel channel = this.Channel; AGIChannel channel = this.Channel;
channel.SendCommand(new Command.SayDateTimeCommand(time, escapeDigits)); AGIReply lastReply = channel.SendCommand(new Command.SayDateTimeCommand(time, escapeDigits));
return channel.LastReply.ResultCodeAsChar; return lastReply.ResultCodeAsChar;
} }
/// <summary> /// <summary>
@ -545,8 +545,8 @@ namespace AsterNET.FastAGI
protected internal char SayDateTime(long time, string escapeDigits, string format) protected internal char SayDateTime(long time, string escapeDigits, string format)
{ {
AGIChannel channel = this.Channel; AGIChannel channel = this.Channel;
channel.SendCommand(new Command.SayDateTimeCommand(time, escapeDigits, format)); AGIReply lastReply = channel.SendCommand(new Command.SayDateTimeCommand(time, escapeDigits, format));
return channel.LastReply.ResultCodeAsChar; return lastReply.ResultCodeAsChar;
} }
/// <summary> /// <summary>
@ -561,8 +561,8 @@ namespace AsterNET.FastAGI
protected internal char SayDateTime(long time, string escapeDigits, string format, string timezone) protected internal char SayDateTime(long time, string escapeDigits, string format, string timezone)
{ {
AGIChannel channel = this.Channel; AGIChannel channel = this.Channel;
channel.SendCommand(new Command.SayDateTimeCommand(time, escapeDigits, format, timezone)); AGIReply lastReply = channel.SendCommand(new Command.SayDateTimeCommand(time, escapeDigits, format, timezone));
return channel.LastReply.ResultCodeAsChar; return lastReply.ResultCodeAsChar;
} }
#endregion #endregion
@ -576,10 +576,10 @@ namespace AsterNET.FastAGI
protected internal string DatabaseGet(string family, string key) protected internal string DatabaseGet(string family, string key)
{ {
AGIChannel channel = this.Channel; AGIChannel channel = this.Channel;
channel.SendCommand(new Command.DatabaseGetCommand(family, key)); AGIReply lastReply = channel.SendCommand(new Command.DatabaseGetCommand(family, key));
if (channel.LastReply.ResultCode != 1) if (lastReply.ResultCode != 1)
return null; return null;
return channel.LastReply.Extra; return lastReply.Extra;
} }
#endregion #endregion
@ -662,8 +662,8 @@ namespace AsterNET.FastAGI
protected internal int RecordFile(string file, string format, string escapeDigits, int timeout) protected internal int RecordFile(string file, string format, string escapeDigits, int timeout)
{ {
AGIChannel channel = this.Channel; AGIChannel channel = this.Channel;
channel.SendCommand(new Command.RecordFileCommand(file, format, escapeDigits, timeout)); AGIReply lastReply = channel.SendCommand(new Command.RecordFileCommand(file, format, escapeDigits, timeout));
return channel.LastReply.ResultCode; return lastReply.ResultCode;
} }
/// <summary> /// <summary>
@ -687,8 +687,8 @@ namespace AsterNET.FastAGI
protected internal int RecordFile(string file, string format, string escapeDigits, int timeout, int offset, bool beep, int maxSilence) protected internal int RecordFile(string file, string format, string escapeDigits, int timeout, int offset, bool beep, int maxSilence)
{ {
AGIChannel channel = this.Channel; AGIChannel channel = this.Channel;
channel.SendCommand(new Command.RecordFileCommand(file, format, escapeDigits, timeout, offset, beep, maxSilence)); AGIReply lastReply = channel.SendCommand(new Command.RecordFileCommand(file, format, escapeDigits, timeout, offset, beep, maxSilence));
return channel.LastReply.ResultCode; return lastReply.ResultCode;
} }
#endregion #endregion
@ -710,8 +710,8 @@ namespace AsterNET.FastAGI
protected internal int ControlStreamFile(string file) protected internal int ControlStreamFile(string file)
{ {
AGIChannel channel = this.Channel; AGIChannel channel = this.Channel;
channel.SendCommand(new Command.ControlStreamFileCommand(file)); AGIReply lastReply = channel.SendCommand(new Command.ControlStreamFileCommand(file));
return channel.LastReply.ResultCode; return lastReply.ResultCode;
} }
/// <summary> /// <summary>
/// Plays the given file, allowing playback to be interrupted by the given /// Plays the given file, allowing playback to be interrupted by the given
@ -731,8 +731,8 @@ namespace AsterNET.FastAGI
protected internal int ControlStreamFile(string file, string escapeDigits) protected internal int ControlStreamFile(string file, string escapeDigits)
{ {
AGIChannel channel = this.Channel; AGIChannel channel = this.Channel;
channel.SendCommand(new Command.ControlStreamFileCommand(file, escapeDigits)); AGIReply lastReply = channel.SendCommand(new Command.ControlStreamFileCommand(file, escapeDigits));
return channel.LastReply.ResultCode; return lastReply.ResultCode;
} }
/// <summary> /// <summary>
/// Plays the given file, allowing playback to be interrupted by the given /// Plays the given file, allowing playback to be interrupted by the given
@ -753,8 +753,8 @@ namespace AsterNET.FastAGI
protected internal int ControlStreamFile(string file, string escapeDigits, int offset) protected internal int ControlStreamFile(string file, string escapeDigits, int offset)
{ {
AGIChannel channel = this.Channel; AGIChannel channel = this.Channel;
channel.SendCommand(new Command.ControlStreamFileCommand(file, escapeDigits, offset)); AGIReply lastReply = channel.SendCommand(new Command.ControlStreamFileCommand(file, escapeDigits, offset));
return channel.LastReply.ResultCode; return lastReply.ResultCode;
} }
/// <summary> /// <summary>
/// Plays the given file, allowing playback to be interrupted by the given /// Plays the given file, allowing playback to be interrupted by the given
@ -778,8 +778,8 @@ namespace AsterNET.FastAGI
protected internal int ControlStreamFile(string file, string escapeDigits, int offset, string forwardDigit, string rewindDigit, string pauseDigit) protected internal int ControlStreamFile(string file, string escapeDigits, int offset, string forwardDigit, string rewindDigit, string pauseDigit)
{ {
AGIChannel channel = this.Channel; AGIChannel channel = this.Channel;
channel.SendCommand(new Command.ControlStreamFileCommand(file, escapeDigits, offset, forwardDigit, rewindDigit, pauseDigit)); AGIReply lastReply = channel.SendCommand(new Command.ControlStreamFileCommand(file, escapeDigits, offset, forwardDigit, rewindDigit, pauseDigit));
return channel.LastReply.ResultCode; return lastReply.ResultCode;
} }
#endregion #endregion