| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620 |
- using System;
- using System.Net;
- namespace System.Net.Sockets
- {
- /// <summary>
- /// Summary description for GHStreamSocket.
- /// </summary>
- internal class GHStreamSocketSSL : GHSocket
- {
- java.net.Socket jSocket;
- public GHStreamSocketSSL(java.net.Socket sslSocket)
- {
- jSocket = sslSocket;
- }
- public override int GetHashCode ()
- {
- if (jSocket == null)
- return -1;
- return jSocket.ToString().GetHashCode();
- }
- public int Available_internal(out int error)
- {
- error = 0;
- int r = 0;
- if (jSocket == null || !jSocket.isConnected())
- {
- return r;
- }
- try
- {
- r = jSocket.getInputStream().available();
- }
- catch (Exception e)
- {
- error = 10054; //WSAECONNRESET (Connection reset by peer)
- r = 0;
- #if DEBUG
- Console.WriteLine("Caught exception during Available_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
- #endif
- }
- return r;
- }
- public void Blocking_internal(bool block, out int error)
- {
- //SVETA: see in the non-blocking io
- error = 0;
- if (block == false)
- throw new NotSupportedException();
- }
- public EndPoint LocalEndPoint_internal(out int error)
- {
- error = 0;
- java.net.InetSocketAddress localAddr = null;
- try
- {
- localAddr = (java.net.InetSocketAddress)jSocket.getLocalSocketAddress();
- }
- catch (Exception e)
- {
- localAddr = null;
- #if DEBUG
- Console.WriteLine("Caught exception during LocalEndPoint_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
- #endif
- }
- if (localAddr == null || localAddr.getAddress() == null || localAddr.getPort() < 0)
- {
- return null;
- }
- IPHostEntry lipa = Dns.Resolve(localAddr.getHostName());
- IPEndPoint ret = new IPEndPoint(lipa.AddressList[0], localAddr.getPort());
- return ret;
- }
- public EndPoint RemoteEndPoint_internal(out int error)
- {
- error = 0;
- java.net.InetSocketAddress remoteAddr = null;
- if (jSocket == null || !jSocket.isBound())
- {
- return null;
- }
- try
- {
- remoteAddr = (java.net.InetSocketAddress)jSocket.getRemoteSocketAddress();
- }
- catch (Exception e)
- {
- remoteAddr = null;
- #if DEBUG
- Console.WriteLine("Caught exception during RemoteEndPoint_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
- #endif
- }
- if (remoteAddr == null || remoteAddr.getAddress() == null || remoteAddr.getPort() <= 0)
- {
- error = 10057; //WSAENOTCONN (Socket is not connected)
- return null;
- }
- IPHostEntry lipa = Dns.Resolve(remoteAddr.getHostName());
- IPEndPoint ret = new IPEndPoint(lipa.AddressList[0], remoteAddr.getPort());
- return ret;
- }
- public GHSocket Accept_internal(out int error)
- {
- error = 10022; //WSAEINVAL (Invalid argument)
- return null;
- }
- public void Bind_internal(EndPoint sa, out int error)
- {
- error = 0;
- IPEndPoint addr = sa as IPEndPoint;
- if (addr == null)
- {
- error = 10044; //WSAESOCKTNOSUPPORT (Socket type not supported)
- return;
- }
- if (jSocket == null || jSocket.isBound() || jSocket.isConnected())
- {
- error = 10022; //WSAEINVAL (Invalid argument)
- return;
- }
- try
- {
- jSocket.bind(new java.net.InetSocketAddress(java.net.InetAddress.getByName(addr.Address.ToString()),
- addr.Port));
- }
- catch (Exception e)
- {
- error = 10048; //WSAEADDRINUSE (Address already in use)
- #if DEBUG
- Console.WriteLine("Caught exception during Bind_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
- #endif
- }
- }
- public void Close_internal(out int error)
- {
- error = 0;
- try
- {
- if (jSocket != null)
- {
- jSocket.close();
- jSocket = null;
- }
- }
- catch (Exception e)
- {
- error = 10022; //WSAEINVAL (Invalid argument)
- #if DEBUG
- Console.WriteLine("Caught exception during Close_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
- #endif
- }
- }
- public void Connect_internal(EndPoint sa, out int error)
- {
- error = 0;
- IPEndPoint addr = sa as IPEndPoint;
- if (addr == null)
- {
- error = 10044; //WSAESOCKTNOSUPPORT (Socket type not supported)
- return;
- }
- if (jSocket == null)
- {
- error = 10022; //WSAEINVAL (Invalid argument)
- return;
- }
- if (jSocket.isConnected())
- {
- error = 10056; //WSAEISCONN (Socket is already connected)
- return;
- }
- try
- {
- jSocket.connect(new java.net.InetSocketAddress(
- java.net.InetAddress.getByName(addr.Address.ToString()),
- addr.Port));
- }
- catch (Exception e)
- {
- error = 10061; //WSAECONNREFUSED (Connection refused)
- #if DEBUG
- Console.WriteLine("Caught exception during Connect_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
- #endif
- }
- }
- public void Listen_internal(int backlog, out int error)
- {
- error = 10022; //WSAEINVAL (Invalid argument)
- return;
- }
- public bool Poll_internal (SelectMode mode, int timeout, Socket source, out int error)
- {
- error = 0;
- throw new NotImplementedException();
- }
- public int Receive_internal(byte[] buffer, int offset, int count, SocketFlags flags,
- out int error)
- {
- error = 0;
- int ret = 0;
- if (jSocket == null || !jSocket.isConnected())
- {
- error = 10057; //WSAENOTCONN (Socket is not connected)
- return ret;
- }
- try
- {
- ret = jSocket.getInputStream().read(vmw.common.TypeUtils.ToSByteArray(buffer), offset, count);
- if (ret < 0) ret = 0;
- }
- catch (Exception e)
- {
- error = 10054; //WSAECONNRESET (Connection reset by peer)
- ret = 0;
- #if DEBUG
- Console.WriteLine("Caught exception during Receive_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
- #endif
- }
- return ret;
- }
- public int RecvFrom_internal(byte[] buffer, int offset, int count, SocketFlags flags,
- ref SocketAddress sockaddr, out int error)
- {
- return Receive_internal(buffer, offset, count, flags, out error);
- }
- public int Send_internal(byte[] buf, int offset, int count, SocketFlags flags,
- out int error)
- {
- error = 0;
- int ret = 0;
- if (jSocket == null || !jSocket.isConnected())
- {
- error = 10057; //WSAENOTCONN (Socket is not connected)
- return ret;
- }
- try
- {
- jSocket.getOutputStream().write(vmw.common.TypeUtils.ToSByteArray(buf), offset, count);
- ret = count;
- }
- catch (Exception e)
- {
- error = 10054; //WSAECONNRESET (Connection reset by peer)
- ret = 0;
- #if DEBUG
- Console.WriteLine("Caught exception during Send_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
- #endif
- }
- return ret;
- }
- public int SendTo_internal(byte[] buffer, int offset, int count,
- SocketFlags flags, SocketAddress sa, out int error)
- {
- return Send_internal(buffer, offset, count, flags, out error);
- }
- public void SetSocketOption_internal (SocketOptionLevel level,
- SocketOptionName name, object obj_val,
- byte [] byte_val, int int_val, out int error)
- {
- error = 0;
- if (byte_val != null)
- {
- error = -1;
- throw new NotImplementedException();
- }
- if (jSocket == null)
- {
- error = 10022; //WSAEINVAL (Invalid argument)
- return;
- }
- switch (level)
- {
- case SocketOptionLevel.IPv6:
- error = 10042; //WSAENOPROTOOPT (Bad protocol option)
- return;
- case SocketOptionLevel.IP:
- if (name != SocketOptionName.NoDelay)
- {
- error = 10042; //WSAENOPROTOOPT (Bad protocol option)
- return;
- }
- break;
- case SocketOptionLevel.Udp:
- if (name == SocketOptionName.NoDelay)
- {
- error = 10042; //WSAENOPROTOOPT (Bad protocol option)
- }
- else
- {
- error = 10022; //WSAEINVAL (Invalid argument)
- }
- return;
- case SocketOptionLevel.Tcp:
- if (name != SocketOptionName.NoDelay)
- {
- error = 10022; //WSAEINVAL (Invalid argument)
- return;
- }
- break;
- }
- try
- {
- bool bval = false;
- int ival = 0;
- switch (name)
- {
- case SocketOptionName.DontLinger:
- jSocket.setSoLinger(false, 0);
- break;
- case SocketOptionName.Linger:
- LingerOption lval = obj_val as LingerOption;
- if (lval != null)
- {
- jSocket.setSoLinger(lval.Enabled, lval.LingerTime);
- }
- else
- {
- error = 10022; //WSAEINVAL (Invalid argument)
- }
- break;
- case SocketOptionName.KeepAlive:
- if (obj_val != null)
- {
- bval = ((int)obj_val == 0)?false:true;
- }
- else
- {
- bval = (int_val == 0)?false:true;
- }
- jSocket.setKeepAlive(bval);
- break;
- case SocketOptionName.NoDelay:
- if (obj_val != null)
- {
- bval = ((int)obj_val == 0)?false:true;
- }
- else
- {
- bval = (int_val == 0)?false:true;
- }
- jSocket.setTcpNoDelay(bval);
- break;
- case SocketOptionName.ReceiveBuffer:
- ival = int_val;
- if (obj_val != null)
- {
- ival = (int) obj_val;
- }
- jSocket.setReceiveBufferSize(ival);
- break;
- case SocketOptionName.ReceiveTimeout:
- ival = int_val;
- if (obj_val != null)
- {
- ival = (int) obj_val;
- }
- jSocket.setSoTimeout(ival);
- break;
- case SocketOptionName.ReuseAddress:
- if (obj_val != null)
- {
- bval = ((int)obj_val == 0)?false:true;
- }
- else
- {
- bval = (int_val == 0)?false:true;
- }
- jSocket.setReuseAddress(bval);
- break;
- case SocketOptionName.SendBuffer:
- ival = int_val;
- if (obj_val != null)
- {
- ival = (int) obj_val;
- }
- jSocket.setSendBufferSize(ival);
- break;
- case SocketOptionName.OutOfBandInline:
- if (obj_val != null)
- {
- bval = ((int)obj_val == 0)?false:true;
- }
- else
- {
- bval = (int_val == 0)?false:true;
- }
- jSocket.setOOBInline(bval);
- break;
- default:
- error = 10022; //WSAEINVAL (Invalid argument)
- break;
- }
- }
- catch (Exception e)
- {
- error = 10022; //WSAEINVAL (Invalid argument)
- obj_val = null;
- }
- }
- public void GetSocketOption_obj_internal(SocketOptionLevel level, SocketOptionName name,
- out object obj_val, out int error)
- {
- obj_val = null;
- error = 0;
- if (jSocket == null)
- {
- error = 10022; //WSAEINVAL (Invalid argument)
- return;
- }
- switch (level)
- {
- case SocketOptionLevel.IPv6:
- error = 10042; //WSAENOPROTOOPT (Bad protocol option)
- return;
- case SocketOptionLevel.IP:
- if (name != SocketOptionName.NoDelay)
- {
- error = 10042; //WSAENOPROTOOPT (Bad protocol option)
- return;
- }
- break;
- case SocketOptionLevel.Udp:
- if (name == SocketOptionName.NoDelay)
- {
- error = 10042; //WSAENOPROTOOPT (Bad protocol option)
- }
- else
- {
- error = 10022; //WSAEINVAL (Invalid argument)
- }
- return;
- case SocketOptionLevel.Tcp:
- if (name != SocketOptionName.NoDelay)
- {
- error = 10022; //WSAEINVAL (Invalid argument)
- return;
- }
- break;
- }
- try
- {
- bool bval = false;
- int ival = 0;
- switch (name)
- {
- case SocketOptionName.DontLinger:
- ival = jSocket.getSoLinger();
- if (ival == -1)
- {
- obj_val = 1;
- }
- else
- {
- obj_val = 0;
- }
- break;
- case SocketOptionName.Linger:
- ival = jSocket.getSoLinger();
- if (ival == -1)
- {
- ival = 0;
- }
- LingerOption ret = new LingerOption((ival != 0), ival);
- obj_val = ret;
- break;
- case SocketOptionName.KeepAlive:
- bval = jSocket.getKeepAlive();
- obj_val = ((bval)?1:0);
- break;
- case SocketOptionName.NoDelay:
- bval = jSocket.getTcpNoDelay();
- obj_val = ((bval)?1:0);
- break;
- case SocketOptionName.ReceiveBuffer:
- ival = jSocket.getReceiveBufferSize();
- obj_val = ival;
- break;
- case SocketOptionName.ReceiveTimeout:
- ival = jSocket.getSoTimeout();
- obj_val = ival;
- break;
- case SocketOptionName.ReuseAddress:
- bval = jSocket.getReuseAddress();
- obj_val = ((bval)?1:0);
- break;
- case SocketOptionName.SendBuffer:
- ival = jSocket.getSendBufferSize();
- obj_val = ival;
- break;
- case SocketOptionName.OutOfBandInline:
- bval = jSocket.getOOBInline();
- obj_val = ((bval)?1:0);
- break;
- default:
- error = 10022; //WSAEINVAL (Invalid argument)
- break;
- }
- }
- catch (Exception e)
- {
- error = 10022; //WSAEINVAL (Invalid argument)
- obj_val = null;
- }
- }
-
- public void GetSocketOption_arr_internal(SocketOptionLevel level, SocketOptionName name,
- ref byte[] byte_val, out int error)
- {
- error = -1;
- throw new NotImplementedException();
- }
- public int WSAIoctl (int ioctl_code, byte [] input, byte [] output, out int error)
- {
- error = -1;
- throw new NotImplementedException();
- }
- public void Shutdown_internal(SocketShutdown how, out int error)
- {
- error = 0;
- if (jSocket == null || !jSocket.isConnected())
- {
- error = 10057; //WSAENOTCONN (Socket is not connected)
- return;
- }
- try
- {
- switch (how)
- {
- case SocketShutdown.Receive:
- jSocket.shutdownInput();
- break;
- case SocketShutdown.Send:
- jSocket.shutdownOutput();
- break;
- case SocketShutdown.Both:
- jSocket.shutdownInput();
- jSocket.shutdownOutput();
- break;
- }
- }
- catch (Exception e)
- {
- error = 10022; //WSAEINVAL (Invalid argument)
- #if DEBUG
- Console.WriteLine("Caught exception during Shutdown_internal - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
- #endif
- }
- }
- public void RegisterSelector(java.nio.channels.Selector selector, int mode, Socket source, out int error)
- {
- throw new InvalidOperationException();
- }
- public bool CheckConnectionFinished()
- {
- throw new InvalidOperationException();
- }
- public GHSocket ChangeToSSL(EndPoint remote_end)
- {
- return this;
- }
- }
- }
|