Mercurial > hg > Database > Christie-sharp
view daemon/Connection.cs @ 16:7352793b5dbe
add some Commands
author | riono <e165729@ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 22 Nov 2020 01:52:22 +0900 |
parents | e4b46d4ef79c |
children | c9d1a5a79254 |
line wrap: on
line source
using System; using System.Collections.Concurrent; using System.IO; using System.Net; using System.Net.Sockets; using Christie_net.datagear.command; namespace Christie_net.daemon { public class Connection { public Socket socket; public string name; //public CodeGearManager cgm; public ConcurrentQueue<Command> sendQueue = new ConcurrentQueue<Command>(); public bool sendManager = true; private object syncObject = new object(); // public Connection(Socket socket, CodeGearManager cgm) { // this.socket = socket; // this.cgm = cgm; // } public Connection(){} public void SendCommand(Command cmd) { sendQueue.Enqueue(cmd); } /// <summary> /// socketが接続しているhostnameとそのport番号を返す /// </summary> /// <returns></returns> public string GetInfoString() { return (Dns.GetHostEntry(((IPEndPoint) socket.RemoteEndPoint).Address.ToString()).HostName + ":" + ((IPEndPoint) socket.RemoteEndPoint).Port.ToString()); } /// <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) { MemoryStream stream = cmd.Convert(); byte[] buffer = stream.ToArray(); try { while (stream.Length > 0) { socket.Send(buffer); } } catch (Exception e) { Console.WriteLine(e.StackTrace); } } } }