Skip to content

Commit

Permalink
Added logging via Common.Logging.
Browse files Browse the repository at this point in the history
Reworked the way auto-reconnect/auto-rebind works.
Added a TODO file.
Minor AsyncSocketClient fixes.
  • Loading branch information
pruiz committed Aug 21, 2011
1 parent ac5fec8 commit b93223a
Show file tree
Hide file tree
Showing 12 changed files with 343 additions and 173 deletions.
2 changes: 2 additions & 0 deletions AberrantSMPP.sln
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Gallio", "Gallio", "{1AA7F5
Libs\Gallio\MbUnit35.xml = Libs\Gallio\MbUnit35.xml
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Packages", "Packages", "{99427D87-ABCF-4A62-91B1-4822133B029C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
25 changes: 22 additions & 3 deletions AberrantSMPP/ASyncSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ namespace AberrantSMPP
/// </summary>
internal class AsyncSocketClient : IDisposable
{
private static readonly global::Common.Logging.ILog _Log = global::Common.Logging.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

#region delegates

/// <summary>
Expand Down Expand Up @@ -153,6 +155,8 @@ public AsyncSocketClient(Int32 bufferSize, object stateObject,
MessageHandler msgHandler, SocketClosingHandler closingHandler,
ErrorHandler errHandler)
{
_Log.DebugFormat("Initializing new instance ({0}).", this.GetHashCode());

//allocate buffer
// clientBufferSize = bufferSize;
// _Buffer = new byte[clientBufferSize];
Expand Down Expand Up @@ -193,6 +197,7 @@ public void Dispose()
{
try
{
_Log.DebugFormat("Disposing instance ({0}).", this.GetHashCode());
_IsDisposed = true;
Disconnect();
}
Expand All @@ -211,7 +216,9 @@ public void Connect(String address, Int16 port)
//do we already have an open connection?
if (_NetworkStream != null)
throw new InvalidOperationException("Already connected to remote host.");


_Log.DebugFormat("Connecting instance ({0}) to {1}:{2}.", this.GetHashCode(), address, port);

_ServerAddress = address;
_ServerPort = port;

Expand All @@ -235,6 +242,8 @@ public void Connect(String address, Int16 port)
/// </summary>
public void Disconnect()
{
_Log.DebugFormat("Disconnecting instance ({0}).", this.GetHashCode());

//close down the connection, making sure it exists first
if (_NetworkStream != null)
{
Expand Down Expand Up @@ -267,6 +276,7 @@ public void Send(byte[] buffer)
{
if (_SendPending)
{
_Log.DebugFormat("Instance {0} => Queuing data for transmission..", this.GetHashCode());
_SendQueue.Enqueue(buffer);
}
else
Expand Down Expand Up @@ -307,8 +317,10 @@ private void SendComplete(IAsyncResult state)
{
_NetworkStream.EndWrite(state);
}
catch
{}
catch (Exception ex)
{
_Log.Warn(string.Format("Instance {0} => Async send failed.", this.GetHashCode()), ex);
}

// If there are more packets to send..

Expand All @@ -319,6 +331,8 @@ private void SendComplete(IAsyncResult state)
if (_SendQueue.Count == 0)
return;

_Log.DebugFormat("Instance {0} => Sending queued packet.", this.GetHashCode());

// Send another packet..
Send(_SendQueue.Dequeue());
// Reduce queue internal space..
Expand Down Expand Up @@ -347,6 +361,10 @@ private void ReceiveComplete(IAsyncResult state)
//send the incoming message to the message handler
_MessageHandler(this);
}
catch (Exception ex)
{
_Log.Error(string.Format("Instance {0} => Receive message handler failed.", this.GetHashCode()), ex);
}
finally
{
//start listening again
Expand All @@ -363,6 +381,7 @@ private void ReceiveComplete(IAsyncResult state)
}
finally
{
_Log.WarnFormat("Instance {0} => Connection terminated, disposing..", this.GetHashCode());
Dispose();
}
}
Expand Down
6 changes: 6 additions & 0 deletions AberrantSMPP/AberrantSMPP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,24 @@
<AssemblyOriginatorKeyFile>AberrantSMPP.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="Common.Logging">
<HintPath>..\packages\Common.Logging.2.0.0\lib\2.0\Common.Logging.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ASyncSocketClient.cs" />
<Compile Include="EventObject\AlertEventArgs.cs" />
<Compile Include="EventObject\BindErrorEventArgs.cs" />
<Compile Include="EventObject\BindEventArgs.cs" />
<Compile Include="EventObject\BindRespEventArgs.cs" />
<Compile Include="EventObject\CancelEventArgs.cs" />
<Compile Include="EventObject\CancelSmEventArgs.cs" />
<Compile Include="EventObject\CancelSmRespEventArgs.cs" />
<Compile Include="EventObject\CommonErrorEventArgs.cs" />
<Compile Include="EventObject\SendErrorEventArgs.cs" />
<Compile Include="EventObject\DataSmEventArgs.cs" />
<Compile Include="EventObject\DataSmRespEventArgs.cs" />
<Compile Include="EventObject\DeliverSmEventArgs.cs" />
Expand Down Expand Up @@ -124,6 +129,7 @@
</ItemGroup>
<ItemGroup>
<None Include="AberrantSMPP.snk" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
15 changes: 15 additions & 0 deletions AberrantSMPP/EventObject/BindErrorEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AberrantSMPP.EventObjects
{
public class BindErrorEventArgs : CommonErrorEventArgs
{
public BindErrorEventArgs(Exception exception)
: base(exception)
{
}
}
}
24 changes: 24 additions & 0 deletions AberrantSMPP/EventObject/SendErrorEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using AberrantSMPP.Packet;

namespace AberrantSMPP.EventObjects
{
public class SendErrorEventArgs : CommonErrorEventArgs
{
private byte[] _data;

public Pdu Packet { get; private set; }
public byte[] Data { get { return (byte[])_data.Clone(); } }

public SendErrorEventArgs(Pdu packet, byte[] data, Exception exception)
: base(exception)
{
Packet = packet;
_data = data;
}
}
}
2 changes: 2 additions & 0 deletions AberrantSMPP/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Linq;
using System.Text;

using Common.Logging;

namespace AberrantSMPP
{
internal static class Helper
Expand Down
Loading

0 comments on commit b93223a

Please sign in to comment.