Mercurial > hg > Database > Christie-sharp
view daemon/Connection.cs @ 39:9217d14cc220
fix NetworkStream
author | riono <e165729@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 25 May 2021 02:35:52 +0900 |
parents | 1236da135f79 |
children | ce46626dddb1 |
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; 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 //Console.WriteLine("length:" + cmd.type); byte[] buffer = cmd.Convert(); try { while (buffer.Length > 0) { stream.Write(buffer); } } catch (Exception e) { Console.WriteLine(e.StackTrace); } } } }