Mercurial > hg > Database > Christie-sharp
view daemon/Connection.cs @ 54:e75e143b92e7
fix Read/Write missing argument and maybe run on Unity
author | riono <e165729@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 14 Jan 2022 20:40:21 +0900 |
parents | 476b6efeca5b |
children |
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; using MessagePack; 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) { byte[] buffer = cmd.Convert(); try { stream.Write(buffer,0 , buffer.Length); } catch (Exception e) { Console.WriteLine(e.StackTrace); } } } }