using System; using System.Collections.Concurrent; using System.IO; using System.Net; using System.Net.Sockets; using Christie_net.codegear; using Christie_net.datagear.command; using Christie_net.Test.Example.RemoteTake; namespace Christie_net.daemon { public class Connection { public Socket socket; public string name; public CodeGearManager cgm; public BlockingCollection sendQueue = new BlockingCollection(); public bool sendManager = true; public NetworkStream stream; private object syncObject = new object(); public Connection(Socket socket, CodeGearManager cgm) { this.socket = socket; this.cgm = cgm; stream = new NetworkStream(this.socket); } public void SendCommand(Command cmd) { sendQueue.TryAdd(cmd); } /// /// socketが接続しているhostnameとそのport番号を返す /// /// public string GetInfoString() { IPEndPoint endPoint = (IPEndPoint) socket.RemoteEndPoint; IPAddress ipAddress = endPoint.Address; IPHostEntry hostEntry = Dns.GetHostEntry(ipAddress); // Dns.GetHostEntry(((IPEndPoint) socket.RemoteEndPoint).Address.ToString()).HostName + ":" +((IPEndPoint) socket.RemoteEndPoint).Port; return hostEntry.HostName + ":" + endPoint.Port; } /// /// socketを閉じる /// public void Close() { socket.Shutdown(SocketShutdown.Both); socket.Close(); } /// /// commandの実装に従ってbyte配列に変換し接続先に書き込む /// /// public void Write(Command cmd) { // Debug RTCommand rtcmd = (RTCommand)cmd.dg.GetData(); Console.WriteLine("length:" + rtcmd.line); byte[] buffer = cmd.Convert(); try { while (buffer.Length > 0) { stream.Write(buffer); } } catch (Exception e) { Console.WriteLine(e.StackTrace); } } } }