public MonitorHandle() { // Init Redis redis = InitializeRedis(); Console.WriteLine("Initializing MonitorClient"); recvHandler = new ReceiveHandler(); }
private void start() { string remoteHost = ((IPEndPoint)so.RemoteEndPoint).Address.ToString(); string remotePort = ((IPEndPoint)so.RemoteEndPoint).Port.ToString(); //Console.WriteLine("[Server] Connection established with {0}:{1}\n", remoteHost, remotePort); for (;;) { // Receive Header recvHeader; Packet recvRequest; // get HEADER byte[] headerBytes = getBytes(HEADER_SIZE); if (null == headerBytes) { break; } else { recvHeader = BytesToHeader(headerBytes); recvRequest.header = recvHeader; } //if (headerBytes.Length != HEADER_SIZE && headerBytes[0] == byte.MaxValue) recvHeader = BytesToHeader(headerBytes); recvRequest.header = recvHeader; // get DATA byte[] dataBytes = getBytes(recvHeader.size); if (null == dataBytes) { break; } recvRequest.data = dataBytes; recvHandler = new ReceiveHandler(this, recvRequest); Packet respPacket = recvHandler.GetResponse(); if (-1 != respPacket.header.comm) { byte[] respBytes = PacketToBytes(respPacket); bool sendSuccess = false; sendSuccess = sendBytes(respBytes); if (!sendSuccess) { Console.WriteLine("Send failed."); break; } } if (!isConnected()) { Console.WriteLine("Connection lost with {0}:{1}", remoteHost, remotePort); break; } } Console.WriteLine("Closing connection with {0}:{1}", remoteHost, remotePort); so.Shutdown(SocketShutdown.Both); so.Close(); Console.WriteLine("Connection closed\n"); }