Skip to content

Commit

Permalink
#317 ニコ生の過去コメントの取得に対応
Browse files Browse the repository at this point in the history
  • Loading branch information
tor4kichi committed Oct 27, 2016
1 parent 2577d26 commit 1a96182
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
51 changes: 26 additions & 25 deletions NicoPlayerHohoema/Models/Live/NicoLiveCommentClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public sealed class NicoLiveCommentClient : IDisposable
{
public NiconicoContext NiconicoContext { get; private set; }

private byte[] _Buffer = new byte[1024];
private byte[] _Buffer = new byte[1024 * 2];

TcpClient _Client;

Expand Down Expand Up @@ -139,13 +139,14 @@ public sealed class NicoLiveCommentClient : IDisposable
public uint CommentCount { get; private set; }
public uint WatchCount { get; private set; }

public NicoLiveCommentClient(string liveId, DateTimeOffset baseTime, CommentServer commentServer, NiconicoContext context)
public NicoLiveCommentClient(string liveId, uint commentCount, DateTimeOffset baseTime, CommentServer commentServer, NiconicoContext context)
{
LiveId = liveId;
NiconicoContext = context;
ThreadIdNumber = commentServer.ThreadIds.ElementAt(0);
Host = commentServer.Host;
Port = commentServer.Port;
CommentCount = commentCount;

_BaseTime = baseTime;

Expand All @@ -167,7 +168,7 @@ public async Task Start()
_LiveCommentRecievingTask = DataRecivingTask();

// コメントサーバーに接続開始データを送信
var s = $"<thread thread=\"{ThreadIdNumber}\" version=\"20061206\" res_from=\"-0\" />\0";
var s = $"<thread thread=\"{ThreadIdNumber}\" version=\"20061206\" res_from=\"-{Math.Min(CommentCount, 50)}\" />\0";
var writer = new StreamWriter(_NetworkStream, Encoding.UTF8);
await writer.WriteAsync(s);
writer.Flush();
Expand Down Expand Up @@ -268,8 +269,7 @@ private async Task DataRecivingTask()

try
{
bool isEndConnect = false;
while (!isEndConnect)
while (true)
{
// データが来るまで待つ
while (true)
Expand All @@ -278,12 +278,6 @@ private async Task DataRecivingTask()

using (var releaser = await _NetworkStreamLock.LockAsync())
{
if (_NetworkStream == null)
{
isEndConnect = true;
break;
}

if (_NetworkStream.DataAvailable)
{
break;
Expand All @@ -295,33 +289,38 @@ private async Task DataRecivingTask()
await Task.Delay(50);
}

if (isEndConnect) { break; }

_LiveCommentRecieveCancelSource.Token.ThrowIfCancellationRequested();


// 受信したデータをバッファに読み込む
var sb = new StringBuilder();
using (var releaser = await _NetworkStreamLock.LockAsync())
{
isEndConnect = await _NetworkStream.ReadAsync(_Buffer, 0, _Buffer.Length) == 0;
while (await _NetworkStream.ReadAsync(_Buffer, 0, _Buffer.Length) != 0)
{
var recievedRawString = Encoding.UTF8.GetString(_Buffer);

sb.Append(recievedRawString.TrimEnd('\0'));

Array.Clear(_Buffer, 0, _Buffer.Length);

if (!_NetworkStream.DataAvailable) { break; }
}
}

_LiveCommentRecieveCancelSource.Token.ThrowIfCancellationRequested();

if (!isEndConnect)
// バッファに詰め込まれた文字列を解析する
var xmlStrings = sb.ToString().Split('\0');
foreach (var xmlString in xmlStrings)
{
// バッファに詰め込まれた文字列を解析する
var recievedRawString = Encoding.UTF8.GetString(_Buffer);

Debug.Write($"recieve comment data -> ");
Debug.Write($"recieve data -> ");

var nullStrStart = recievedRawString.IndexOf('\0');
var trimEmptyString = recievedRawString.Remove(nullStrStart);
ParseLiveCommentServerResponse(trimEmptyString);
ParseLiveCommentServerResponse(xmlString);

Debug.WriteLine($" -> end");

Array.Clear(_Buffer, 0, _Buffer.Length);
}
}

}
}
catch (Exception ex)
Expand All @@ -348,6 +347,8 @@ private void ParseLiveCommentServerResponse(string recievedString)
// Note: 寄り厳密にXMLフォーマットチェックをやるなら
// <>の数が同数であることをチェックする
Debug.Write($"illigal format, required XML");
Debug.Write($" -> ");
Debug.Write(recievedString);
return;
}

Expand Down
4 changes: 2 additions & 2 deletions NicoPlayerHohoema/Models/Live/NicoLiveVideo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ private async Task StartCommentClientConnection()
await EndCommentClientConnection();

var baseTime = PlayerStatusResponse.Program.BaseAt;
_NicoLiveCommentClient = new NicoLiveCommentClient(LiveId, baseTime, PlayerStatusResponse.Comment.Server, HohoemaApp.NiconicoContext);
_NicoLiveCommentClient = new NicoLiveCommentClient(LiveId, PlayerStatusResponse.Program.CommentCount, baseTime, PlayerStatusResponse.Comment.Server, HohoemaApp.NiconicoContext);
_NicoLiveCommentClient.CommentServerConnected += _NicoLiveCommentReciever_CommentServerConnected;
_NicoLiveCommentClient.Heartbeat += _NicoLiveCommentClient_Heartbeat;
_NicoLiveCommentClient.EndConnect += _NicoLiveCommentClient_EndConnect;
Expand Down Expand Up @@ -587,7 +587,7 @@ private async void _NicoLiveCommentReciever_CommentRecieved(Chat chat)
{
_LiveComments.Add(chat);
});

if (chat.User_id == BroadcasterId)
Expand Down

0 comments on commit 1a96182

Please sign in to comment.