protected void OnSendComplete(object sender, SocketAsyncEventArgs e) { if (e.SocketError == SocketError.Success) { try { if (e.UserToken != null) { TCPSubmitter tcpSubmitter = e.UserToken as TCPSubmitter; tcpSubmitter.WaitHandle.Set(); } } catch (Exception ex) { Console.WriteLine(""); Console.WriteLine("******************** SOCKET PAYLOAD ERROR **********************"); Console.WriteLine(ex.Message); Console.WriteLine("***************** END SOCKET PAYLOAD ERROR **********************"); Console.WriteLine(""); } } else { Boolean autoReconnectWhenConnectionIsLost = AutoReconnectWhenConnectionIsLost; OnServerDisconnected(tcpClientSocket, ref autoReconnectWhenConnectionIsLost); AutoReconnectWhenConnectionIsLost = autoReconnectWhenConnectionIsLost; ConnectToServer(true); } }
protected void Send(String sendData) { TCPSubmitter tcpSubmitter = new TCPSubmitter(sendData, packetTerminator); SocketAsyncEventArgs socketArgs = new SocketAsyncEventArgs(); socketArgs.UserToken = tcpSubmitter; socketArgs.SetBuffer(tcpSubmitter.SendBuffer, 0, tcpSubmitter.SendBuffer.Length); //socketArgs.Completed += new EventHandler<SocketAsyncEventArgs>(OnSendComplete); bool isASyncSend = tcpClientSocket.SendAsync(socketArgs); tcpSubmitter.WaitHandle = new AutoResetEvent(false); //if (!isASyncSend) OnSendComplete(this, socketArgs); if (!tcpSubmitter.WaitHandle.WaitOne(new TimeSpan(0, 0, 0, 0, this.sendTimeout))) { throw new USSDTcpSendTimeoutException("Socket send timeout"); } }