- Much work on the networking

- ServerSocket done.
- ClientSocket done.
- Few fixes required:
   - Warning in ServerSocket
   - Friends possible in C#?
This commit is contained in:
u8sand_cp 2010-07-19 07:09:34 -07:00
parent 6987d8178f
commit a26280b711
5 changed files with 228 additions and 69 deletions

View file

@ -74,7 +74,6 @@
<Compile Include="Networking\ClientSocket.cs" /> <Compile Include="Networking\ClientSocket.cs" />
<Compile Include="Networking\Server.cs" /> <Compile Include="Networking\Server.cs" />
<Compile Include="Networking\ServerSocket.cs" /> <Compile Include="Networking\ServerSocket.cs" />
<Compile Include="Networking\Socket.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Scripting\GameObject.cs" /> <Compile Include="Scripting\GameObject.cs" />
<Compile Include="Scripting\GameObjectCollection.cs" /> <Compile Include="Scripting\GameObjectCollection.cs" />

View file

@ -2,21 +2,69 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Net;
using System.Net.Sockets;
// TODO: everything D: // TODO: Make ClientSocket a friend of ServerSocket so I can make sock and type private
// other, havn't thought of what else I need
namespace MudEngine.Networking namespace MudEngine.Networking
{ {
class ClientSocket : Socket class ClientSocket
{ {
public ClientSocket() public ClientSocket()
{ {
type = 0;
ip = 0;
} }
~ClientSocket() ~ClientSocket()
{ {
type = 0;
ip = 0;
}
public int Send(byte[] ba,int size,SocketFlags sf)
{
try
{
sock.Send(ba, size, sf);
}
catch (Exception)
{
return -1;
}
return 1;
}
public int Receive(ref byte[] ba, int size, SocketFlags sf)
{
try
{
sock.Receive(ba, size, sf);
}
catch (Exception)
{
return -1;
}
return 1;
}
public int CleanUp()
{
try
{
sock.Disconnect(true);
sock.Close();
sock.Dispose();
ip = 0;
type = 0;
}
catch (Exception)
{
return -1;
}
return 1;
} }
public string ip; public ProtocolType type;
public long ip;
public Socket sock;
} }
} }

View file

@ -2,7 +2,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using MudEngine.Networking;
// TODO: everything D: // TODO: everything D:
@ -19,6 +18,7 @@ namespace MudEngine.Networking
} }
private ServerSocket server; //private ServerSocket server;
//private ClientSocket[] clients;
} }
} }

View file

@ -2,17 +2,14 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Net;
using System.Net.Sockets;
// TODO: everything D: // TODO: ln 167 warning, catch more errors and don't return all of em as unknown :o
namespace MudEngine.Networking namespace MudEngine.Networking
{ {
enum SocketType class ServerSocket
{
TCP = 1,
UDP = 2
};
class ServerSocket : Socket
{ {
public ServerSocket() public ServerSocket()
{ {
@ -27,55 +24,191 @@ namespace MudEngine.Networking
stage = 0; stage = 0;
type = 0; type = 0;
} }
// all methods return int, 1 = success, -1 = error, > 1 are just extra info w/ success public int Initialize(int p,ProtocolType pt)
// < -1, known error find it out with: (string)getError(er_code); {
try
{
if (stage == 0)
{
if (pt != ProtocolType.Tcp && pt != ProtocolType.Udp)
return -3;
port = p;
type = pt;
stage++;
}
else
return -2;
}
catch (Exception)
{
return -1;
}
return 1;
}
public int Start()
{
try
{
if (stage == 1)
{
if (type == ProtocolType.Tcp)
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, type);
else
sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, type);
stage++;
}
else
return -2;
}
catch (Exception)
{
return -1;
}
return 1;
}
public int Bind()
{
try
{
if (stage == 2)
{
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, port);
sock.Bind(ipep);
stage++;
}
else
return -2;
}
catch (Exception)
{
return -1;
}
return 1;
}
public int Listen()
{
try
{
if (stage == 3 && type == ProtocolType.Udp)
return -4;
if (stage == 3)
{
sock.Listen(10);
stage++;
}
else
return -2;
}
catch (Exception)
{
return -1;
}
return 1;
}
public int Accept(ref ClientSocket cs)
{
try
{
if (type == ProtocolType.Udp)
return -2;
if (stage == 4)
stage++;
public int init(int p, SocketType st) if (stage == 5)
{ {
return 0; cs.sock = sock.Accept();
cs.type = ProtocolType.Tcp;
} }
public int start() else
return -2;
}
catch (Exception)
{ {
return 0; return -1;
} }
public int bind() return 1;
}
public int SendTo(byte[] ba, int size, SocketFlags sf, ref ClientSocket rcs)
{ {
return 0; try
}
public int listen()
{ {
return 0; if (type != ProtocolType.Udp)
} return -3;
public int accept()
if (stage == 3)
stage += 2;
if (stage == 5)
{ {
return 0; IPEndPoint ipep = new IPEndPoint(rcs.ip, port);
sock.SendTo(ba, size, sf, ipep);
} }
public int send() else
return -2;
}
catch (Exception)
{ {
return 0; return -1;
} }
public int recv() return 1;
}
public int ReceiveFrom(ref byte[] ba, int size, SocketFlags sf, ref ClientSocket rcs)
{ {
return 0; try
}
public int end()
{ {
return 0; if (type != ProtocolType.Udp)
return -3;
if (stage == 3)
stage += 2;
if (stage == 5)
{
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 0);
EndPoint ep = (EndPoint)ipep;
sock.ReceiveFrom(ba, size, sf, ref ep);
rcs.ip = ipep.Address.Address; // Why am I getting this warning?
} }
public string getError(int er_code) else
return -2;
}
catch (Exception)
{
return -1;
}
return 1;
}
public int CleanUp()
{
try
{
sock.Close();
sock.Dispose();
}
catch (Exception)
{
return -1;
}
return 1;
}
public string GetError(int er_code)
{ {
if (er_code > 0) if (er_code > 0)
return "No Error"; return "No Error";
switch (er_code) switch (er_code)
{ {
case -2:
return "Method cannot be called yet.";
case -3:
return "ProtocolType not supported.";
case -4:
return "The ProtocolType does not support that method.";
default: default:
return "Unknown Error"; return "Unknown Error";
} }
} }
private int stage; public int stage { get; private set; }
private int port; public int port { get; private set; }
private SocketType type; public ProtocolType type { get; private set; }
private Socket sock;
} }
} }

View file

@ -1,21 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// TODO: everything D:
namespace MudEngine.Networking
{
class Socket
{
public Socket()
{
}
~Socket()
{
}
}
}