|
-
January 21st, 2010, 05:05 AM
#9
Re: HELP PLEASE - Modifying Open-Source Project
client netstream.cs
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using Yoozy;
namespace netcl
{
public partial class NetStream
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Socket accepted = null;
IPEndPoint local = null;
int port = 28137;
string delimiter = "netcl_end";
public event _OnConnect OnConnect;
public event _OnAccept OnAccept;
public event _OnCloseConnection OnCloseConnection;
public event _UpdateTraffic OnUpdate;
private event _CommandQuery OnCommandQuery;
byte[] buffer = new byte[1024000];
Store store = new Store();
bool busy = false;
bool is_file_in_progress = false;
Encoding unc = Encoding.Default;
long in_traffic = 0;
long out_traffic = 0;
Compression compress = new Compression();
bool _connected;
public NetStream()
{
local = new IPEndPoint(IPAddress.Any, port);
_connected = false;
}
public NetStream(int Port)
{
local = new IPEndPoint(IPAddress.Any, port);
}
~NetStream()
{
socket.Close();
if (accepted != null)
{
_connected = false;
accepted.Close();
}
}
public bool isBusy
{
get
{
return busy;
}
set
{
busy = value;
}
}
public bool isFileOperation
{
get
{
return is_file_in_progress;
}
set
{
is_file_in_progress = value;
}
}
/// <summary>
/// Gets or sets data delemiter
/// </summary>
public string Delimiter
{
set
{
delimiter = value;
}
get
{
return delimiter;
}
}
/// <summary>
/// Async connect to host (OnConnect event)
/// </summary>
/// <param name="address">Ip address of host</param>
/// <param name="port">Port number</param>
public void Connect(string address, int port)
{
try
{
accepted = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress addr = IPAddress.Parse(address);
accepted.BeginConnect(addr, port, new AsyncCallback(ConnectCallback), null);
}
catch (Exception ex)
{
_connected = false;
OnCloseConnection(ex.Message);
}
}
public void Disconnect()
{
Shutdown("Disconnected by user");
}
/// <summary>
/// Async listen
/// </summary>
public void Listen()
{
try
{
socket.Bind(local);
socket.BeginAccept(new AsyncCallback(AcceptCallback), null);
}
catch (Exception ex)
{
_connected = false;
Shutdown(ex.Message);
return;
}
}
/// <summary>
/// Async recieve data
/// </summary>
/// <param name="callback">Delegate to call when data to be recieved</param>
public void RecieveData(_OnRecieveData callback)
{
try
{
accepted.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(RecieveCallback), callback);
}
catch (Exception ex)
{
_connected = false;
Shutdown(ex.Message);
return;
}
}
/// <summary>
/// Async send data
/// </summary>
/// <param name="data">data to send</param>
/// <param name="callback">delegate to call, when data will be sended</param>
public void SendData(byte[] data, _OnSendData callback)
{
try
{
accepted.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), callback);
}
catch (Exception ex)
{
_connected = false;
Shutdown(ex.Message);
return;
}
}
void Shutdown(string msg)
{
_connected = false;
OnCloseConnection(msg);
if (accepted != null)
{
accepted.Close();
}
socket.Close();
}
public bool Connected
{
get
{
return _connected;
}
}
/// <summary>
/// Send command and recieve answer
/// </summary>
/// <param name="cmd">Command name</param>
/// <param name="callback">Delegate to call, when answer recieved</param>
public void CommandQuery(string cmd, _CommandQuery callback)
{
OnCommandQuery = callback;
string c_cmd = compress.CompressData(cmd);
SendData(Encoding.Default.GetBytes(c_cmd + delimiter), new _OnSendData(CQsendOK));
}
private void CQsendOK(int b)
{
RecieveData(new _OnRecieveData(CQRecieveOK));
}
private void CQRecieveOK(byte[] data)
{
byte[] decompressed = compress.DecompressData(data);
OnCommandQuery(decompressed);
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|