Skip to content

Commit f570d19

Browse files
committed
Network: move the protocol knowledge to the Remote
Having the knowledge in the Remote will allow us to use different transports more effectively and abstract away the details of the particular way of talking to the remote end. References libgit2#210
1 parent 08da442 commit f570d19

File tree

3 files changed

+68
-18
lines changed

3 files changed

+68
-18
lines changed

LibGit2Sharp/GitTransport.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23
using System.Net.Sockets;
34
using System.Collections.Generic;
45
using System.Text;
@@ -9,6 +10,9 @@ public class GitTransport : ITransport
910
{
1011
Uri uri = null;
1112
List<PktRef> Refs;
13+
bool connected;
14+
Stream stream;
15+
1216
public GitTransport(string url)
1317
{
1418
var uribld = new UriBuilder(url);
@@ -19,6 +23,7 @@ public GitTransport(string url)
1923
uribld.Port = 9418;
2024

2125
uri = uribld.Uri;
26+
connected = false;
2227
}
2328

2429
byte[] GenRequest(Uri uri)
@@ -31,30 +36,25 @@ public void Connect()
3136
{
3237
var req = GenRequest(uri);
3338
var tcp = new TcpClient(uri.Host, uri.Port);
34-
var stream = tcp.GetStream();
39+
stream = tcp.GetStream();
3540
stream.Write(req, 0, req.Length);
36-
var parser = new PktParser(stream);
37-
Refs = new List<PktRef>();
38-
Pkt pkt;
39-
40-
/* Store the lines advertising references, stop at the flush */
41-
while(true) {
42-
pkt = parser.Next();
43-
if (pkt is PktFlush)
44-
break;
45-
46-
if (pkt is PktRef)
47-
Refs.Add(pkt as PktRef);
48-
49-
if (pkt is PktError)
50-
throw new GitProtocolException((pkt as PktError).Error);
51-
}
41+
connected = true;
42+
}
43+
44+
public Stream GetStream()
45+
{
46+
return stream;
5247
}
5348

5449
public List<PktRef> ListRefs()
5550
{
5651
return Refs;
5752
}
53+
54+
public bool Connected()
55+
{
56+
return connected;
57+
}
5858
}
5959
}
6060

LibGit2Sharp/ITransport.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
using System;
2+
using System.IO;
23
using System.Collections.Generic;
34

45
namespace LibGit2Sharp
56
{
67
public interface ITransport
78
{
89
void Connect();
9-
List<PktRef> ListRefs();
10+
bool Connected();
11+
Stream GetStream();
1012
}
1113
}
1214

LibGit2Sharp/Remote.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using System;
2+
using System.Linq;
3+
using System.Collections.Generic;
24
using LibGit2Sharp.Core;
5+
using LibGit2Sharp.Core.Compat;
36
using LibGit2Sharp.Core.Handles;
47

58
namespace LibGit2Sharp
@@ -9,6 +12,51 @@ namespace LibGit2Sharp
912
/// </summary>
1013
public class Remote : IEquatable<Remote>
1114
{
15+
private readonly ITransport transport;
16+
public List<PktRef> Refs { get; private set; }
17+
private Lazy<IEnumerable<string>> refnames;
18+
19+
/* Mostly so that the already-existing code doesn't break */
20+
public Remote()
21+
{
22+
}
23+
24+
public Remote(string url)
25+
{
26+
refnames = new Lazy<IEnumerable<string>>(() => Refs.Select(s => s.Name));
27+
if (url.StartsWith("git://"))
28+
transport = new GitTransport(url);
29+
else
30+
throw new NotImplementedException("This transport" + url + "isn't supported, sorry");
31+
}
32+
33+
public IEnumerable<string> RefNames
34+
{
35+
get {
36+
return refnames.Value;
37+
}
38+
}
39+
40+
public void Connect()
41+
{
42+
transport.Connect();
43+
/* Store the lines advertising references, stop at the flush */
44+
var parser = new PktParser(transport.GetStream());
45+
Refs = new List<PktRef>();
46+
47+
while(true) {
48+
var pkt = parser.Next();
49+
if (pkt is PktFlush)
50+
break;
51+
52+
if (pkt is PktRef)
53+
Refs.Add(pkt as PktRef);
54+
55+
if (pkt is PktError)
56+
throw new GitProtocolException((pkt as PktError).Error);
57+
}
58+
}
59+
1260
private static readonly LambdaEqualityHelper<Remote> equalityHelper =
1361
new LambdaEqualityHelper<Remote>(new Func<Remote, object>[] { x => x.Name, x => x.Url });
1462

0 commit comments

Comments
 (0)