Mercurial > hg > Database > Christie-sharp
view daemon/Connection.cs @ 43:87d88bc28ac0
bug servey
author | riono <e165729@ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 19 Sep 2021 18:03:04 +0900 |
parents | ce46626dddb1 |
children | 98ee1ee1efb7 |
line wrap: on
line source
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<Command> sendQueue = new BlockingCollection<Command>(); 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); } /// <summary> /// socketが接続しているhostnameとそのport番号を返す /// </summary> /// <returns></returns> 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; } /// <summary> /// socketを閉じる /// </summary> public void Close() { socket.Shutdown(SocketShutdown.Both); socket.Close(); } /// <summary> /// commandの実装に従ってbyte配列に変換し接続先に書き込む /// </summary> /// <param name="cmd"></param> 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); } } } }