private void OnClientConnect(IAsyncResult asyn) { try { if (!_isListening) { return; } Socket socket = _listener.EndAccept(asyn); if (socket == null) { return; } int id = SocketHelper.GetNewSocketID(); ISocketWorker worker = SocketWorkerFactory.Create(id, socket, this); if (worker == null) { SocketLogMgt.SetLog(SocketLogType.Error, this, "Create socket worker failed."); return; } worker.Open(); //_listener.BeginAccept(new AsyncCallback(OnClientConnect), null); } catch (Exception err) { SocketLogMgt.SetLastError(err); } finally { _listener.BeginAccept(new AsyncCallback(OnClientConnect), null); } }
private Socket Connect(SocketClientConfig config) { if (_socket != null && _socket.Connected) { return(_socket); } Disconnect(); try { SocketLogMgt.SetLog(this, "============================="); SocketLogMgt.SetLog(this, "Opening connection..."); SocketLogMgt.SetLog(this, config); _socketID = string.Format("({0}) ", SocketHelper.GetNewSocketID()); IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(config.IPAddress), config.Port); _socket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp); _socket.ReceiveTimeout = config.ReceiveTimeout; _socket.SendTimeout = config.SendTimeout; _socket.Connect(ipe); if (_socket.Connected) { SocketLogMgt.SetLog(this, _socketID + "Connection opened."); return(_socket); } else { SocketLogMgt.SetLog(SocketLogType.Error, this, _socketID + "Connection failed."); return(null); } } catch (Exception err) { SocketLogMgt.SetLastError(this, err); return(null); } }
public SocketResult SendData(SocketClientConfig config, string content) { SocketResult result = SocketResult.Empty; try { SocketLogMgt.SetLog(this, "============================================="); SocketLogMgt.SetLog(this, ": Send Data Begin."); SocketLogMgt.SetLog(this, config); //string strSend = SocketHelper.PackMessageBlock(content); string strSend = content; if (SocketLogMgt.DumpData) { SocketLogMgt.SetLog(this, ": Data to be sent."); SocketLogMgt.SetLog(this, "------------------------"); SocketLogMgt.SetLog(this, strSend); SocketLogMgt.SetLog(this, "------------------------"); } Byte[] bytesSent = null; System.Text.Encoding encoder = SocketHelper.GetEncoder(config); if (encoder != null) { bytesSent = encoder.GetBytes(strSend); } if (bytesSent == null) { SocketLogMgt.SetLog(SocketLogType.Error, this, "Encode data failed."); return(SocketResult.SendFailed); } Byte[] bytesReceived = new Byte[config.ReceiveResponseBufferSizeKB * 1024]; SocketLogMgt.SetLog(this, ": Socket prepared."); SocketLogMgt.SetLog(this, "------------------------"); string strReceived = null; StringBuilder sb = new StringBuilder(); IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(config.IPAddress), config.Port); using (Socket socket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) { int id = SocketHelper.GetNewSocketID(); string strID = "(" + id.ToString() + ") "; //socket.NoDelay = true; socket.SendTimeout = config.SendTimeout; socket.ReceiveTimeout = config.ReceiveTimeout; SocketLogMgt.SetLog(this, strID + "Socket created."); socket.Connect(ipe); if (socket.Connected) { SocketLogMgt.SetLog(this, strID + "Socket connected."); } else { SocketLogMgt.SetLog(SocketLogType.Warning, this, strID + "Connection failed."); return(SocketResult.Disconnect); } _allDone.Reset(); SocketLogMgt.SetLog(this, strID + "Send data."); socket.BeginSend(bytesSent, 0, bytesSent.Length, 0, new AsyncCallback(OnDataSent), new SocketWrapper(strID, socket)); bool rec = true; while (rec) { if (!socket.Connected) { SocketLogMgt.SetLog(SocketLogType.Warning, this, strID + "Connection closed."); break; } SocketLogMgt.SetLog(this, strID + "Receive data."); int bytes = socket.Receive(bytesReceived, bytesReceived.Length, 0); SocketLogMgt.SetLog(this, strID + "Receive succeeded. " + bytes.ToString() + " bytes."); string str = encoder.GetString(bytesReceived, 0, bytes); sb.Append(str); strReceived = sb.ToString(); //rec = !SocketHelper.FindBlockEnding(strReceived); rec = false; // This socket client assume that there is a completed HL7 message (without MLLP signs) in one Receive(); } _allDone.WaitOne(); // need not to set timeout here, we can depend on socket timeout of the Receive() method. socket.Shutdown(SocketShutdown.Both); socket.Close(); SocketLogMgt.SetLog(this, strID + "Socket disconnected."); } SocketLogMgt.SetLog(this, "------------------------"); if (SocketLogMgt.DumpData) { SocketLogMgt.SetLog(this, ": Data received."); SocketLogMgt.SetLog(this, "------------------------"); SocketLogMgt.SetLog(this, strReceived); SocketLogMgt.SetLog(this, "------------------------"); } //strReceived = SocketHelper.UnpackMessageBlock(strReceived); strReceived = SocketHelper.TrimMessageBlock(strReceived); result = new SocketResult(SocketResultType.Success, strReceived); } catch (Exception err) { SocketLogMgt.SetLastError(this, err); result = new SocketResult(err); } SocketLogMgt.SetLog(this, ": Send Data End"); SocketLogMgt.SetLog(this, "=============================================\r\n"); return(result); }