Create a gist now

Instantly share code, notes, and snippets.

@kou-yeung /Session.cs Secret
Created Feb 17, 2017

// ユーザのセッション情報
// TcpListenerにアクセス要求が来たときに作成されます
using System;
using System.Net.Sockets;
public class Session
{
TcpClient client;
NetworkStream stream;
Action<Session, Command> commandExec;
public Session(TcpClient client, Action<Session, Command> commandExec)
{
this.client = client;
this.stream = client.GetStream();
this.commandExec = commandExec;
}
public void Poll()
{
while (client.Available > 0)
{
Command cmd;
if(TryParseCommand(out cmd))
{
commandExec(this, cmd);
}
}
}
bool TryParseCommand(out Command cmd)
{
// stream -> Command : 今回は解説しない
return true;
}
public void Send(byte[] bytes)
{
stream.Write(bytes, 0, bytes.Length);
stream.Flush();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment