Alachisoft.NCache.Common.Communication.TcpConnection Maven / Gradle / Ivy
package Alachisoft.NCache.Common.Communication;
import Alachisoft.NCache.Common.Communication.Exceptions.ConnectionException;
import java.io.ObjectInputStream;
import java.net.*;
public class TcpConnection implements IConnection {
private Socket _socket;
private boolean _connected;
private Object _sync_lock = new Object();
private InetAddress _bindIP;
private ObjectInputStream in = null;
public final boolean Connect(String serverIP, int port) throws UnknownHostException, java.io.IOException {
boolean connected = false;
try {
//.net
// Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
SocketAddress sockaddr = new InetSocketAddress(serverIP, port);
Socket socket = new Socket();
// server.bind(null);
if (_bindIP != null) {
socket.bind(sockaddr);
}
socket.connect(sockaddr);
// InetAddress ip = InetAddress.getByName(serverIP);
// socket.connect(new InetSocketAddress(ip, port));
connected = socket.isConnected();
_socket = socket;
} catch (Exception e) {
throw new ConnectionException("[" + serverIP + "] " + e.getMessage());
}
_connected = connected;
return connected;
}
public final void Disconnect() {
try {
if (_connected && _socket != null && _socket.isConnected()) {
_socket.shutdownOutput();
_socket.shutdownInput();
_socket.close();
// _socket.dispose();
}
} catch (Exception e) {
}
}
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: public bool Send(byte[] buffer, int offset, int count)
public final boolean Send(byte[] buffer, int offset, int count) throws ConnectionException {
boolean sent = false;
synchronized (_sync_lock) {
if (_connected) {
int dataSent = 0;
while (count > 0) {
try {
//Muneeb : IN-1015 : Changed _socket.Receive();
com.alachisoft.ncache.serialization.standard.io.ObjectOutputStream os = new com.alachisoft.ncache.serialization.standard.io.ObjectOutputStream(_socket.getOutputStream(), "");
os.write(buffer, offset, count);
os.flush();
dataSent = count;
// dataSent = _socket.Send(buffer, offset, count, SocketFlags.None);
offset += dataSent;
count = count - dataSent;
} catch (java.io.IOException se) {
_connected = false;
throw new ConnectionException(se.getMessage(), se);
}
}
sent = true;
} else {
throw new ConnectionException();
}
}
return sent;
}
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: public bool Receive(byte[] buffer, int count)
public final boolean Receive(byte[] buffer, int count) throws ConnectionException {
boolean received = false;
//lock (_sync_lock)
{
if (_connected) {
int receivedCount = 0;
int offset = 0;
while (count > 0) {
try {
//Muneeb: IN-1015 : Changed _socket.Receive();
receivedCount = _socket.getInputStream().read(buffer, offset, count);
// receivedCount = _socket.Receive(buffer, offset, count, SocketFlags.None);
offset += receivedCount;
count = count - receivedCount;
} catch (java.io.IOException se) {
_connected = false;
throw new ConnectionException(se.getMessage(), se);
}
}
received = true;
} else {
throw new ConnectionException();
}
}
return received;
}
public final boolean getIsConnected() {
if (_connected) {
_connected = _socket.isConnected();
}
return _connected;
}
public final void Bind(String address) throws UnknownHostException {
if (address == null) {
throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: address");
}
_bindIP = InetAddress.getByName(address);
}
}