Skip to content

Commit 453f50c

Browse files
committed
support more events
1 parent 98444d5 commit 453f50c

File tree

6 files changed

+55
-35
lines changed

6 files changed

+55
-35
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Buffers;
3+
4+
namespace SciSharp.MySQL.Replication
5+
{
6+
7+
class DefaultEventFactory<TEventType> : ILogEventFactory
8+
where TEventType : LogEvent, new()
9+
{
10+
public LogEvent Create()
11+
{
12+
return new TEventType();
13+
}
14+
}
15+
}

src/SciSharp.MySQL.Replication/Events/EmptyPayloadEvent.cs

+1-21
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,9 @@ namespace SciSharp.MySQL.Replication
55
{
66
public sealed class EmptyPayloadEvent : LogEvent
77
{
8-
public EmptyPayloadEvent(LogEventType eventType)
9-
{
10-
EventType = eventType;
11-
}
12-
138
protected internal override void DecodeBody(ref SequenceReader<byte> reader)
149
{
15-
16-
}
17-
}
18-
19-
class EmptyPayloadEventFactory : ILogEventFactory
20-
{
21-
public LogEventType EventType { get; private set; }
22-
23-
public EmptyPayloadEventFactory(LogEventType eventType)
24-
{
25-
EventType = eventType;
26-
}
27-
28-
public LogEvent Create()
29-
{
30-
return new EmptyPayloadEvent(EventType);
10+
3111
}
3212
}
3313
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Buffers;
3+
4+
namespace SciSharp.MySQL.Replication
5+
{
6+
public sealed class NotImplementedEvent : LogEvent
7+
{
8+
protected internal override void DecodeBody(ref SequenceReader<byte> reader)
9+
{
10+
11+
}
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Buffers;
3+
using System.Text;
4+
using SuperSocket.ProtoBase;
5+
6+
namespace SciSharp.MySQL.Replication
7+
{
8+
public sealed class XIDEvent : LogEvent
9+
{
10+
public long TransactionID { get; set; }
11+
12+
protected internal override void DecodeBody(ref SequenceReader<byte> reader)
13+
{
14+
reader.TryReadLittleEndian(out long tarnsID);
15+
TransactionID = tarnsID;
16+
}
17+
}
18+
}

src/SciSharp.MySQL.Replication/LogEvent.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public abstract class LogEvent
77
{
88
public static ChecksumType ChecksumType { get; internal set; }
99
public DateTime Timestamp { get; set; }
10-
public LogEventType EventType { get; set; }
10+
public LogEventType EventType { get; internal set; }
1111
public int ServerID { get; set; }
1212
public int EventSize { get; set; }
1313
public int Position { get; set; }

src/SciSharp.MySQL.Replication/LogEventPackageDecoder.cs

+7-13
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@ namespace SciSharp.MySQL.Replication
1111
*/
1212
class LogEventPackageDecoder : IPackageDecoder<LogEvent>
1313
{
14-
class DefaultLogEventFactory<TLogEvent> : ILogEventFactory
15-
where TLogEvent : LogEvent, new()
16-
{
17-
public LogEvent Create()
18-
{
19-
return new TLogEvent();
20-
}
21-
}
2214

2315
private static readonly DateTime _unixEpoch = new DateTime(1970, 1, 1);
2416

@@ -28,11 +20,11 @@ internal static DateTime GetTimestapmFromUnixEpoch(int seconds)
2820
}
2921

3022
private static Dictionary<LogEventType, ILogEventFactory> _logEventFactories = new Dictionary<LogEventType, ILogEventFactory>();
31-
23+
private static ILogEventFactory _notImplementedEventFactory = new DefaultEventFactory<NotImplementedEvent>();
3224
internal static void RegisterLogEventType<TLogEvent>(LogEventType eventType)
3325
where TLogEvent : LogEvent, new()
3426
{
35-
RegisterLogEventType(eventType, new DefaultLogEventFactory<TLogEvent>());
27+
RegisterLogEventType(eventType, new DefaultEventFactory<TLogEvent>());
3628
}
3729

3830
internal static void RegisterLogEventType(LogEventType eventType, ILogEventFactory factory)
@@ -44,16 +36,18 @@ internal static void RegisterEmptyPayloadEventTypes(params LogEventType[] eventT
4436
{
4537
foreach (var eventType in eventTypes)
4638
{
47-
_logEventFactories.Add(eventType, new EmptyPayloadEventFactory(eventType));
39+
_logEventFactories.Add(eventType, new DefaultEventFactory<EmptyPayloadEvent>());
4840
}
4941
}
5042

5143
protected virtual LogEvent CreateLogEvent(LogEventType eventType)
5244
{
5345
if (!_logEventFactories.TryGetValue(eventType, out var factory))
54-
throw new Exception("Unexpected eventType: " + eventType);
46+
factory = _notImplementedEventFactory;
5547

56-
return factory.Create();
48+
var log = factory.Create();
49+
log.EventType = eventType;
50+
return log;
5751
}
5852

5953
public LogEvent Decode(ReadOnlySequence<byte> buffer)

0 commit comments

Comments
 (0)