Skip to content

Commit c76b9dc

Browse files
committed
update
1 parent effb405 commit c76b9dc

20 files changed

+478
-68
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ dolphindb_csharpapi/obj/
3333
.vs/dolphindb_csharpapi/v15/.suo
3434
*.cache
3535
*.cache
36+
dolphindb_csharpapi_test/obj/Release/CoreCompileInputs.cache

.vs/dolphindb_csharpapi/v15/.suo

14 KB
Binary file not shown.

dolphindb_csharpapi.sln

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dolphindb_csharpapi_test",
99
EndProject
1010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dolphindb_dataAppender", "dolphindb_dataAppender\dolphindb_dataAppender.csproj", "{59591491-5A40-470F-AC0C-EBB66382E5DD}"
1111
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WindowsFormsApp1", "WindowsFormsApp1\WindowsFormsApp1.csproj", "{0DFE182A-7353-42C7-A7EA-7C76104B1958}"
13+
EndProject
1214
Global
1315
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1416
Debug|Any CPU = Debug|Any CPU
@@ -55,6 +57,18 @@ Global
5557
{59591491-5A40-470F-AC0C-EBB66382E5DD}.Release|x64.Build.0 = Release|Any CPU
5658
{59591491-5A40-470F-AC0C-EBB66382E5DD}.Release|x86.ActiveCfg = Release|Any CPU
5759
{59591491-5A40-470F-AC0C-EBB66382E5DD}.Release|x86.Build.0 = Release|Any CPU
60+
{0DFE182A-7353-42C7-A7EA-7C76104B1958}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
61+
{0DFE182A-7353-42C7-A7EA-7C76104B1958}.Debug|Any CPU.Build.0 = Debug|Any CPU
62+
{0DFE182A-7353-42C7-A7EA-7C76104B1958}.Debug|x64.ActiveCfg = Debug|Any CPU
63+
{0DFE182A-7353-42C7-A7EA-7C76104B1958}.Debug|x64.Build.0 = Debug|Any CPU
64+
{0DFE182A-7353-42C7-A7EA-7C76104B1958}.Debug|x86.ActiveCfg = Debug|Any CPU
65+
{0DFE182A-7353-42C7-A7EA-7C76104B1958}.Debug|x86.Build.0 = Debug|Any CPU
66+
{0DFE182A-7353-42C7-A7EA-7C76104B1958}.Release|Any CPU.ActiveCfg = Release|Any CPU
67+
{0DFE182A-7353-42C7-A7EA-7C76104B1958}.Release|Any CPU.Build.0 = Release|Any CPU
68+
{0DFE182A-7353-42C7-A7EA-7C76104B1958}.Release|x64.ActiveCfg = Release|Any CPU
69+
{0DFE182A-7353-42C7-A7EA-7C76104B1958}.Release|x64.Build.0 = Release|Any CPU
70+
{0DFE182A-7353-42C7-A7EA-7C76104B1958}.Release|x86.ActiveCfg = Release|Any CPU
71+
{0DFE182A-7353-42C7-A7EA-7C76104B1958}.Release|x86.Build.0 = Release|Any CPU
5872
EndGlobalSection
5973
GlobalSection(SolutionProperties) = preSolution
6074
HideSolutionNode = FALSE

dolphindb_csharpapi/DBConnection.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ public bool connect()
133133
{
134134
close();
135135
throw new IOException("Invalid ack msg : " + line);
136-
//return false;
137136
}
138137
sessionID = line.Substring(0, endPos);
139138

@@ -317,7 +316,7 @@ public virtual IEntity run(string script, ProgressListener listener)
317316
@out.writeBytes(body);
318317
@out.flush();
319318

320-
@in = remoteLittleEndian ? (ExtendedDataInput)new LittleEndianDataInputStream(new BufferedStream(new NetworkStream(socket))) : (ExtendedDataInput)new BigEndianDataInputStream(new BufferedStream(new NetworkStream(socket)));
319+
@in = remoteLittleEndian ? new LittleEndianDataInputStream(new BufferedStream(new NetworkStream(socket))) : (ExtendedDataInput)new BigEndianDataInputStream(new BufferedStream(new NetworkStream(socket)));
321320

322321
header = @in.readLine();
323322
}
@@ -338,7 +337,7 @@ public virtual IEntity run(string script, ProgressListener listener)
338337
@out.writeBytes(body);
339338
@out.flush();
340339

341-
@in = remoteLittleEndian ? (ExtendedDataInput)new LittleEndianDataInputStream(new BufferedStream(new NetworkStream(socket))) : (ExtendedDataInput)new BigEndianDataInputStream(new BufferedStream(new NetworkStream(socket)));
340+
@in = remoteLittleEndian ? new LittleEndianDataInputStream(new BufferedStream(new NetworkStream(socket))) : (ExtendedDataInput)new BigEndianDataInputStream(new BufferedStream(new NetworkStream(socket)));
342341
header = @in.readLine();
343342
reconnect = true;
344343
}
@@ -471,7 +470,7 @@ public virtual IEntity run(string function, IList<IEntity> arguments)
471470
}
472471
@out.flush();
473472

474-
@in = remoteLittleEndian ? (ExtendedDataInput)new LittleEndianDataInputStream(new BufferedStream(new NetworkStream(socket))) : (ExtendedDataInput)new BigEndianDataInputStream(new BufferedStream(new NetworkStream(socket)));
473+
@in = remoteLittleEndian ? new LittleEndianDataInputStream(new BufferedStream(new NetworkStream(socket))) : (ExtendedDataInput)new BigEndianDataInputStream(new BufferedStream(new NetworkStream(socket)));
475474
headers = @in.readLine().Split(' ');
476475
}
477476
catch (IOException ex)
@@ -496,7 +495,7 @@ public virtual IEntity run(string function, IList<IEntity> arguments)
496495
}
497496
@out.flush();
498497

499-
@in = remoteLittleEndian ? (ExtendedDataInput)new LittleEndianDataInputStream(new BufferedStream(new NetworkStream(socket))) : (ExtendedDataInput)new BigEndianDataInputStream(new BufferedStream(new NetworkStream(socket)));
498+
@in = remoteLittleEndian ? new LittleEndianDataInputStream(new BufferedStream(new NetworkStream(socket))) : (ExtendedDataInput)new BigEndianDataInputStream(new BufferedStream(new NetworkStream(socket)));
500499
headers = @in.readLine().Split(' ');
501500
reconnect = true;
502501
}
@@ -670,7 +669,7 @@ public virtual void upload(IDictionary<string, IEntity> variableObjectMap)
670669
}
671670
}
672671

673-
ExtendedDataInput @in = remoteLittleEndian ? (ExtendedDataInput)new LittleEndianDataInputStream(new BufferedStream(new NetworkStream(socket))) : (ExtendedDataInput)new BigEndianDataInputStream(new BufferedStream(new NetworkStream(socket)));
672+
ExtendedDataInput @in = remoteLittleEndian ? new LittleEndianDataInputStream(new BufferedStream(new NetworkStream(socket))) : (ExtendedDataInput)new BigEndianDataInputStream(new BufferedStream(new NetworkStream(socket)));
674673

675674
string[] headers = @in.readLine().Split(' ');
676675
if (headers.Length != 3)
Binary file not shown.
Binary file not shown.
Binary file not shown.

dolphindb_csharpapi/dolphindb_csharpapi.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
<DebugType>full</DebugType>
1818
<Optimize>false</Optimize>
1919
<OutputPath>bin\Debug\</OutputPath>
20-
<DefineConstants>DEBUG;TRACE</DefineConstants>
20+
<DefineConstants>TRACE;DEBUG</DefineConstants>
2121
<ErrorReport>prompt</ErrorReport>
2222
<WarningLevel>4</WarningLevel>
23+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
2324
</PropertyGroup>
2425
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
2526
<DebugType>pdbonly</DebugType>

dolphindb_csharpapi/streaming/AbstractClient.cs

Lines changed: 186 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,103 @@ public abstract class AbstractClient : MessageDispatcher
1313
protected static readonly int DEFAULT_PORT = 8849;
1414
protected static readonly string DEFAULT_HOST = "localhost";
1515
protected static readonly string DEFAULT_ACTION_NAME = "csharpStreamingApi";
16-
protected string localIP;
1716
protected int listeningPort;
1817
protected QueueManager queueManager = new QueueManager();
1918
protected Dictionary<string, List<IMessage>> messageCache = new Dictionary<string, List<IMessage>>();
20-
protected Dictionary<string, string> tableName2Topic = new Dictionary<string, string>();
19+
protected Dictionary<string, string> tableNameToTopic = new Dictionary<string, string>();
2120
protected Dictionary<string, bool> hostEndian = new Dictionary<string, bool>();
2221
protected Thread pThread;
22+
protected Dictionary<string, Site> topicToSite = new Dictionary<string, Site>();
23+
24+
protected class Site
25+
{
26+
public string host;
27+
public int port;
28+
public string tableName;
29+
public string actionName;
30+
public MessageHandler handler;
31+
public long msgId;
32+
public bool reconnect;
33+
public IVector filter = null;
34+
public bool closed = false;
35+
36+
public Site(string host, int port, string tableName, string actionName, MessageHandler handler, long msgId, bool reconnect, IVector filter)
37+
{
38+
this.host = host;
39+
this.port = port;
40+
this.tableName = tableName;
41+
this.actionName = actionName;
42+
this.handler = handler;
43+
this.msgId = msgId;
44+
this.reconnect = reconnect;
45+
this.filter = filter;
46+
}
47+
}
48+
49+
abstract protected void doReconnect(Site site);
50+
51+
public void setMsgId(string topic, long msgId)
52+
{
53+
lock (topicToSite)
54+
{
55+
Site site = topicToSite[topic];
56+
if (site != null)
57+
site.msgId = msgId;
58+
}
59+
}
60+
61+
public void tryReconnect(string topic)
62+
{
63+
Console.WriteLine("Trigger reconnect");
64+
queueManager.removeQueue(topic);
65+
Site site = null;
66+
lock (topicToSite)
67+
{
68+
site = topicToSite[topic];
69+
}
70+
if (site == null || !site.reconnect)
71+
return;
72+
tableNameToTopic.Remove(site.host + ":" + site.port + ":" + site.tableName);
73+
topicToSite.Remove(topic);
74+
activeCloseConnection(site);
75+
doReconnect(site);
76+
}
77+
78+
private void activeCloseConnection(Site site)
79+
{
80+
while (true)
81+
{
82+
try
83+
{
84+
DBConnection conn = new DBConnection();
85+
conn.connect(site.host, site.port);
86+
try
87+
{
88+
string localIP = conn.LocalAddress;
89+
List<IEntity> @params = new List<IEntity>
90+
{
91+
new BasicString(localIP),
92+
new BasicInt(listeningPort)
93+
};
94+
conn.run("activeClosePublishConnection", @params);
95+
}
96+
catch (Exception ioex)
97+
{
98+
throw ioex;
99+
}
100+
finally
101+
{
102+
conn.close();
103+
}
104+
return;
105+
}
106+
catch (Exception)
107+
{
108+
Console.WriteLine("Unable to actively close the publish connection from site " + site.host + ":" + site.port);
109+
}
110+
Thread.Sleep(1000);
111+
}
112+
}
23113

24114
public AbstractClient() : this(DEFAULT_PORT) { }
25115

@@ -90,61 +180,121 @@ public bool isRemoteLittleEndian(string host)
90180
return false;
91181
}
92182

93-
protected BlockingCollection<List<IMessage>> subscribeInternal(string host, int port, string tableName, string actionName, long offset)
183+
public bool isClosed(string topic)
184+
{
185+
lock (topicToSite)
186+
{
187+
Site site = topicToSite[topic];
188+
if (site != null)
189+
return site.closed;
190+
else
191+
return true;
192+
}
193+
}
194+
195+
protected BlockingCollection<List<IMessage>> subscribeInternal(string host, int port, string tableName, string actionName, MessageHandler handler, long offset, bool reconnect, IVector filter)
94196
{
95-
IEntity re;
96197
string topic = "";
198+
IEntity re;
97199

98200
DBConnection dbConn = new DBConnection();
99201
dbConn.connect(host, port);
100-
localIP = dbConn.LocalAddress;
202+
try
203+
{
204+
string localIP = dbConn.LocalAddress;
101205

102-
if (!hostEndian.ContainsKey(host))
103-
hostEndian.Add(host, dbConn.RemoteLittleEndian);
206+
if (!hostEndian.ContainsKey(host))
207+
hostEndian.Add(host, dbConn.RemoteLittleEndian);
104208

105-
List<IEntity> @params = new List<IEntity>
106-
{
107-
new BasicString(tableName),
108-
new BasicString(actionName)
109-
};
110-
re = dbConn.run("getSubscriptionTopic", @params);
111-
topic = ((BasicAnyVector)re).getEntity(0).getString();
112-
BlockingCollection<List<IMessage>> queue = queueManager.addQueue(topic);
113-
@params.Clear();
209+
List<IEntity> @params = new List<IEntity>
210+
{
211+
new BasicString(tableName),
212+
new BasicString(actionName)
213+
};
214+
re = dbConn.run("getSubscriptionTopic", @params);
215+
topic = ((BasicAnyVector)re).getEntity(0).getString();
216+
@params.Clear();
114217

115-
tableName2Topic.Add(host + ":" + port + ":" + tableName, topic);
218+
lock (tableNameToTopic)
219+
{
220+
tableNameToTopic.Add(host + ":" + port + ":" + tableName, topic);
221+
}
222+
lock (topicToSite)
223+
{
224+
topicToSite.Add(topic, new Site(host, port, tableName, actionName, handler, offset - 1, reconnect, filter));
225+
}
116226

117-
@params.Add(new BasicString(localIP));
118-
@params.Add(new BasicInt(listeningPort));
119-
@params.Add(new BasicString(tableName));
120-
@params.Add(new BasicString(actionName));
121-
if (offset != -1)
227+
@params.Add(new BasicString(localIP));
228+
@params.Add(new BasicInt(listeningPort));
229+
@params.Add(new BasicString(tableName));
230+
@params.Add(new BasicString(actionName));
122231
@params.Add(new BasicLong(offset));
123-
dbConn.run("publishTable", @params);
124-
125-
dbConn.close();
232+
if (filter != null)
233+
@params.Add(filter);
234+
re = dbConn.run("publishTable", @params);
235+
}
236+
catch (Exception ex)
237+
{
238+
throw ex;
239+
}
240+
finally
241+
{
242+
dbConn.close();
243+
}
244+
245+
BlockingCollection<List<IMessage>> queue = queueManager.addQueue(topic);
126246
return queue;
127247
}
128248

249+
protected BlockingCollection<List<IMessage>> subscribeInternal(string host, int port, string tableName, string actionName, long offset, bool reconnect)
250+
{
251+
return subscribeInternal(host, port, tableName, actionName, null, offset, reconnect, null);
252+
}
253+
129254
protected BlockingCollection<List<IMessage>> subscribeInternal(string host, int port, string tableName, long offset)
130255
{
131-
return subscribeInternal(host, port, tableName, DEFAULT_ACTION_NAME, offset);
256+
return subscribeInternal(host, port, tableName, DEFAULT_ACTION_NAME, offset, false);
132257
}
133258

134259
protected void unsubscribeInternal(string host, int port, string tableName, string actionName)
135260
{
136261
DBConnection dbConn = new DBConnection();
137262
dbConn.connect(host, port);
138-
localIP = dbConn.LocalAddress;
139-
List<IEntity> @params = new List<IEntity>
140-
{
141-
new BasicString(localIP),
142-
new BasicInt(port),
143-
new BasicString(tableName),
144-
new BasicString(actionName)
145-
};
146-
dbConn.run("stopPublishTable", @params);
147-
dbConn.close();
263+
try
264+
{
265+
string localIP = dbConn.LocalAddress;
266+
List<IEntity> @params = new List<IEntity>
267+
{
268+
new BasicString(localIP),
269+
new BasicInt(listeningPort),
270+
new BasicString(tableName),
271+
new BasicString(actionName)
272+
};
273+
dbConn.run("stopPublishTable", @params);
274+
string topic = null;
275+
string fullTableName = host + ":" + port + ":" + tableName;
276+
lock (tableNameToTopic)
277+
{
278+
topic = tableNameToTopic[fullTableName];
279+
}
280+
lock (topicToSite)
281+
{
282+
Site site = topicToSite[topic];
283+
if (site != null)
284+
site.closed = true;
285+
}
286+
Console.WriteLine("Successfully unsubscribed table " + fullTableName);
287+
}
288+
catch (Exception ex)
289+
{
290+
Console.WriteLine(ex);
291+
throw ex;
292+
}
293+
finally
294+
{
295+
dbConn.close();
296+
}
297+
return;
148298
}
149299

150300
protected void unsubscribeInternal(string host, int port, string tableName)

0 commit comments

Comments
 (0)