Skip to content

Instantly share code, notes, and snippets.

@kou-yeung
Created February 17, 2017 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kou-yeung/984d43706ab1787691004600a3cc142f to your computer and use it in GitHub Desktop.
Save kou-yeung/984d43706ab1787691004600a3cc142f to your computer and use it in GitHub Desktop.
// ユーザのセッション情報
// 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