public static SocketErrors GetSocketErrorForTcpError(TcpError error) { switch (error) { // These are defined in Contracts\NetStack.Contracts\TcpConnectionContract.sg. case TcpError.Unknown: return(SocketErrors.SocketError); case TcpError.AlreadyConnected: return(SocketErrors.WSAEISCONN); case TcpError.Refused: return(SocketErrors.WSAECONNREFUSED); case TcpError.Reset: return(SocketErrors.WSAECONNRESET); case TcpError.Timeout: return(SocketErrors.WSAETIMEDOUT); case TcpError.ProtocolViolation: return(SocketErrors.WSAECONNRESET); case TcpError.ResourcesExhausted: return(SocketErrors.SocketError); case TcpError.Closed: return(SocketErrors.WSAEDISCON); default: return(SocketErrors.SocketError); } }
void InvokeOnError(TcpError type, string message) { if (OnError != null) { OnError(this, new TcpErrorEventArgs(type, message)); } }
internal void onError(TcpStruct localTcpInfo, TcpStruct remoteTcpInfo, TcpError error, Exception ex, TcpOptionType type, ITcpReader tcpReader) { if (OnError != null) { OnError(localTcpInfo, remoteTcpInfo, error, ex, type, tcpReader); } }
public static string GetMessageForTcpError(TcpError error) { switch (error) { // These are defined in Contracts\NetStack.Contracts\TcpConnectionContract.sg. case TcpError.Unknown: return("Unknown error"); case TcpError.AlreadyConnected: return("The connection is already in use."); case TcpError.Refused: return("The remote host actively refused the connection."); case TcpError.Reset: return("The connection was reset by the peer."); case TcpError.Timeout: return("No response was received."); case TcpError.ProtocolViolation: return("An invalid packet was received from the peer."); case TcpError.ResourcesExhausted: return("Insufficient resources."); case TcpError.Closed: return("The TCP connection has been closed."); default: return(String.Format("Unknown TcpError code ({0}).", ((int)error).ToString())); } }
/// <summary> /// Closes the packet listener /// </summary> public void EndReceive() { try { _instance.PacketReceived -= OnPacketReceived; } catch (Exception ex) { TcpError?.Invoke(ex); } }
/// <summary> /// Encodes a string as a byte array, breaks it into packets, and sequentially sends them to the given endpoint /// </summary> /// <param name="message"></param> /// <param name="packetSize"></param> /// <param name="destination"></param> public void SendMessage(string message, uint packetSize, EndPoint destination) { try { // Converts the string into bytes and calls the send function Send(Encoding.ASCII.GetBytes(message), packetSize, destination); } catch (Exception ex) { TcpError?.Invoke(ex); } }
/// <summary> /// Reads in a file, breaks it into packets, and sequentially sends them to the given endpoint /// </summary> /// <param name="data"></param> /// <param name="packetSize"></param> /// <param name="destination"></param> public void SendFile(string filePath, uint packetSize, EndPoint destination) { try { // Converts the file into bytes and calls the send function Send(File.ReadAllBytes(filePath), packetSize, destination); } catch (Exception ex) { TcpError?.Invoke(ex); } }
/// <summary> /// Begins a packet listener targeting the given endpoint /// </summary> /// <param name="remoteEndPoint"></param> public void BeginReceive(EndPoint remoteEndPoint) { try { _instance.BeginReceivePackets(remoteEndPoint); _instance.PacketReceived += OnPacketReceived; } catch (Exception ex) { TcpError?.Invoke(ex); } }
/// <summary> /// Breaks the given byte array into packets and sequentially sends them to the given endpoint /// </summary> /// <param name="data"></param> /// <param name="packetSize"></param> /// <param name="destination"></param> public void Send(byte[] data, uint packetSize, EndPoint destination) { try { var packets = PacketHandler.ToPackets(data, packetSize); foreach (var packet in packets) { _instance.SendPacket(packet, destination); } } catch (Exception ex) { TcpError?.Invoke(ex); } }
/// <summary> /// Fires when a packet is received - Processes packets /// </summary> /// <param name="packet"></param> private void OnPacketReceived(Packet packet) { try { PacketReceived?.Invoke(packet); if (packet.PacketNumber + 1 != packet.TotalPackets) { tempPackets.Add(packet); } else { var fileData = PacketHandler.FromPackets(tempPackets.ToArray()); FileReceived?.Invoke(fileData); tempPackets.Clear(); } } catch (Exception ex) { TcpError?.Invoke(ex); } }
public TcpException(TcpError error) : this(error, GetMessageForTcpError(error)) { }
public TcpException(TcpError error, string message) : base(GetSocketErrorForTcpError(error), message) { _tcpError = error; }
public TcpErrorEventArgs(TcpError type, string message) { ErrorType = type; Message = message; }
public TcpException(TcpError error) { _message = error.ToString(); _error = error; }
public TcpException(string message, TcpError error) { _message = message; _error = error; }
internal void onError(TcpStruct localTcpInfo, TcpStruct remoteTcpInfo, TcpError error, Exception ex, TcpOptionType type, __listen__readSmmp tcpReader) { if (OnError != null) OnError(localTcpInfo, remoteTcpInfo, error, ex, type, tcpReader); }